summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/src/multiarray/scalarapi.c31
-rw-r--r--numpy/core/src/multiarray/scalartypes.c.src4
-rw-r--r--numpy/core/tests/test_unicode.py6
3 files changed, 39 insertions, 2 deletions
diff --git a/numpy/core/src/multiarray/scalarapi.c b/numpy/core/src/multiarray/scalarapi.c
index 00c71f913..d9bc492be 100644
--- a/numpy/core/src/multiarray/scalarapi.c
+++ b/numpy/core/src/multiarray/scalarapi.c
@@ -641,6 +641,35 @@ PyArray_Scalar(void *data, PyArray_Descr *descr, PyObject *base)
itemsize = (((itemsize - 1) >> 2) + 1) << 2;
}
}
+#if PY_VERSION_HEX >= 0x03030000
+ if (type_num == NPY_UNICODE) {
+ PyObject *u, *args;
+ int byteorder;
+
+#if NPY_BYTE_ORDER == NPY_LITTLE_ENDIAN
+ byteorder = -1;
+#elif NPY_BYTE_ORDER == NPY_BIG_ENDIAN
+ byteorder = +1;
+#else
+ #error Endianness undefined ?
+#endif
+ if (swap) byteorder *= -1;
+
+ u = PyUnicode_DecodeUTF32(data, itemsize, NULL, &byteorder);
+ if (u == NULL) {
+ return NULL;
+ }
+ args = Py_BuildValue("(O)", u);
+ if (args == NULL) {
+ Py_DECREF(u);
+ return NULL;
+ }
+ obj = type->tp_new(type, args, NULL);
+ Py_DECREF(u);
+ Py_DECREF(args);
+ return obj;
+ }
+#endif
if (type->tp_itemsize != 0) {
/* String type */
obj = type->tp_alloc(type, itemsize);
@@ -672,6 +701,7 @@ PyArray_Scalar(void *data, PyArray_Descr *descr, PyObject *base)
memcpy(destptr, data, itemsize);
return obj;
}
+#if PY_VERSION_HEX < 0x03030000
else if (type_num == NPY_UNICODE) {
/* tp_alloc inherited from Python PyBaseObject_Type */
PyUnicodeObject *uni = (PyUnicodeObject*)obj;
@@ -743,6 +773,7 @@ PyArray_Scalar(void *data, PyArray_Descr *descr, PyObject *base)
#endif
return obj;
}
+#endif /* PY_VERSION_HEX < 0x03030000 */
else {
PyVoidScalarObject *vobj = (PyVoidScalarObject *)obj;
vobj->base = NULL;
diff --git a/numpy/core/src/multiarray/scalartypes.c.src b/numpy/core/src/multiarray/scalartypes.c.src
index e547071cc..12745d720 100644
--- a/numpy/core/src/multiarray/scalartypes.c.src
+++ b/numpy/core/src/multiarray/scalartypes.c.src
@@ -2592,7 +2592,11 @@ finish:
*((npy_@name@ *)dest) = *((npy_@name@ *)src);
#elif @default@ == 1 /* unicode and strings */
if (itemsize == 0) { /* unicode */
+#if PY_VERSION_HEX >= 0x03030000
+ itemsize = PyUnicode_GetLength(robj) * PyUnicode_KIND(robj);
+#else
itemsize = ((PyUnicodeObject *)robj)->length * sizeof(Py_UNICODE);
+#endif
}
memcpy(dest, src, itemsize);
/* @default@ == 2 won't get here */
diff --git a/numpy/core/tests/test_unicode.py b/numpy/core/tests/test_unicode.py
index d47ac548b..7b27076e8 100644
--- a/numpy/core/tests/test_unicode.py
+++ b/numpy/core/tests/test_unicode.py
@@ -26,10 +26,12 @@ else:
return len(arr.data)
return len(buffer(arr))
+# In both cases below we need to make sure that the byte swapped value (as
+# UCS4) is still a valid unicode:
# Value that can be represented in UCS2 interpreters
-ucs2_value = u'\uFFFF'
+ucs2_value = u'\u0900'
# Value that cannot be represented in UCS2 interpreters (but can in UCS4)
-ucs4_value = u'\U0010FFFF'
+ucs4_value = u'\U00100900'
############################################################