diff options
author | Eric Wieser <wieser.eric@gmail.com> | 2018-11-12 22:14:53 -0800 |
---|---|---|
committer | Eric Wieser <wieser.eric@gmail.com> | 2018-11-13 00:35:00 -0800 |
commit | fc4b477909293b42ce75fb7bce7a69ad255183ec (patch) | |
tree | bf38c6b9bd073b2012085d8393a7dc176d0177ac /numpy/core | |
parent | cd39348e8593dc2b41e2516fbdd8a69b0f0bda6e (diff) | |
download | numpy-fc4b477909293b42ce75fb7bce7a69ad255183ec.tar.gz |
MAINT: Improve error message for legal but unsupported PEP3118 types
Relates to #12369
Traceback is now:
```
In [1]: from array import array
...: import numpy as np
...:
...: buf = array('u', ['q', 'w', 'e', 'r', 't'])
...: view = memoryview(buf)
...:
In [2]: np.array(view)
NotImplementedError: Unsupported PEP 3118 data type 'u' (UCS-2 strings)
The above exception was the direct cause of the following exception:
ValueError: 'u' is not a valid PEP 3118 buffer format string
```
Diffstat (limited to 'numpy/core')
-rw-r--r-- | numpy/core/_internal.py | 11 | ||||
-rw-r--r-- | numpy/core/tests/test_multiarray.py | 7 |
2 files changed, 16 insertions, 2 deletions
diff --git a/numpy/core/_internal.py b/numpy/core/_internal.py index 30069f0ca..a1a166b2c 100644 --- a/numpy/core/_internal.py +++ b/numpy/core/_internal.py @@ -482,6 +482,12 @@ _pep3118_standard_map = { } _pep3118_standard_typechars = ''.join(_pep3118_standard_map.keys()) +_pep3118_unsupported_map = { + 'u': 'UCS-2 strings', + '&': 'pointers', + 't': 'bitfields', + 'X': 'function pointers', +} class _Stream(object): def __init__(self, s): @@ -593,6 +599,11 @@ def __dtype_from_pep3118(stream, is_subdtype): stream.byteorder, stream.byteorder) value = dtype(numpy_byteorder + dtypechar) align = value.alignment + elif stream.next in _pep3118_unsupported_map: + desc = _pep3118_unsupported_map[stream.next] + raise NotImplementedError( + "Unrepresentable PEP 3118 data type {!r} ({})" + .format(stream.next, desc)) else: raise ValueError("Unknown PEP 3118 data type specifier %r" % stream.s) diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index cee8ed35c..d1b306ef0 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -6788,7 +6788,7 @@ class TestNewBufferProtocol(object): ValueError, "format string", np.array, m) - def test_error_message(self): + def test_error_message_unsupported(self): # wchar has no corresponding numpy type - if this changes in future, we # need a better way to construct an invalid memoryview format. t = ctypes.c_wchar * 4 @@ -6797,7 +6797,10 @@ class TestNewBufferProtocol(object): exc = cm.exception if sys.version_info.major > 2: - with assert_raises_regex(ValueError, "Unknown .* specifier 'u'"): + with assert_raises_regex( + NotImplementedError, + r"Unrepresentable .* 'u' \(UCS-2 strings\)" + ): raise exc.__cause__ def test_ctypes_integer_via_memoryview(self): |