From 02eca61455c70e9656e52428469a270fded5303c Mon Sep 17 00:00:00 2001 From: Travis Oliphant Date: Fri, 26 Oct 2007 21:26:22 +0000 Subject: Fix pickling of numpy.unicode_ objects on narrow builds of Python. --- numpy/core/src/multiarraymodule.c | 3 +++ numpy/core/src/scalartypes.inc.src | 32 ++++++++++++++++++++++++++++++++ numpy/core/src/ucsnarrow.c | 2 +- 3 files changed, 36 insertions(+), 1 deletion(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarraymodule.c b/numpy/core/src/multiarraymodule.c index f6e9ddd32..c150b3a10 100644 --- a/numpy/core/src/multiarraymodule.c +++ b/numpy/core/src/multiarraymodule.c @@ -5710,6 +5710,9 @@ array_empty(PyObject *ignored, PyObject *args, PyObject *kwds) return ret; } +/* This function is needed for supporting Pickles of + numpy scalar objects. +*/ static PyObject * array_scalar(PyObject *ignored, PyObject *args, PyObject *kwds) { diff --git a/numpy/core/src/scalartypes.inc.src b/numpy/core/src/scalartypes.inc.src index acf2097d1..27d37a1a5 100644 --- a/numpy/core/src/scalartypes.inc.src +++ b/numpy/core/src/scalartypes.inc.src @@ -1271,9 +1271,41 @@ gentype_reduce(PyObject *self, PyObject *args) Py_BuildValue("NO", obj, mod)); } else { +#ifndef Py_UNICODE_WIDE + /* We need to expand the buffer so that we always write + UCS4 to disk for pickle of unicode scalars. + + This could be in a unicode_reduce function, but + that would require re-factoring. + */ + int alloc=0; + char *tmp; + int newlen; + + if (PyArray_IsScalar(self, Unicode)) { + tmp = _pya_malloc(buflen*2); + if (tmp == NULL) { + Py_DECREF(ret); + return Py_NoMemory(); + } + alloc = 1; + newlen = PyUCS2Buffer_AsUCS4((Py_UNICODE *)buffer, + (PyArray_UCS4 *)tmp, + buflen / 2, buflen / 2); + buflen = newlen*4; + buffer = tmp; + } +#endif mod = PyString_FromStringAndSize(buffer, buflen); + if (mod == NULL) { + Py_DECREF(ret); + return Py_NoMemory(); + } PyTuple_SET_ITEM(ret, 1, Py_BuildValue("NN", obj, mod)); +#ifndef Py_UNICODE_WIDE + if (alloc) _pya_free(buffer); +#endif } return ret; } diff --git a/numpy/core/src/ucsnarrow.c b/numpy/core/src/ucsnarrow.c index 2d6fec082..9c4a45e9e 100644 --- a/numpy/core/src/ucsnarrow.c +++ b/numpy/core/src/ucsnarrow.c @@ -39,7 +39,7 @@ PyUCS2Buffer_FromUCS4(Py_UNICODE *ucs2, PyArray_UCS4 *ucs4, int ucs4length) It converts up to ucs4len characters of UCS2 It returns the number of characters converted which can - be less than ucslen if there are surrogate pairs in ucs2. + be less than ucs2len if there are surrogate pairs in ucs2. The return value is the actual size of the used part of the ucs4 buffer. */ -- cgit v1.2.1