-
-
Notifications
You must be signed in to change notification settings - Fork 11.1k
BUG, MAINT: Stop using the error-prone deprecated Py_UNICODE apis #15385
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 all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
``np.str_`` scalars now support the buffer protocol | ||
--------------------------------------------------- | ||
``np.str_`` arrays are always stored as UCS4, so the corresponding scalars | ||
now expose this through the buffer interface, meaning | ||
``memoryview(np.str_('test'))`` now works. |
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
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
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
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
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 |
---|---|---|
|
@@ -450,12 +450,6 @@ static int | |
UNICODE_setitem(PyObject *op, void *ov, void *vap) | ||
{ | ||
PyArrayObject *ap = vap; | ||
PyObject *temp; | ||
Py_UNICODE *ptr; | ||
int datalen; | ||
#ifndef Py_UNICODE_WIDE | ||
char *buffer; | ||
#endif | ||
|
||
if (PyArray_IsZeroDim(op)) { | ||
return convert_to_scalar_and_retry(op, ov, vap, UNICODE_setitem); | ||
|
@@ -466,6 +460,8 @@ UNICODE_setitem(PyObject *op, void *ov, void *vap) | |
"setting an array element with a sequence"); | ||
return -1; | ||
} | ||
|
||
PyObject *temp; | ||
if (PyBytes_Check(op)) { | ||
/* Try to decode from ASCII */ | ||
temp = PyUnicode_FromEncodedObject(op, "ASCII", "strict"); | ||
|
@@ -476,18 +472,27 @@ UNICODE_setitem(PyObject *op, void *ov, void *vap) | |
else if ((temp=PyObject_Str(op)) == NULL) { | ||
return -1; | ||
} | ||
ptr = PyUnicode_AS_UNICODE(temp); | ||
if ((ptr == NULL) || (PyErr_Occurred())) { | ||
|
||
/* truncate if needed */ | ||
Py_ssize_t max_len = PyArray_DESCR(ap)->elsize >> 2; | ||
Py_ssize_t actual_len = PyUnicode_GetLength(temp); | ||
if (actual_len < 0) { | ||
Py_DECREF(temp); | ||
return -1; | ||
} | ||
datalen = PyUnicode_GET_DATA_SIZE(temp); | ||
if (actual_len > max_len) { | ||
Py_SETREF(temp, PyUnicode_Substring(temp, 0, max_len)); | ||
if (temp == NULL) { | ||
return -1; | ||
} | ||
actual_len = max_len; | ||
} | ||
|
||
#ifdef Py_UNICODE_WIDE | ||
memcpy(ov, ptr, PyArray_MIN(PyArray_DESCR(ap)->elsize, datalen)); | ||
#else | ||
Py_ssize_t num_bytes = actual_len * 4; | ||
|
||
char *buffer; | ||
if (!PyArray_ISALIGNED(ap)) { | ||
buffer = PyArray_malloc(PyArray_DESCR(ap)->elsize); | ||
buffer = PyArray_malloc(num_bytes); | ||
if (buffer == NULL) { | ||
Py_DECREF(temp); | ||
PyErr_NoMemory(); | ||
|
@@ -497,20 +502,23 @@ UNICODE_setitem(PyObject *op, void *ov, void *vap) | |
else { | ||
buffer = ov; | ||
} | ||
datalen = PyUCS2Buffer_AsUCS4(ptr, (npy_ucs4 *)buffer, | ||
datalen >> 1, PyArray_DESCR(ap)->elsize >> 2); | ||
datalen <<= 2; | ||
if (PyUnicode_AsUCS4(temp, (Py_UCS4 *)buffer, actual_len, 0) == NULL) { | ||
PyArray_free(buffer); | ||
Py_DECREF(temp); | ||
return -1; | ||
} | ||
|
||
if (!PyArray_ISALIGNED(ap)) { | ||
memcpy(ov, buffer, datalen); | ||
memcpy(ov, buffer, num_bytes); | ||
PyArray_free(buffer); | ||
} | ||
#endif | ||
|
||
/* Fill in the rest of the space with 0 */ | ||
if (PyArray_DESCR(ap)->elsize > datalen) { | ||
memset((char*)ov + datalen, 0, (PyArray_DESCR(ap)->elsize - datalen)); | ||
if (PyArray_DESCR(ap)->elsize > num_bytes) { | ||
memset((char*)ov + num_bytes, 0, (PyArray_DESCR(ap)->elsize - num_bytes)); | ||
} | ||
if (PyArray_ISBYTESWAPPED(ap)) { | ||
byte_swap_vector(ov, PyArray_DESCR(ap)->elsize >> 2, 4); | ||
byte_swap_vector(ov, actual_len, 4); | ||
} | ||
Py_DECREF(temp); | ||
return 0; | ||
|
@@ -2650,12 +2658,6 @@ STRING_nonzero (char *ip, PyArrayObject *ap) | |
return nonz; | ||
} | ||
|
||
#ifdef Py_UNICODE_WIDE | ||
#define PyArray_UCS4_ISSPACE Py_UNICODE_ISSPACE | ||
#else | ||
#define PyArray_UCS4_ISSPACE(ch) Py_STRING_ISSPACE((char)ch) | ||
#endif | ||
Comment on lines
-2653
to
-2657
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. Pretty sure this was a bug, but constructing a failing string is non-trivial |
||
|
||
static npy_bool | ||
UNICODE_nonzero (npy_ucs4 *ip, PyArrayObject *ap) | ||
{ | ||
|
@@ -2681,7 +2683,7 @@ UNICODE_nonzero (npy_ucs4 *ip, PyArrayObject *ap) | |
if (*ip == '\0') { | ||
seen_null = NPY_TRUE; | ||
} | ||
else if (seen_null || !PyArray_UCS4_ISSPACE(*ip)) { | ||
else if (seen_null || !Py_UNICODE_ISSPACE(*ip)) { | ||
nonz = NPY_TRUE; | ||
break; | ||
} | ||
|
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code never made sense in the first place, as
chararray.__new__
has an identity crisis over whether it's trying to benp.ndarray.__new__
ornp.array
, and acceptsstr
objects in place of thebuffer
.Edit: Perhaps it was a workaround for the original bug.