diff options
author | Travis Oliphant <oliphant@enthought.com> | 2006-08-17 01:01:14 +0000 |
---|---|---|
committer | Travis Oliphant <oliphant@enthought.com> | 2006-08-17 01:01:14 +0000 |
commit | 9e244f2d5ab9b2a92a46bf8d41daed3258c0f7fb (patch) | |
tree | 72284c77f6fcb294033c1498e46d8e3dbcd15115 /numpy | |
parent | bd9dc9ec6a44ad339e6ad8ac7e0b849bcbb496b7 (diff) | |
download | numpy-9e244f2d5ab9b2a92a46bf8d41daed3258c0f7fb.tar.gz |
Fix ambiguity of list data-type objects. Lists are always interpeted as array_descriptor format. Use a comma-separated string for the other approach. Add 'int', 'float', etc. to the sctypeDict so they can be interpreted in strings.
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/_internal.py | 3 | ||||
-rw-r--r-- | numpy/core/numerictypes.py | 13 | ||||
-rw-r--r-- | numpy/core/records.py | 6 | ||||
-rw-r--r-- | numpy/core/src/arrayobject.c | 43 | ||||
-rw-r--r-- | numpy/core/src/multiarraymodule.c | 22 |
5 files changed, 35 insertions, 52 deletions
diff --git a/numpy/core/_internal.py b/numpy/core/_internal.py index d538743b6..e650be9a0 100644 --- a/numpy/core/_internal.py +++ b/numpy/core/_internal.py @@ -120,7 +120,8 @@ def _split(input): newlist = [] hold = '' - for element in input.split(','): + listinput = input.split(',') + for element in listinput: if hold != '': item = hold + ',' + element else: diff --git a/numpy/core/numerictypes.py b/numpy/core/numerictypes.py index 396c00de8..5ffb79045 100644 --- a/numpy/core/numerictypes.py +++ b/numpy/core/numerictypes.py @@ -433,6 +433,19 @@ for key, val in _typestr.items(): if val not in sctypeDict: sctypeDict[val] = key +# Add additional strings to the sctypeDict + +_toadd = ['int', 'float', 'complex', 'bool', 'object', 'string', ('str', allTypes['string_']), + 'unicode', 'object'] + +for name in _toadd: + if isinstance(name, tuple): + sctypeDict[name[0]] = name[1] + else: + sctypeDict[name] = allTypes['%s_' % name] + +del _toadd, name + # Now add the types we've determined to this module for key in allTypes: globals()[key] = allTypes[key] diff --git a/numpy/core/records.py b/numpy/core/records.py index cf8e94425..9f626fc4b 100644 --- a/numpy/core/records.py +++ b/numpy/core/records.py @@ -50,10 +50,14 @@ class format_parser: if formats is None: raise ValueError, "Need formats argument" + if isinstance(formats, list): + if len(formats) < 2: + formats.append('') + formats = ','.join(formats) dtype = sb.dtype(formats, aligned) fields = dtype.fields if fields is None: - dtype = sb.dtype([formats], aligned) + dtype = sb.dtype([('f1', dtype)], aligned) fields = dtype.fields keys = dtype.names self._f_formats = [fields[key][0] for key in keys] diff --git a/numpy/core/src/arrayobject.c b/numpy/core/src/arrayobject.c index 4aae2cdd8..03b37326a 100644 --- a/numpy/core/src/arrayobject.c +++ b/numpy/core/src/arrayobject.c @@ -10596,7 +10596,6 @@ static PyGetSetDef arraydescr_getsets[] = { {NULL, NULL, NULL, NULL}, }; -static PyArray_Descr *_convert_from_list(PyObject *obj, int align, int try_descr); static PyArray_Descr *_convert_from_dict(PyObject *obj, int align); static PyArray_Descr *_convert_from_commastring(PyObject *obj, int align); static PyArray_Descr *_convert_from_array_descr(PyObject *obj); @@ -10620,33 +10619,12 @@ arraydescr_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds) if PyDict_Check(odescr) conv = _convert_from_dict(odescr, 1); else if PyList_Check(odescr) { - conv = _convert_from_list(odescr, 1, 0); - if (conv == NULL) { - /* There is an errror. Possibly it's - because we have an array_descriptor. - Try converting from an array_descriptor. - If that fails then raise the old error. - */ - PyObject *type, *value, *traceback; - PyArray_Descr *temp; - PyErr_Fetch(&type, &value, &traceback); - temp = _convert_from_array_descr(odescr); - if (!PyErr_Occurred()) { - Py_DECREF(temp); - Py_XDECREF(type); - Py_XDECREF(value); - Py_XDECREF(traceback); - PyErr_SetString(PyExc_ValueError, - "align cannot be True" \ - " with array_descriptor " \ - "specification."); - } - else { - PyErr_Restore(type, value, traceback); - } - return NULL; - } - } + PyErr_SetString(PyExc_ValueError, + "align cannot be True" \ + " with array_descriptor " \ + "specification."); + return NULL; + } else if PyString_Check(odescr) conv = _convert_from_commastring(odescr, 1); else { @@ -10662,15 +10640,6 @@ arraydescr_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds) return NULL; } - if PyList_Check(odescr) { - conv = _convert_from_array_descr(odescr); - if (!conv) { - PyErr_Clear(); - conv = _convert_from_list(odescr, 0, 0); - } - return (PyObject *)conv; - } - if (!PyArray_DescrConverter(odescr, &conv)) return NULL; /* Get a new copy of it unless it's already a copy */ diff --git a/numpy/core/src/multiarraymodule.c b/numpy/core/src/multiarraymodule.c index 2c4dc41b8..72bdd10db 100644 --- a/numpy/core/src/multiarraymodule.c +++ b/numpy/core/src/multiarraymodule.c @@ -4246,7 +4246,7 @@ _convert_from_array_descr(PyObject *obj) */ static PyArray_Descr * -_convert_from_list(PyObject *obj, int align, int try_descr) +_convert_from_list(PyObject *obj, int align) { int n, i; int totalsize; @@ -4260,6 +4260,11 @@ _convert_from_list(PyObject *obj, int align, int try_descr) int hasobject=0; n = PyList_GET_SIZE(obj); + /* Ignore any empty string at end which _internal._commastring + can produce */ + key = PyList_GET_ITEM(obj, n-1); + if (PyString_Check(key) && PyString_GET_SIZE(key) == 0) n = n-1; + /* End ignore code.*/ totalsize = 0; if (n==0) return NULL; nameslist = PyTuple_New(n); @@ -4304,16 +4309,7 @@ _convert_from_list(PyObject *obj, int align, int try_descr) fail: Py_DECREF(nameslist); Py_DECREF(fields); - if (!try_descr) return NULL; - if (align) { - PyErr_SetString(PyExc_ValueError, - "failed to convert from list of formats "\ - "and align cannot be 1 for conversion from "\ - "array_descr structure"); - return NULL; - } - PyErr_Clear(); - return _convert_from_array_descr(obj); + return NULL; } @@ -4348,7 +4344,7 @@ _convert_from_commastring(PyObject *obj, int align) } } else { - res = _convert_from_list(listobj, align, 0); + res = _convert_from_list(listobj, align); } Py_DECREF(listobj); if (!res && !PyErr_Occurred()) { @@ -4716,7 +4712,7 @@ PyArray_DescrConverter(PyObject *obj, PyArray_Descr **at) } /* or a list */ else if (PyList_Check(obj)) { - *at = _convert_from_list(obj,0,1); + *at = _convert_from_array_descr(obj); if (*at == NULL) { if (PyErr_Occurred()) return PY_FAIL; goto fail; |