diff options
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/src/multiarray/descriptor.c | 10 | ||||
-rw-r--r-- | numpy/core/tests/test_multiarray.py | 16 |
2 files changed, 24 insertions, 2 deletions
diff --git a/numpy/core/src/multiarray/descriptor.c b/numpy/core/src/multiarray/descriptor.c index d02590185..05397228e 100644 --- a/numpy/core/src/multiarray/descriptor.c +++ b/numpy/core/src/multiarray/descriptor.c @@ -509,8 +509,14 @@ _convert_from_array_descr(PyObject *obj, int align) && (PyUString_Check(title) || PyUnicode_Check(title)) #endif && (PyDict_GetItem(fields, title) != NULL))) { - PyErr_SetString(PyExc_ValueError, - "two fields with the same name"); +#if defined(NPY_PY3K) + name = PyUnicode_AsUTF8String(name); +#endif + PyErr_Format(PyExc_ValueError, + "field '%s' occurs more than once", PyString_AsString(name)); +#if defined(NPY_PY3K) + Py_DECREF(name); +#endif goto fail; } dtypeflags |= (conv->flags & NPY_FROM_FIELDS); diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 3cab3cf3e..4ef419e96 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -3554,6 +3554,13 @@ class TestRecord(TestCase): dt.names = ['p', 'q'] assert_equal(dt.names, ['p', 'q']) + def test_multiple_field_name_occurrence(self): + def test_assign(): + dtype = np.dtype([("A", "f8"), ("B", "f8"), ("A", "f8")]) + + # Error raised when multiple fields have the same name + assert_raises(ValueError, test_assign) + if sys.version_info[0] >= 3: def test_bytes_fields(self): # Bytes are not allowed in field names and not recognized in titles @@ -3570,6 +3577,15 @@ class TestRecord(TestCase): y = x[0] assert_raises(IndexError, y.__getitem__, asbytes('a')) + def test_multiple_field_name_unicode(self): + def test_assign_unicode(): + dt = np.dtype([("\u20B9", "f8"), + ("B", "f8"), + ("\u20B9", "f8")]) + + # Error raised when multiple fields have the same name(unicode included) + assert_raises(ValueError, test_assign_unicode) + else: def test_unicode_field_titles(self): # Unicode field titles are added to field dict on Py2 |