diff options
author | Robert Kern <robert.kern@gmail.com> | 2010-05-24 17:04:09 +0000 |
---|---|---|
committer | Robert Kern <robert.kern@gmail.com> | 2010-05-24 17:04:09 +0000 |
commit | 1512b58b77280e35f0fdf1df79cc131042941f53 (patch) | |
tree | d6dc1d89f93ca0c0d289f41666a7741129df07db /numpy | |
parent | 5ab03fb56e40dd5ec20535297d1c2602e8ad17a1 (diff) | |
download | numpy-1512b58b77280e35f0fdf1df79cc131042941f53.tar.gz |
BUG: Add some guards for bad inputs to the dtype constructor.
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/src/multiarray/descriptor.c | 9 | ||||
-rw-r--r-- | numpy/core/tests/test_dtype.py | 9 |
2 files changed, 18 insertions, 0 deletions
diff --git a/numpy/core/src/multiarray/descriptor.c b/numpy/core/src/multiarray/descriptor.c index 0ba1a9c04..1784e2102 100644 --- a/numpy/core/src/multiarray/descriptor.c +++ b/numpy/core/src/multiarray/descriptor.c @@ -1010,6 +1010,9 @@ _convert_from_dict(PyObject *obj, int align) } tup = PyTuple_New(len); descr = PyObject_GetItem(descrs, index); + if (!descr) { + goto fail; + } ret = PyArray_DescrConverter(descr, &newdescr); Py_DECREF(descr); if (ret == PY_FAIL) { @@ -1025,6 +1028,9 @@ _convert_from_dict(PyObject *obj, int align) if (offsets) { long offset; off = PyObject_GetItem(offsets, index); + if (!off) { + goto fail; + } offset = PyInt_AsLong(off); PyTuple_SET_ITEM(tup, 1, off); if (offset < totalsize) { @@ -1046,6 +1052,9 @@ _convert_from_dict(PyObject *obj, int align) PyTuple_SET_ITEM(tup, 2, item); } name = PyObject_GetItem(names, index); + if (!name) { + goto fail; + } Py_DECREF(index); #if defined(NPY_PY3K) if (!PyUString_Check(name)) { diff --git a/numpy/core/tests/test_dtype.py b/numpy/core/tests/test_dtype.py index 02fec7242..e817ce5d4 100644 --- a/numpy/core/tests/test_dtype.py +++ b/numpy/core/tests/test_dtype.py @@ -33,6 +33,15 @@ class TestRecord(TestCase): self.assertTrue(hash(a) != hash(b), "%s and %s hash the same !" % (a, b)) + def test_not_lists(self): + """Test if an appropriate exception is raised when passing bad values to + the dtype constructor. + """ + self.assertRaises(TypeError, np.dtype, + dict(names=set(['A', 'B']), formats=['f8', 'i4'])) + self.assertRaises(TypeError, np.dtype, + dict(names=['A', 'B'], formats=set(['f8', 'i4']))) + class TestSubarray(TestCase): def test_single_subarray(self): a = np.dtype((np.int, (2))) |