diff options
author | Travis E. Oliphant <teoliphant@gmail.com> | 2012-08-03 22:46:21 -0700 |
---|---|---|
committer | Travis E. Oliphant <teoliphant@gmail.com> | 2012-08-03 22:46:21 -0700 |
commit | 4676f33f9c77b04e9c599e642de7ab465f48ea8f (patch) | |
tree | 29b4ddb27c4d5d00dcdc5e329cbd6440480f5905 /numpy | |
parent | fd15162fbff5dd68c548284947d39bb2a2481183 (diff) | |
parent | f2ac38f09ff258339ef44572a3abba02019e1f55 (diff) | |
download | numpy-4676f33f9c77b04e9c599e642de7ab465f48ea8f.tar.gz |
Merge pull request #372 from certik/py3.3-cleaner
A fix for the €˜PyUnicodeObject Python 3.3
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/src/multiarray/scalarapi.c | 31 | ||||
-rw-r--r-- | numpy/core/src/multiarray/scalartypes.c.src | 4 | ||||
-rw-r--r-- | numpy/core/tests/test_unicode.py | 6 |
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' ############################################################ |