[Numpy-svn] r3800 - trunk/numpy/core/src
numpy-svn at scipy.org
numpy-svn at scipy.org
Tue May 22 18:36:13 EDT 2007
Author: oliphant
Date: 2007-05-22 17:36:10 -0500 (Tue, 22 May 2007)
New Revision: 3800
Modified:
trunk/numpy/core/src/arrayobject.c
trunk/numpy/core/src/scalartypes.inc.src
Log:
Add a few more checks to make sure that numpy unicode scalars report correctly on narrow builds. Fix a long-standing seg-fault that arose when calling u.imag on an object with numpy.unicode_ type.
Modified: trunk/numpy/core/src/arrayobject.c
===================================================================
--- trunk/numpy/core/src/arrayobject.c 2007-05-22 09:18:38 UTC (rev 3799)
+++ trunk/numpy/core/src/arrayobject.c 2007-05-22 22:36:10 UTC (rev 3800)
@@ -1378,7 +1378,7 @@
byte_swap_vector(destptr, length, 4);
#else
/* need aligned data buffer */
- if (!PyArray_ISBEHAVED(base)) {
+ if ((((intp)data) % descr->alignment) != 0) {
buffer = _pya_malloc(itemsize);
if (buffer == NULL)
return PyErr_NoMemory();
Modified: trunk/numpy/core/src/scalartypes.inc.src
===================================================================
--- trunk/numpy/core/src/scalartypes.inc.src 2007-05-22 09:18:38 UTC (rev 3799)
+++ trunk/numpy/core/src/scalartypes.inc.src 2007-05-22 22:36:10 UTC (rev 3800)
@@ -158,6 +158,9 @@
where only a reference for flexible types is returned
*/
+/* This may not work right on narrow builds for NumPy unicode scalars.
+ */
+
/*OBJECT_API
Cast Scalar to c-type
*/
@@ -752,13 +755,7 @@
static PyObject *
gentype_data_get(PyObject *self)
{
- PyArray_Descr *typecode;
- PyObject *ret;
-
- typecode = PyArray_DescrFromScalar(self);
- ret = PyBuffer_FromObject(self, 0, typecode->elsize);
- Py_DECREF(typecode);
- return ret;
+ return PyBuffer_FromObject(self, 0, Py_END_OF_BUFFER);
}
@@ -767,9 +764,16 @@
{
PyArray_Descr *typecode;
PyObject *ret;
+ int elsize;
typecode = PyArray_DescrFromScalar(self);
- ret = PyInt_FromLong((long) typecode->elsize);
+ elsize = typecode->elsize;
+#ifndef Py_UNICODE_WIDE
+ if (typecode->type_num == NPY_UNICODE) {
+ elsize >>= 1;
+ }
+#endif
+ ret = PyInt_FromLong((long) elsize);
Py_DECREF(typecode);
return ret;
}
@@ -928,9 +932,11 @@
}
else {
char *temp;
+ int elsize;
typecode = PyArray_DescrFromScalar(self);
- temp = PyDataMem_NEW(typecode->elsize);
- memset(temp, '\0', typecode->elsize);
+ elsize = typecode->elsize;
+ temp = PyDataMem_NEW(elsize);
+ memset(temp, '\0', elsize);
ret = PyArray_Scalar(temp, typecode, NULL);
PyDataMem_FREE(temp);
}
@@ -1633,6 +1639,11 @@
numbytes = outcode->elsize;
*ptrptr = (void *)scalar_value(self, outcode);
+#ifndef Py_UNICODE_WIDE
+ if (outcode->type_num == NPY_UNICODE) {
+ numbytes >>= 1;
+ }
+#endif
Py_DECREF(outcode);
return numbytes;
}
@@ -1643,8 +1654,14 @@
PyArray_Descr *outcode;
outcode = PyArray_DescrFromScalar(self);
- if (lenp)
+ if (lenp) {
*lenp = outcode->elsize;
+#ifndef Py_UNICODE_WIDE
+ if (outcode->type_num == NPY_UNICODE) {
+ *lenp >>= 1;
+ }
+#endif
+ }
Py_DECREF(outcode);
return 1;
}
More information about the Numpy-svn
mailing list