diff options
author | Travis Oliphant <oliphant@enthought.com> | 2006-06-02 01:23:31 +0000 |
---|---|---|
committer | Travis Oliphant <oliphant@enthought.com> | 2006-06-02 01:23:31 +0000 |
commit | 0293199869cce0a8dab3a589ee7b05249ff6be33 (patch) | |
tree | b5e10062f78c6fd9b15d51a3a0845f89e33ef954 /numpy | |
parent | ce75dc9f0921a81cb16fe8d79a3a5ba24efbb922 (diff) | |
download | numpy-0293199869cce0a8dab3a589ee7b05249ff6be33.tar.gz |
Fix-up handling of registering of data-types. Now it is done using a PyArray_Descr * object as it should be. Any Pyarray_Descr * object will do.
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/code_generators/array_api_order.txt | 1 | ||||
-rw-r--r-- | numpy/core/include/numpy/arrayobject.h | 2 | ||||
-rw-r--r-- | numpy/core/numeric.py | 3 | ||||
-rw-r--r-- | numpy/core/src/arrayobject.c | 91 | ||||
-rw-r--r-- | numpy/core/src/multiarraymodule.c | 21 | ||||
-rw-r--r-- | numpy/core/src/scalartypes.inc.src | 18 |
6 files changed, 21 insertions, 115 deletions
diff --git a/numpy/core/code_generators/array_api_order.txt b/numpy/core/code_generators/array_api_order.txt index d7d9b6121..2491b5ab8 100644 --- a/numpy/core/code_generators/array_api_order.txt +++ b/numpy/core/code_generators/array_api_order.txt @@ -26,7 +26,6 @@ PyArray_CastScalarToCtype PyArray_CastScalarDirect PyArray_ScalarFromObject PyArray_RegisterDataType -PyArray_RegisterDescrForType PyArray_FromDims PyArray_FromDimsAndDataAndDescr PyArray_FromAny diff --git a/numpy/core/include/numpy/arrayobject.h b/numpy/core/include/numpy/arrayobject.h index 81bdd8910..12a78918d 100644 --- a/numpy/core/include/numpy/arrayobject.h +++ b/numpy/core/include/numpy/arrayobject.h @@ -79,7 +79,7 @@ extern "C" CONFUSE_EMACS #define PY_SUCCEED 1 /* Helpful to distinguish what is installed */ -#define NDARRAY_VERSION 0x00090709 +#define NDARRAY_VERSION 0x00090901 /* Some platforms don't define bool, long long, or long double. Handle that here. diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index c99ca75a8..014a729ff 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -3,7 +3,7 @@ __all__ = ['newaxis', 'ndarray', 'flatiter', 'ufunc', 'fromstring', 'fromfile', 'frombuffer','newbuffer', 'getbuffer', 'where', 'argwhere', 'concatenate', 'fastCopyAndTranspose', 'lexsort', - 'register_dtype', 'set_numeric_ops', 'can_cast', + 'set_numeric_ops', 'can_cast', 'asarray', 'asanyarray', 'ascontiguousarray', 'asfortranarray', 'isfortran', 'empty_like', 'zeros_like', 'correlate', 'convolve', 'inner', 'dot', 'outer', 'vdot', @@ -107,7 +107,6 @@ getbuffer = multiarray.getbuffer where = multiarray.where concatenate = multiarray.concatenate fastCopyAndTranspose = multiarray._fastCopyAndTranspose -register_dtype = multiarray.register_dtype set_numeric_ops = multiarray.set_numeric_ops can_cast = multiarray.can_cast lexsort = multiarray.lexsort diff --git a/numpy/core/src/arrayobject.c b/numpy/core/src/arrayobject.c index d4b84bdfb..9fdc67ee5 100644 --- a/numpy/core/src/arrayobject.c +++ b/numpy/core/src/arrayobject.c @@ -1025,7 +1025,7 @@ PyArray_Scalar(void *data, PyArray_Descr *descr, PyObject *base) else obj = type->tp_alloc(type, 0); if (obj == NULL) return NULL; - if PyTypeNum_ISEXTENDED(type_num) { + if PyTypeNum_ISFLEXIBLE(type_num) { if (type_num == PyArray_STRING) { destptr = PyString_AS_STRING(obj); ((PyStringObject *)obj)->ob_shash = -1; @@ -1136,7 +1136,7 @@ PyArray_Scalar(void *data, PyArray_Descr *descr, PyObject *base) */ -/* Return Python scalar if 0-d array object is encountered */ +/* Return Array Scalar if 0-d array object is encountered */ /*OBJECT_API Return either an array or the appropriate Python object if the array @@ -1177,107 +1177,38 @@ PyArray_Return(PyArrayObject *mp) */ /*OBJECT_API Register Data type + Does not change the reference count of descr */ static int -PyArray_RegisterDataType(PyTypeObject *type) +PyArray_RegisterDataType(PyArray_Descr *descr) { - PyArray_Descr *descr; - PyObject *obj; + PyArray_Descr *descr2; int typenum; int i; - if ((type == &PyVoidArrType_Type) || \ - !PyType_IsSubtype(type, &PyVoidArrType_Type)) { - PyErr_SetString(PyExc_ValueError, - "can only register void subtypes"); - return -1; - } /* See if this type is already registered */ for (i=0; i<PyArray_NUMUSERTYPES; i++) { - descr = userdescrs[i]; - if (descr->typeobj == type) + descr2 = userdescrs[i]; + if (descr2 == descr) return descr->type_num; } - descr = PyArray_DescrNewFromType(PyArray_VOID); typenum = PyArray_USERDEF + PyArray_NUMUSERTYPES; descr->type_num = typenum; - descr->typeobj = type; - obj = PyObject_GetAttrString((PyObject *)type,"itemsize"); - if (obj) { - i = PyInt_AsLong(obj); - if ((i < 0) && (PyErr_Occurred())) PyErr_Clear(); - else descr->elsize = i; - Py_DECREF(obj); + if (descr->elsize == 0) { + PyErr_SetString(PyExc_ValueError, "cannot register a" \ + "flexible data-type"); + return -1; } - Py_INCREF(type); userdescrs = realloc(userdescrs, (PyArray_NUMUSERTYPES+1)*sizeof(void *)); if (userdescrs == NULL) { PyErr_SetString(PyExc_MemoryError, "RegisterDataType"); - Py_DECREF(descr); return -1; } userdescrs[PyArray_NUMUSERTYPES++] = descr; return typenum; } - -/* - copyies over from the old descr table for anything - NULL or zero in what is given. - DECREF's the Descr already there. - places a pointer to the new one into the slot. -*/ - -/* steals a reference to descr */ -/*OBJECT_API - Insert Descr Table -*/ -static int -PyArray_RegisterDescrForType(int typenum, PyArray_Descr *descr) -{ - PyArray_Descr *old; - - if (!PyTypeNum_ISUSERDEF(typenum)) { - PyErr_SetString(PyExc_TypeError, - "data type not registered"); - Py_DECREF(descr); - return -1; - } - old = userdescrs[typenum-PyArray_USERDEF]; - descr->typeobj = old->typeobj; - descr->type_num = typenum; - - if (descr->f == NULL) descr->f = old->f; - if (descr->fields == NULL) { - descr->fields = old->fields; - Py_XINCREF(descr->fields); - } - if (descr->subarray == NULL && old->subarray) { - descr->subarray = _pya_malloc(sizeof(PyArray_ArrayDescr)); - memcpy(descr->subarray, old->subarray, - sizeof(PyArray_ArrayDescr)); - Py_INCREF(descr->subarray->shape); - Py_INCREF(descr->subarray->base); - } - Py_XINCREF(descr->typeobj); - -#define _ZERO_CHECK(member) \ - if (descr->member == 0) descr->member = old->member - - _ZERO_CHECK(kind); - _ZERO_CHECK(type); - _ZERO_CHECK(byteorder); - _ZERO_CHECK(elsize); - _ZERO_CHECK(alignment); -#undef _ZERO_CHECK - - Py_DECREF(old); - userdescrs[typenum-PyArray_USERDEF] = descr; - return 0; -} - - /*OBJECT_API To File */ diff --git a/numpy/core/src/multiarraymodule.c b/numpy/core/src/multiarraymodule.c index 0a272dd20..0ad681e7e 100644 --- a/numpy/core/src/multiarraymodule.c +++ b/numpy/core/src/multiarraymodule.c @@ -5818,9 +5818,9 @@ array_where(PyObject *ignored, PyObject *args) if (!PyArg_ParseTuple(args, "O|OO", &obj, &x, &y)) return NULL; return PyArray_Where(obj, x, y); - } + static char doc_lexsort[] = \ "lexsort(keys=, axis=-1) returns an array of indices similar to argsort,\n"\ "except the sorting is done using the provided sorting keys. First the\n"\ @@ -5847,23 +5847,6 @@ array_lexsort(PyObject *ignored, PyObject *args, PyObject *kwds) #undef _ARET -static char doc_register_dtype[] = \ - "register_dtype(a) registers a new type object -- gives it a typenum"; - -static PyObject * -array_register_dtype(PyObject *dummy, PyObject *args) -{ - PyObject *dtype; - int ret; - - if (!PyArg_ParseTuple(args, "O", &dtype)) return NULL; - - ret = PyArray_RegisterDataType((PyTypeObject *)dtype); - if (ret < 0) - return NULL; - return PyInt_FromLong((long) ret); -} - static char doc_can_cast_safely[] = \ "can_cast_safely(from=d1, to=d2) returns True if data type d1 "\ "can be cast to data type d2 without losing precision."; @@ -6002,8 +5985,6 @@ static struct PyMethodDef array_module_methods[] = { METH_VARARGS | METH_KEYWORDS, doc_frombuffer}, {"fromfile", (PyCFunction)array_fromfile, METH_VARARGS | METH_KEYWORDS, doc_fromfile}, - {"register_dtype", (PyCFunction)array_register_dtype, - METH_VARARGS, doc_register_dtype}, {"can_cast", (PyCFunction)array_can_cast_safely, METH_VARARGS | METH_KEYWORDS, doc_can_cast_safely}, {"newbuffer", (PyCFunction)new_buffer, diff --git a/numpy/core/src/scalartypes.inc.src b/numpy/core/src/scalartypes.inc.src index 50687ab9f..b4622bb32 100644 --- a/numpy/core/src/scalartypes.inc.src +++ b/numpy/core/src/scalartypes.inc.src @@ -4,9 +4,8 @@ #define _MULTIARRAYMODULE #endif #include "numpy/arrayscalars.h" -static int PyArrayScalar_Offset[PyArray_NTYPES+1]; -#define _SOFFSET_(obj, type_num) ((char *)(obj) + PyArrayScalar_Offset[(type_num)]) +#define _SOFFSET_(obj,y) ((char *)obj + (offsetof(PyScalarObject, obval))) static PyBoolScalarObject _PyArrayScalar_BoolValues[2] = { {PyObject_HEAD_INIT(&PyBoolArrType_Type) 0}, @@ -2268,13 +2267,6 @@ ComplexFloating, Flexible, Character# PyArrayIter_Type.tp_iter = PyObject_SelfIter; PyArrayMapIter_Type.tp_iter = PyObject_SelfIter; - -/**begin repeat -#name=Bool, Byte, Short, Int, Long, LongLong, UByte, UShort, UInt, ULong, ULongLong, Float, Double, LongDouble, CFloat, CDouble, CLongDouble, Object,# -#num=BOOL, BYTE, SHORT, INT, LONG, LONGLONG, UBYTE, USHORT, UINT, ULONG, ULONGLONG, FLOAT, DOUBLE, LONGDOUBLE, CFLOAT, CDOUBLE, CLONGDOUBLE, OBJECT, NTYPES# -**/ - PyArrayScalar_Offset[PyArray_@num@] = (int) offsetof(Py@name@ScalarObject, obval); -/**end repeat**/ } @@ -2345,7 +2337,6 @@ PyArray_DescrFromTypeObject(PyObject *type) typenum = _typenum_fromtypeobj(type,1); if (typenum != PyArray_NOTYPE) { new = PyArray_DescrFromType(typenum); - if (PyTypeNum_ISUSERDEF(typenum)) goto finish; return new; } @@ -2375,9 +2366,14 @@ PyArray_DescrFromTypeObject(PyObject *type) currently only VOID allows it -- use it as the type-object. */ /* look for a dtypedescr attribute */ + if (!PyType_IsSubtype((PyTypeObject *)type, &PyVoidArrType_Type)) { + PyErr_SetString(PyExc_TypeError, + "data-type cannot be detemrined from " \ + "type-object"); + return NULL; + } new = PyArray_DescrNewFromType(PyArray_VOID); - finish: conv = _arraydescr_fromobj(type); if (conv) { new->fields = conv->fields; |