-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
bpo-36346: array: Don't use deprecated APIs #19653
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
240e7e5
bpo-36346: array: Don't use deprecated APIs
methane 1d9569a
update doc
methane c075693
Add what's new entry
methane 3cf0028
Apply suggestions from code review
methane fbc3925
Update doc
methane bea1779
Don't use deprecated "u#" format.
methane df0ea67
more assert
methane 8454e8c
remove redundant NULL check
methane 4ddac33
remove redundant assert
methane 21b49a4
Merge branch 'master' into array-wchar
methane File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
|
@@ -235,13 +235,13 @@ BB_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) | |
static PyObject * | ||
u_getitem(arrayobject *ap, Py_ssize_t i) | ||
{ | ||
return PyUnicode_FromOrdinal(((Py_UNICODE *) ap->ob_item)[i]); | ||
return PyUnicode_FromOrdinal(((wchar_t *) ap->ob_item)[i]); | ||
} | ||
|
||
static int | ||
u_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) | ||
{ | ||
Py_UNICODE *p; | ||
wchar_t *p; | ||
Py_ssize_t len; | ||
|
||
if (!PyArg_Parse(v, "u#;array item must be unicode character", &p, &len)) | ||
|
@@ -252,7 +252,7 @@ u_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) | |
return -1; | ||
} | ||
if (i >= 0) | ||
((Py_UNICODE *)ap->ob_item)[i] = p[0]; | ||
((wchar_t *)ap->ob_item)[i] = p[0]; | ||
return 0; | ||
} | ||
|
||
|
@@ -530,7 +530,7 @@ d_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) | |
|
||
DEFINE_COMPAREITEMS(b, signed char) | ||
DEFINE_COMPAREITEMS(BB, unsigned char) | ||
DEFINE_COMPAREITEMS(u, Py_UNICODE) | ||
DEFINE_COMPAREITEMS(u, wchar_t) | ||
DEFINE_COMPAREITEMS(h, short) | ||
DEFINE_COMPAREITEMS(HH, unsigned short) | ||
DEFINE_COMPAREITEMS(i, int) | ||
|
@@ -548,7 +548,7 @@ DEFINE_COMPAREITEMS(QQ, unsigned long long) | |
static const struct arraydescr descriptors[] = { | ||
{'b', 1, b_getitem, b_setitem, b_compareitems, "b", 1, 1}, | ||
{'B', 1, BB_getitem, BB_setitem, BB_compareitems, "B", 1, 0}, | ||
{'u', sizeof(Py_UNICODE), u_getitem, u_setitem, u_compareitems, "u", 0, 0}, | ||
{'u', sizeof(wchar_t), u_getitem, u_setitem, u_compareitems, "u", 0, 0}, | ||
{'h', sizeof(short), h_getitem, h_setitem, h_compareitems, "h", 1, 1}, | ||
{'H', sizeof(short), HH_getitem, HH_setitem, HH_compareitems, "H", 1, 0}, | ||
{'i', sizeof(int), i_getitem, i_setitem, i_compareitems, "i", 1, 1}, | ||
|
@@ -1660,7 +1660,7 @@ array_array_tobytes_impl(arrayobject *self) | |
/*[clinic input] | ||
array.array.fromunicode | ||
|
||
ustr: Py_UNICODE(zeroes=True) | ||
ustr: unicode | ||
/ | ||
|
||
Extends this array with data from the unicode string ustr. | ||
|
@@ -1671,25 +1671,29 @@ some other type. | |
[clinic start generated code]*/ | ||
|
||
static PyObject * | ||
array_array_fromunicode_impl(arrayobject *self, const Py_UNICODE *ustr, | ||
Py_ssize_clean_t ustr_length) | ||
/*[clinic end generated code: output=cf2f662908e2befc input=150f00566ffbca6e]*/ | ||
array_array_fromunicode_impl(arrayobject *self, PyObject *ustr) | ||
/*[clinic end generated code: output=24359f5e001a7f2b input=025db1fdade7a4ce]*/ | ||
{ | ||
char typecode; | ||
|
||
typecode = self->ob_descr->typecode; | ||
if (typecode != 'u') { | ||
if (self->ob_descr->typecode != 'u') { | ||
PyErr_SetString(PyExc_ValueError, | ||
"fromunicode() may only be called on " | ||
"unicode type arrays"); | ||
return NULL; | ||
} | ||
if (ustr_length > 0) { | ||
|
||
Py_ssize_t ustr_length = PyUnicode_AsWideChar(ustr, NULL, 0); | ||
methane marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (ustr_length > 1) { | ||
ustr_length--; /* trim trailing NUL character */ | ||
Py_ssize_t old_size = Py_SIZE(self); | ||
if (array_resize(self, old_size + ustr_length) == -1) | ||
if (array_resize(self, old_size + ustr_length) == -1) { | ||
return NULL; | ||
} | ||
|
||
Py_ssize_t res = PyUnicode_AsWideChar( | ||
ustr, ((wchar_t *)self->ob_item) + old_size, ustr_length); | ||
if (res < 0) { // must not happen | ||
methane marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return NULL; | ||
memcpy(self->ob_item + old_size * sizeof(Py_UNICODE), | ||
ustr, ustr_length * sizeof(Py_UNICODE)); | ||
} | ||
} | ||
|
||
Py_RETURN_NONE; | ||
|
@@ -1709,14 +1713,12 @@ static PyObject * | |
array_array_tounicode_impl(arrayobject *self) | ||
/*[clinic end generated code: output=08e442378336e1ef input=127242eebe70b66d]*/ | ||
{ | ||
char typecode; | ||
typecode = self->ob_descr->typecode; | ||
if (typecode != 'u') { | ||
if (self->ob_descr->typecode != 'u') { | ||
PyErr_SetString(PyExc_ValueError, | ||
"tounicode() may only be called on unicode type arrays"); | ||
return NULL; | ||
} | ||
return PyUnicode_FromWideChar((Py_UNICODE *) self->ob_item, Py_SIZE(self)); | ||
return PyUnicode_FromWideChar((wchar_t *) self->ob_item, Py_SIZE(self)); | ||
} | ||
|
||
/*[clinic input] | ||
|
@@ -2675,30 +2677,21 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | |
Py_DECREF(v); | ||
} | ||
else if (initial != NULL && PyUnicode_Check(initial)) { | ||
Py_UNICODE *ustr; | ||
Py_ssize_t n; | ||
|
||
ustr = PyUnicode_AsUnicode(initial); | ||
wchar_t *ustr = PyUnicode_AsWideCharString(initial, &n); | ||
if (ustr == NULL) { | ||
PyErr_NoMemory(); | ||
Py_DECREF(a); | ||
return NULL; | ||
} | ||
|
||
n = PyUnicode_GET_DATA_SIZE(initial); | ||
if (n > 0) { | ||
arrayobject *self = (arrayobject *)a; | ||
char *item = self->ob_item; | ||
item = (char *)PyMem_Realloc(item, n); | ||
if (item == NULL) { | ||
PyErr_NoMemory(); | ||
Py_DECREF(a); | ||
return NULL; | ||
if (self->ob_item != NULL) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick: PyMem_Free(NULL) does nothing, it's part of its API ;-) |
||
PyMem_Free(self->ob_item); | ||
} | ||
self->ob_item = item; | ||
Py_SET_SIZE(self, n / sizeof(Py_UNICODE)); | ||
memcpy(item, ustr, n); | ||
self->allocated = Py_SIZE(self); | ||
self->ob_item = (char *)ustr; | ||
Py_SET_SIZE(self, n); | ||
self->allocated = n; | ||
} | ||
} | ||
else if (initial != NULL && array_Check(initial) && len > 0) { | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.