diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2012-04-17 19:52:01 -0600 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2012-04-21 10:29:10 -0600 |
commit | d78746bd838761279c666465859a4b650525b422 (patch) | |
tree | cb8f44e353de44d43afaff5a39c44fdb70c4840f | |
parent | 321a01462b4ce54aba30241dc0dd8a03f96b4347 (diff) | |
download | numpy-d78746bd838761279c666465859a4b650525b422.tar.gz |
BUG: ticket #1578, fix UNICODE_getitem.
Use PyUnicode_FromUCS4 in UNICODE_getitime so that the same memory
management is used for all parts of the returned PyUnicodeObject.
-rw-r--r-- | numpy/core/src/multiarray/arraytypes.c.src | 53 |
1 files changed, 4 insertions, 49 deletions
diff --git a/numpy/core/src/multiarray/arraytypes.c.src b/numpy/core/src/multiarray/arraytypes.c.src index 0e73c9fe7..0270675ef 100644 --- a/numpy/core/src/multiarray/arraytypes.c.src +++ b/numpy/core/src/multiarray/arraytypes.c.src @@ -315,56 +315,11 @@ CLONGDOUBLE_getitem(char *ip, PyArrayObject *ap) static PyObject * UNICODE_getitem(char *ip, PyArrayObject *ap) { - npy_intp elsize = PyArray_DESCR(ap)->elsize; - npy_intp mysize = elsize/sizeof(npy_ucs4); - int alloc = 0; - npy_ucs4 *buffer = NULL; - PyUnicodeObject *obj; - npy_intp i; - - if (!PyArray_ISBEHAVED_RO(ap)) { - buffer = malloc(elsize); - if (buffer == NULL) { - PyErr_NoMemory(); - goto fail; - } - alloc = 1; - memcpy(buffer, ip, elsize); - if (!PyArray_ISNOTSWAPPED(ap)) { - byte_swap_vector(buffer, mysize, sizeof(npy_ucs4)); - } - } - else { - buffer = (npy_ucs4 *)ip; - } - for (i = mysize; i > 0 && buffer[--i] == 0; mysize = i); + Py_ssize_t size = PyArray_ITEMSIZE(ap); + int swap = !PyArray_ISNOTSWAPPED(ap); + int align = !PyArray_ISALIGNED(ap); -#ifdef Py_UNICODE_WIDE - obj = (PyUnicodeObject *)PyUnicode_FromUnicode(buffer, mysize); -#else - /* create new empty unicode object of length mysize*2 */ - obj = (PyUnicodeObject *)MyPyUnicode_New(mysize*2); - if (obj == NULL) { - goto fail; - } - mysize = PyUCS2Buffer_FromUCS4(obj->str, buffer, mysize); - /* reset length of unicode object to ucs2size */ - if (MyPyUnicode_Resize(obj, mysize) < 0) { - Py_DECREF(obj); - goto fail; - } -#endif - - if (alloc) { - free(buffer); - } - return (PyObject *)obj; - -fail: - if (alloc) { - free(buffer); - } - return NULL; + return (PyObject *)PyUnicode_FromUCS4(ip, size, swap, align); } static int |