FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

ctypes: remove use of legacy unicode API by methane · Pull Request #12340 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
14 changes: 7 additions & 7 deletions Modules/_ctypes/_ctypes.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1293,8 +1293,6 @@ static int
WCharArray_set_value(CDataObject *self, PyObject *value, void *Py_UNUSED(ignored))
{
Py_ssize_t result = 0;
Py_UNICODE *wstr;
Py_ssize_t len;

if (value == NULL) {
PyErr_SetString(PyExc_TypeError,
Expand All @@ -1309,12 +1307,14 @@ WCharArray_set_value(CDataObject *self, PyObject *value, void *Py_UNUSED(ignored
} else
Py_INCREF(value);

wstr = PyUnicode_AsUnicodeAndSize(value, &len);
if (wstr == NULL)
Py_ssize_t len = PyUnicode_AsWideChar(value, NULL, 0);
if (len < 0) {
return -1;
if ((size_t)len > self->b_size/sizeof(wchar_t)) {
PyErr_SetString(PyExc_ValueError,
"string too long");
}
// PyUnicode_AsWideChar() returns number of wchars including trailing null byte,
// when it is called with NULL.
if (((size_t)len-1) > self->b_size/sizeof(wchar_t)) {
PyErr_SetString(PyExc_ValueError, "string too long");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Is it worth providing in the exception message the provided length and what the max length actually is?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Maybe yes. But this file has similar codes which are not relating to wchar_t APIs. ("bytes string too long").
I want to keep this pull request simple.

result = -1;
goto done;
}
Expand Down
20 changes: 12 additions & 8 deletions Modules/_ctypes/cfield.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1229,9 +1229,6 @@ U_get(void *ptr, Py_ssize_t size)
static PyObject *
U_set(void *ptr, PyObject *value, Py_ssize_t length)
{
Py_UNICODE *wstr;
Py_ssize_t size;

/* It's easier to calculate in characters than in bytes */
length /= sizeof(wchar_t);

Expand All @@ -1242,9 +1239,14 @@ U_set(void *ptr, PyObject *value, Py_ssize_t length)
return NULL;
}

wstr = PyUnicode_AsUnicodeAndSize(value, &size);
if (wstr == NULL)
Py_ssize_t size = PyUnicode_AsWideChar(value, NULL, 0);
if (size < 0) {
return NULL;
}
// PyUnicode_AsWideChar() returns number of wchars including trailing null byte,
// when it is called with NULL.
size--;
assert(size >= 0);
if (size > length) {
PyErr_Format(PyExc_ValueError,
"string too long (%zd, maximum length %zd)",
Expand Down Expand Up @@ -1421,16 +1423,18 @@ BSTR_set(void *ptr, PyObject *value, Py_ssize_t size)

/* create a BSTR from value */
if (value) {
wchar_t* wvalue;
Py_ssize_t wsize;
wvalue = PyUnicode_AsUnicodeAndSize(value, &wsize);
if (wvalue == NULL)
wchar_t *wvalue = PyUnicode_AsWideCharString(value, &wsize);
if (wvalue == NULL) {
return NULL;
}
if ((unsigned) wsize != wsize) {
PyErr_SetString(PyExc_ValueError, "String too long for BSTR");
PyMem_Free(wvalue);
return NULL;
}
bstr = SysAllocStringLen(wvalue, (unsigned)wsize);
PyMem_Free(wvalue);
} else
bstr = NULL;

Expand Down

Back | FazBrowse Home | New Git URL