[Numpy-svn] r4313 - trunk/numpy/core/src

numpy-svn@scip... numpy-svn@scip...
Fri Oct 26 16:26:27 CDT 2007


Author: oliphant
Date: 2007-10-26 16:26:22 -0500 (Fri, 26 Oct 2007)
New Revision: 4313

Modified:
   trunk/numpy/core/src/multiarraymodule.c
   trunk/numpy/core/src/scalartypes.inc.src
   trunk/numpy/core/src/ucsnarrow.c
Log:
Fix pickling of numpy.unicode_ objects on narrow builds of Python.

Modified: trunk/numpy/core/src/multiarraymodule.c
===================================================================
--- trunk/numpy/core/src/multiarraymodule.c	2007-10-26 13:19:22 UTC (rev 4312)
+++ trunk/numpy/core/src/multiarraymodule.c	2007-10-26 21:26:22 UTC (rev 4313)
@@ -5710,6 +5710,9 @@
     return ret;
 }
 
+/* This function is needed for supporting Pickles of 
+   numpy scalar objects.
+*/
 static PyObject *
 array_scalar(PyObject *ignored, PyObject *args, PyObject *kwds)
 {

Modified: trunk/numpy/core/src/scalartypes.inc.src
===================================================================
--- trunk/numpy/core/src/scalartypes.inc.src	2007-10-26 13:19:22 UTC (rev 4312)
+++ trunk/numpy/core/src/scalartypes.inc.src	2007-10-26 21:26:22 UTC (rev 4313)
@@ -1271,9 +1271,41 @@
                                  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;
 }

Modified: trunk/numpy/core/src/ucsnarrow.c
===================================================================
--- trunk/numpy/core/src/ucsnarrow.c	2007-10-26 13:19:22 UTC (rev 4312)
+++ trunk/numpy/core/src/ucsnarrow.c	2007-10-26 21:26:22 UTC (rev 4313)
@@ -39,7 +39,7 @@
    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.
 */



More information about the Numpy-svn mailing list