8000 Merge pull request #372 from certik/py3.3-cleaner · numpy/numpy@4676f33 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4676f33

Browse files
committed
Merge pull request #372 from certik/py3.3-cleaner
A fix for the ��PyUnicodeObject Python 3.3
2 parents fd15162 + f2ac38f commit 4676f33

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

numpy/core/src/multiarray/scalarapi.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,35 @@ PyArray_Scalar(void *data, PyArray_Descr *descr, PyObject *base)
641641
itemsize = (((itemsize - 1) >> 2) + 1) << 2;
642642
}
643643
}
644+
#if PY_VERSION_HEX >= 0x03030000
645+
if (type_num == NPY_UNICODE) {
646+
PyObject *u, *args;
647+
int byteorder;
648+
649+
#if NPY_BYTE_ORDER == NPY_LITTLE_ENDIAN
650+
byteorder = -1;
651+
#elif NPY_BYTE_ORDER == NPY_BIG_ENDIAN
652+
byteorder = +1;
653+
#else
654+
#error Endianness undefined ?
655+
#endif
656+
if (swap) byteorder *= -1;
657+
658+
u = PyUnicode_DecodeUTF32(data, itemsize, NULL, &byteorder);
659+
if (u == NULL) {
660+
return NULL;
661+
}
662+
args = Py_BuildValue("(O)", u);
663+
if (args == NULL) {
664+
Py_DECREF(u);
665+
return NULL;
666+
}
667+
obj = type->tp_new(type, args, NULL);
668+
Py_DECREF(u);
669+
Py_DECREF(args);
670+
return obj;
671+
}
672+
#endif
644673
if (type->tp_itemsize != 0) {
645674
/* String type */
646675
obj = type->tp_alloc(type, itemsize);
@@ -672,6 +701,7 @@ PyArray_Scalar(void *data, PyArray_Descr *descr, PyObject *base)
672701
memcpy(destptr, data, itemsize);
673702
return obj;
674703
}
704+
#if PY_VERSION_HEX < 0x03030000
675705
else if (type_num == NPY_UNICODE) {
676706
/* tp_alloc inherited from Python PyBaseObject_Type */
677707
PyUnicodeObject *uni = (PyUnicodeObject*)obj;
@@ -743,6 +773,7 @@ PyArray_Scalar(void *data, PyArray_Descr *descr, PyObject *base)
743773
#endif
744774
return obj;
745775
}
776+
#endif /* PY_VERSION_HEX < 0x03030000 */
746777
else {
747778
PyVoidScalarObject *vobj = (PyVoidScalarObject *)obj;
748779
vobj->base = NULL;

numpy/core/src/multiarray/scalartypes.c.src

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2592,7 +2592,11 @@ finish:
25922592
*((npy_@name@ *)dest) = *((npy_@name@ *)src);
25932593
#elif @default@ == 1 /* unicode and strings */
25942594
if (itemsize == 0) { /* unicode */
2595+
#if PY_VERSION_HEX >= 0x03030000
2596+
itemsize = PyUnicode_GetLength(robj) * PyUnicode_KIND(robj);
2597+
#else
25952598
itemsize = ((PyUnicodeObject *)robj)->length * sizeof(Py_UNICODE);
2599+
#endif
25962600
}
25972601
memcpy(dest, src, itemsize);
25982602
/* @default@ == 2 won't get here */

numpy/core/tests/test_unicode.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ def buffer_length(arr):
2626
return len(arr.data)
2727
return len(buffer(arr))
2828

29+
# In both cases below we need to make sure that the byte swapped value (as
30+
# UCS4) is still a valid unicode:
2931
# Value that can be represented in UCS2 interpreters
30-
ucs2_value = u'\uFFFF'
32+
ucs2_value = u'\u0900'
3133
# Value that cannot be represented in UCS2 interpreters (but can in UCS4)
32-
ucs4_value = u'\U0010FFFF'
34+
ucs4_value = u'\U00100900'
3335

3436

3537
############################################################

0 commit comments

Comments
 (0)
0