[Numpy-discussion] Status of NumPy and Python 3.3

Stefan Krah stefan-usenet@bytereef....
Sat Jul 28 13:19:57 CDT 2012


Ond??ej ??ert??k <ondrej.certik@gmail.com> wrote:
> So at some point, the strings get converted to numpy strings in 3.2,
> but not in 3.3.

PyArray_Scalar() must return a subtype of PyUnicodeObject. I'm boldly
assuming that data is in utf-32. If so, then this unoptimized version
should work:

diff --git a/numpy/core/src/multiarray/scalarapi.c b/numpy/core/src/multiarray/scalarapi.c
index 2e255c0..c134aed 100644
--- a/numpy/core/src/multiarray/scalarapi.c
+++ b/numpy/core/src/multiarray/scalarapi.c
@@ -643,7 +643,20 @@ PyArray_Scalar(void *data, PyArray_Descr *descr, PyObject *base)
     }
 #if PY_VERSION_HEX >= 0x03030000
     if (type_num == NPY_UNICODE) {
-        return PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, data, itemsize/4);
+        PyObject *b, *args;
+        b = PyBytes_FromStringAndSize(data, itemsize);
+        if (b == NULL) {
+            return NULL;
+        }
+        args = Py_BuildValue("(Os)", b, "utf-32");
+        if (args == NULL) {
+            Py_DECREF(b);
+            return NULL;
+        }
+        obj = type->tp_new(type, args, NULL);
+        Py_DECREF(b);
+        Py_DECREF(args);
+        return obj;
     }
 #endif
     if (type->tp_itemsize != 0) {


Stefan Krah




More information about the NumPy-Discussion mailing list