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

numpy-svn at scipy.org numpy-svn at scipy.org
Fri Oct 27 16:42:02 EDT 2006


Author: oliphant
Date: 2006-10-27 15:41:58 -0500 (Fri, 27 Oct 2006)
New Revision: 3407

Modified:
   trunk/numpy/core/src/arrayobject.c
   trunk/numpy/core/src/scalartypes.inc.src
Log:
More fixes to allow user-defined types in C.

Modified: trunk/numpy/core/src/arrayobject.c
===================================================================
--- trunk/numpy/core/src/arrayobject.c	2006-10-27 16:56:32 UTC (rev 3406)
+++ trunk/numpy/core/src/arrayobject.c	2006-10-27 20:41:58 UTC (rev 3407)
@@ -1316,11 +1316,11 @@
         type_num = descr->type_num;
         if (type_num == PyArray_BOOL)
                 PyArrayScalar_RETURN_BOOL_FROM_LONG(*(Bool*)data);
-        else if (type_num == PyArray_OBJECT) {
+        else if (type_num == PyArray_OBJECT ||          \
+                 ((type=descr->typeobj)==NULL)) {
                 return descr->f->getitem(data, base);
         }
         itemsize = descr->elsize;
-        type = descr->typeobj;
         copyswap = descr->f->copyswap;
         swap = !PyArray_ISNBO(descr->byteorder);
         if PyTypeNum_ISSTRING(type_num) { /* Eliminate NULL bytes */
@@ -1541,7 +1541,8 @@
 
         for (i=0; i<NPY_NUMUSERTYPES; i++) {
                 descr = userdescrs[i];
-                if (strcmp(descr->typeobj->tp_name, str) == 0)
+                if (descr->typeobj && 
+                    strcmp(descr->typeobj->tp_name, str) == 0)
                         return descr->type_num;
         }
 
@@ -1588,10 +1589,12 @@
                                 " is missing.");
                 return -1;
         }
+        /*
         if (descr->typeobj == NULL) {
                 PyErr_SetString(PyExc_ValueError, "missing typeobject");
                 return -1;
         }
+        */
         userdescrs = realloc(userdescrs,
                              (NPY_NUMUSERTYPES+1)*sizeof(void *));
         if (userdescrs == NULL) {
@@ -10576,7 +10579,7 @@
                 Py_INCREF(new->subarray->shape);
                 Py_INCREF(new->subarray->base);
         }
-        Py_INCREF(new->typeobj);
+        Py_XINCREF(new->typeobj);
         return new;
 }
 
@@ -10658,8 +10661,14 @@
         PyObject *res;
 	char *s;
         static int prefix_len=0;
-
+        
         if (PyTypeNum_ISUSERDEF(self->type_num)) {
+                if (typeobj == NULL) {
+                        return PyString_FromFormat("%s%d (%d)", 
+                                                   self->kind,
+                                                   self->elsize,
+                                                   self->type_num);
+                }
 		s = strrchr(typeobj->tp_name, '.');
 		if (s == NULL) {
 			res = PyString_FromString(typeobj->tp_name);
@@ -10883,6 +10892,10 @@
         Py_DECREF(mod);
         if (obj == NULL) {Py_DECREF(ret); return NULL;}
         PyTuple_SET_ITEM(ret, 0, obj);
+        if (self->typeobj == NULL) { /* Must handle this case in getitem */
+                obj = self->f->getitem(NULL, NULL);
+                if (obj == NULL) {Py_DECREF(ret); return NULL;}
+        }
         if (PyTypeNum_ISUSERDEF(self->type_num) ||              \
             ((self->type_num == PyArray_VOID &&                 \
               self->typeobj != &PyVoidArrType_Type))) {

Modified: trunk/numpy/core/src/scalartypes.inc.src
===================================================================
--- trunk/numpy/core/src/scalartypes.inc.src	2006-10-27 16:56:32 UTC (rev 3406)
+++ trunk/numpy/core/src/scalartypes.inc.src	2006-10-27 20:41:58 UTC (rev 3407)
@@ -32,6 +32,8 @@
 scalar_value(PyObject *scalar, PyArray_Descr *descr)
 {
         int type_num;
+        int align;
+        intp memloc;
         if (descr == NULL) {
                 descr = PyArray_DescrFromScalar(scalar);
                 type_num = descr->type_num;
@@ -111,8 +113,17 @@
         }
         else _IFCASE(Object);
         
-        PyErr_SetString(PyExc_RuntimeError, "bad scalar");
-        return NULL;
+
+        /* Use the alignment flag to figure out where the data begins
+           after a PyObject_HEAD
+        */
+        memloc = (intp)scalar;
+        memloc += sizeof(PyObject);
+        /* now round-up to the nearest alignment value 
+         */
+        align = descr->alignment;
+        if (align > 1) memloc = ((memloc + align - 1)/align)*align;
+        return (void *)memloc
 #undef _IFCASE
 #undef _OBJ
 #undef _CHK
@@ -2578,7 +2589,7 @@
                 conv->subarray = NULL;
                 Py_DECREF(conv);
         }
-        Py_DECREF(new->typeobj);
+        Py_XDECREF(new->typeobj);
         new->typeobj = (PyTypeObject *)type;
         Py_INCREF(type);
         return new;
@@ -2651,7 +2662,7 @@
 
 /* New reference */
 /*OBJECT_API
- Get a typeobject from a type-number
+ Get a typeobject from a type-number -- can return NULL.
 */
 static PyObject *
 PyArray_TypeObjectFromType(int type)
@@ -2662,7 +2673,7 @@
         descr = PyArray_DescrFromType(type);
         if (descr == NULL) return NULL;
         obj = (PyObject *)descr->typeobj;
-        Py_INCREF(obj);
+        Py_XINCREF(obj);
         Py_DECREF(descr);
         return obj;
 }




More information about the Numpy-svn mailing list