summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTravis Oliphant <oliphant@enthought.com>2005-12-06 01:43:36 +0000
committerTravis Oliphant <oliphant@enthought.com>2005-12-06 01:43:36 +0000
commit4a7ce99355e8a7860f9782a8f0900234c7fa96ee (patch)
treec4194cb21d4d2e68d5ad6b0fcf78cbe1259c138b
parentbcd661e1bd0d478b22341079c7e5fb60c9353cfc (diff)
downloadnumpy-4a7ce99355e8a7860f9782a8f0900234c7fa96ee.tar.gz
Fixed typo in cfuncs.py
-rw-r--r--scipy/base/matrix.py2
-rw-r--r--scipy/base/src/arrayobject.c17
-rw-r--r--scipy/base/src/multiarraymodule.c70
-rw-r--r--scipy/base/src/scalartypes.inc.src6
-rw-r--r--scipy/doc/records.txt30
-rw-r--r--scipy/f2py/cfuncs.py2
6 files changed, 72 insertions, 55 deletions
diff --git a/scipy/base/matrix.py b/scipy/base/matrix.py
index fe5528214..01f817f8a 100644
--- a/scipy/base/matrix.py
+++ b/scipy/base/matrix.py
@@ -88,7 +88,7 @@ class matrix(N.ndarray):
if not (fortran or arr.flags.contiguous):
arr = arr.copy()
- ret = N.ndarray.__new__(subtype, shape, arr.descr,
+ ret = N.ndarray.__new__(subtype, shape, arr.dtypedescr,
buffer=arr,
fortran=fortran,
swap=arr.flags.swapped)
diff --git a/scipy/base/src/arrayobject.c b/scipy/base/src/arrayobject.c
index bbcd99a6b..4906ac0c7 100644
--- a/scipy/base/src/arrayobject.c
+++ b/scipy/base/src/arrayobject.c
@@ -51,7 +51,6 @@ PyArray_PyIntAsIntp(PyObject *o)
}
else if (PyArray_IsScalar(o, Integer)) {
arr = PyArray_FromScalar(o, descr);
- Py_DECREF(descr);
}
if (arr != NULL) {
ret = *((intp *)PyArray_DATA(arr));
@@ -124,7 +123,6 @@ PyArray_PyIntAsInt(PyObject *o)
}
if (PyArray_IsScalar(o, Integer)) {
arr = PyArray_FromScalar(o, descr);
- Py_DECREF(descr);
}
if (arr != NULL) {
ret = *((int *)PyArray_DATA(arr));
@@ -4219,16 +4217,7 @@ array_descr_set(PyArrayObject *self, PyObject *arg)
temp->descr = NULL;
Py_DECREF(temp);
}
-
- if ((self->descr->elsize == newtype->elsize) && newtype->fields) {
- PyArray_Descr *_thetype;
- _thetype = PyArray_DescrNew(self->descr);
- Py_XDECREF(_thetype->fields);
- Py_INCREF(newtype->fields);
- _thetype->fields = newtype->fields;
- Py_DECREF(newtype);
- newtype = _thetype;
- }
+
self->descr = newtype;
PyArray_UpdateFlags(self, UPDATE_ALL_FLAGS);
@@ -6597,8 +6586,10 @@ iter_ass_subscript(PyArrayIterObject *self, PyObject *ind, PyObject *val)
/* convert to INTP array if Integer array scalar or List */
indtype = PyArray_DescrFromType(PyArray_INTP);
- if (PyArray_IsScalar(ind, Integer))
+ if (PyArray_IsScalar(ind, Integer)) {
+ Py_INCREF(indtype);
obj = PyArray_FromScalar(ind, indtype);
+ }
else if (PyList_Check(ind)) {
Py_INCREF(indtype);
obj = PyArray_FromAny(ind, indtype, 0, 0, FORCECAST);
diff --git a/scipy/base/src/multiarraymodule.c b/scipy/base/src/multiarraymodule.c
index d8fd4bedf..70bc4e3c0 100644
--- a/scipy/base/src/multiarraymodule.c
+++ b/scipy/base/src/multiarraymodule.c
@@ -2693,39 +2693,80 @@ PyArray_IntpConverter(PyObject *obj, PyArray_Dims *seq)
}
+/* A tuple type would be either (generic typeobject, typesize)
+ or (fixed-length data-type, shape)
+ or (inheriting data-type, new-data-type)
+ The new data-type must have the same itemsize as the inheriting data-type
+ unless the latter is 0 in the final case.
+
+ Thus (int32, {'real':(int16,0),'imag',(int16,2)})
-
-/* A tuple type would be either (generic typeobject, typesize)
- or (fixed-length, data-type, shape)
- to indicate an array of a fixed-type.
+ is one way to specify a descriptor that will give
+ a['real'] and a['imag'] to an int32 array.
*/
+/* steal type reference */
+static PyArray_Descr *
+_use_inherit(PyArray_Descr *type, PyObject *newobj)
+{
+ PyArray_Descr *new;
+ PyArray_Descr *conv;
+
+ if (!PyArray_DescrConverter(newobj, &conv)) {
+ Py_DECREF(type);
+ return NULL;
+ }
+ new = PyArray_DescrNew(type);
+ Py_DECREF(type);
+ if (new == NULL) return NULL;
+
+ if (new->elsize && new->elsize != conv->elsize) {
+ PyErr_SetString(PyExc_ValueError,
+ "mismatch in size of old"\
+ "and new data-descriptor");
+ return NULL;
+ }
+
+ new->fields = conv->fields;
+ Py_INCREF(new->fields);
+ Py_DECREF(conv);
+ return new;
+}
+
static PyArray_Descr *
_convert_from_tuple(PyObject *obj)
{
PyArray_Descr *type;
+ PyObject *val;
if (PyTuple_GET_SIZE(obj) < 2) return NULL;
+
if (!PyArray_DescrConverter(PyTuple_GET_ITEM(obj,0), &type))
return NULL;
+ val = PyTuple_GET_ITEM(obj,1);
if (type->elsize == 0) { /* interpret next item as a typesize */
int itemsize;
itemsize = PyArray_PyIntAsInt(PyTuple_GET_ITEM(obj,1));
- if (error_converting(itemsize)) goto fail;
+ if (error_converting(itemsize)) {
+ PyErr_Clear();
+ return _use_inherit(type, val);
+ }
PyArray_DESCR_REPLACE(type);
type->elsize = itemsize;
}
- else { /* interpret next item as shape
- and reset the type to PyArray_VOID with
- a new fields attribute.
+ else {
+ /* interpret next item as shape
+ and reset the type to PyArray_VOID with
+ anew fields attribute.
*/
PyArray_Dims shape;
PyArray_Descr *newdescr;
- PyObject *val;
- val = PyTuple_GET_ITEM(obj,1);
if ((PyArray_IntpConverter(val, &shape) < 0) || \
- (shape.len > MAX_DIMS)) goto fail;
+ (shape.len > MAX_DIMS)) {
+ PyErr_Clear();
+ return _use_inherit(type, val);
+ }
newdescr = PyArray_DescrNew(type);
if (newdescr == NULL) {PyDimMem_FREE(shape.ptr); goto fail;}
newdescr->elsize *= PyArray_MultiplyList(shape.ptr,
@@ -2749,8 +2790,7 @@ _convert_from_tuple(PyObject *obj)
/* a dictionary specifying a data-type
must have at least two and up to four
- keys
- These must all be sequences of the same length.
+ keys These must all be sequences of the same length.
"names" --- field names
"formats" --- the data-type descriptors for the field.
@@ -2764,7 +2804,7 @@ _convert_from_tuple(PyObject *obj)
"titles" --- Allows the use of an additional key
for the fields dictionary.
-
+
Attribute-lookup-based field names merely has to query the fields
dictionary of the data-descriptor. Any result present can be used
to return the correct field.
@@ -2801,7 +2841,7 @@ _convert_from_dict(PyObject *obj)
PyObject *names, *offsets, *descrs, *titles;
int n, i;
int totalsize;
-
+
fields = PyDict_New();
if (fields == NULL) return (PyArray_Descr *)PyErr_NoMemory();
diff --git a/scipy/base/src/scalartypes.inc.src b/scipy/base/src/scalartypes.inc.src
index 2fdec9b84..a437bc486 100644
--- a/scipy/base/src/scalartypes.inc.src
+++ b/scipy/base/src/scalartypes.inc.src
@@ -110,7 +110,7 @@ PyArray_CastScalarToCtype(PyObject *scalar, void *ctypeptr,
/* 0-dim array from array-scalar object */
/* always contains a copy of the data */
-/* does nothing with outcode */
+/* steals reference to outcode */
static PyObject *
PyArray_FromScalar(PyObject *scalar, PyArray_Descr *outcode)
{
@@ -125,7 +125,7 @@ PyArray_FromScalar(PyObject *scalar, PyArray_Descr *outcode)
typecode,
0, NULL,
NULL, NULL, 0, NULL);
- if (r==NULL) return NULL;
+ if (r==NULL) {Py_XDECREF(outcode); return NULL;}
switch(typecode->type_num) {
case PyArray_STRING:
@@ -159,7 +159,6 @@ PyArray_FromScalar(PyObject *scalar, PyArray_Descr *outcode)
}
/* cast if necessary to desired output typecode */
- Py_INCREF(outcode);
ret = PyArray_CastToType((PyArrayObject *)r, outcode, 0);
Py_DECREF(r);
return ret;
@@ -953,7 +952,6 @@ gentype_getarray(PyObject *scalar, PyObject *args)
if (!PyArg_ParseTuple(args, "|O&", &PyArray_DescrConverter,
&outcode)) return NULL;
ret = PyArray_FromScalar(scalar, outcode);
- Py_DECREF(outcode);
return ret;
}
diff --git a/scipy/doc/records.txt b/scipy/doc/records.txt
index 7c8f299c8..75641359e 100644
--- a/scipy/doc/records.txt
+++ b/scipy/doc/records.txt
@@ -15,19 +15,10 @@ be created since it needs a specific element size.
2) An additional PyObject * member of the PyArray_Descr structure for
describing the fields. This is Py_NotImplemented for statically defined types.
For dynamically defined types it is NULL or a dictionary with keys that
-return ordered lists. There must be the same number of items in each list.
-
-'names' : new attribute names (note that these will never
- be able to over-ride "real" method and attribute names --
- those will be found first).
-
-'titles' : labels for the fields (can be used alone in index-style lookups) in
- which case a reference to the field is returned. (optional)
-
-'formats' : the actual parameters to pass to GetField to retrieve the
- specific field. This parameter is a (data-descr, offset) pair.
- The data-descr must be a PyArray_Descr * object and offset
- is an integer.
+return ordered lists. This dictionary is a mapping between keys and
+a tuple (PyArray_Descr object, offset, [optional title]). Notice that there
+may be more than one key returning the same item, but the title is always the
+same.
Doing this allows us to remove the self->itemsize from the array
structure (I think that's a good thing because the size is an
@@ -35,8 +26,8 @@ important part of the data-type description).
3) An additional PyArray_ArrayDescr * member of the PyArray_Descr
structure for supporting types that are themselves arrays of other
- types. The PyArray_ArrayDescr structure has members PyArray_Descr *,
- int, and intp[MAX_DIMS] for holding a referencing to the base-type and
+ types. The PyArray_ArrayDescr structure has members PyArray_Descr *,
+ PyObject *, for holding a reference to the base-type and
the shape of the sub-array.
For now, this PyArray_ArrayDescr * member is only really used to get a
@@ -45,7 +36,7 @@ with this structure set are not really useful.
However, placing it here completes the notion of a type descriptor
and simplies the formats key so that all supported type description
-is handled by the PyArray_Descr structure. Adde a byte-order type to
+is handled by the PyArray_Descr structure. Add a byte-order type to
descriptor as well so that entire array_descr in protocol can be supported
by a PyArray_Descr structure.
@@ -91,12 +82,9 @@ converted to a "proper" descr->fields entry (all generic data-types
converted to PyArray_Descr * structure).
-
Reference counting for PyArray_Descr * objects.
-Most functions that take PyArary_Descr * as arguments steal the reference
-unless otherwise noted.
-
-Exceptions: PyArray_Scalar,
+Most functions that take PyArary_Descr * as arguments and return a PyObject *
+steal the reference unless otherwise noted in the code.
Functions that return PyArray_Descr * objects return a new reference.
diff --git a/scipy/f2py/cfuncs.py b/scipy/f2py/cfuncs.py
index 19508fe14..f68c93772 100644
--- a/scipy/f2py/cfuncs.py
+++ b/scipy/f2py/cfuncs.py
@@ -869,7 +869,7 @@ static int complex_double_from_pyobj(complex_double* v,PyObject *obj,const char
\t\t\tarr = PyArray_Cast((PyArrayObject *)obj, PyArray_CDOUBLE);
\t\t}
\t\telse {
-\t\t\tarr = PyArray_FromScalar(obj, PyArray_DescrFromType(PyArray_CDOUBLE)));
+\t\t\tarr = PyArray_FromScalar(obj, PyArray_DescrFromType(PyArray_CDOUBLE));
\t\t}
\t\tif (arr==NULL) return 0;
\t\t(*v).r = ((cdouble *)PyArray_DATA(arr))->real;