diff options
-rw-r--r-- | numpy/core/_internal.py | 10 | ||||
-rw-r--r-- | numpy/core/src/multiarray/buffer.c | 14 | ||||
-rw-r--r-- | numpy/core/tests/test_multiarray.py | 18 |
3 files changed, 30 insertions, 12 deletions
diff --git a/numpy/core/_internal.py b/numpy/core/_internal.py index 56acfb757..109da1567 100644 --- a/numpy/core/_internal.py +++ b/numpy/core/_internal.py @@ -351,6 +351,7 @@ def _index_fields(ary, fields): # construct a Numpy dtype _pep3118_map = { + '?': '?', 'b': 'b', 'B': 'B', 'h': 'h', @@ -373,6 +374,7 @@ _pep3118_map = { 'O': 'O', 'x': 'V', # padding } +_pep3118_typechars = ''.join(_pep3118_map.keys()) def _dtype_from_pep3118(spec, byteorder='=', is_subdtype=False): from numpy.core.multiarray import dtype @@ -421,10 +423,10 @@ def _dtype_from_pep3118(spec, byteorder='=', is_subdtype=False): if itemsize != 1: # Not supported raise ValueError("Non item-size 1 structures not supported") - elif spec[0].isalpha(): + elif spec[0] in _pep3118_typechars: j = 1 for j in xrange(1, len(spec)): - if not spec[j].isalpha(): + if spec[j] not in _pep3118_typechars: break typechar = spec[:j] spec = spec[j:] @@ -446,16 +448,18 @@ def _dtype_from_pep3118(spec, byteorder='=', is_subdtype=False): value = dtype((value, shape)) # Field name + this_explicit_name = False if spec and spec.startswith(':'): i = spec[1:].index(':') + 1 name = spec[1:i] spec = spec[i+1:] explicit_name = True + this_explicit_name = True else: name = 'f%d' % findex findex += 1 - if not is_padding: + if not is_padding or this_explicit_name: fields[name] = (value, offset) offset += value.itemsize diff --git a/numpy/core/src/multiarray/buffer.c b/numpy/core/src/multiarray/buffer.c index fdf71135f..eec446fcb 100644 --- a/numpy/core/src/multiarray/buffer.c +++ b/numpy/core/src/multiarray/buffer.c @@ -238,6 +238,7 @@ _buffer_format_string(PyArray_Descr *descr, _tmp_string_t *str, } switch (descr->type_num) { + case NPY_BOOL: if (_append_char(str, '?')) return -1; break; case NPY_BYTE: if (_append_char(str, 'b')) return -1; break; case NPY_UBYTE: if (_append_char(str, 'B')) return -1; break; case NPY_SHORT: if (_append_char(str, 'h')) return -1; break; @@ -254,6 +255,9 @@ _buffer_format_string(PyArray_Descr *descr, _tmp_string_t *str, case NPY_CFLOAT: if (_append_str(str, "Zf")) return -1; break; case NPY_CDOUBLE: if (_append_str(str, "Zd")) return -1; break; case NPY_CLONGDOUBLE: if (_append_str(str, "Zg")) return -1; break; + /* XXX: datetime */ + /* XXX: timedelta */ + case NPY_OBJECT: if (_append_char(str, 'O')) return -1; break; case NPY_STRING: { char buf[128]; PyOS_snprintf(buf, sizeof(buf), "%ds", descr->elsize); @@ -268,9 +272,15 @@ _buffer_format_string(PyArray_Descr *descr, _tmp_string_t *str, if (_append_str(str, buf)) return -1; break; } - case NPY_OBJECT: if (_append_char(str, 'O')) return -1; break; + case NPY_VOID: { + /* Insert padding bytes */ + char buf[128]; + PyOS_snprintf(buf, sizeof(buf), "%dx", descr->elsize); + if (_append_str(str, buf)) return -1; + break; + } default: - PyErr_Format(PyExc_ValueError, "unknown dtype code %d", + PyErr_Format(PyExc_ValueError, "cannot convert dtype %d to buffer", descr->type_num); return -1; } diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 04a9f785b..0d7717a7e 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -1491,9 +1491,11 @@ if sys.version_info >= (2, 6): ('j', np.double), ('k', np.longdouble), ('l', 'S4'), - ('m', 'U4')] + ('m', 'U4'), + ('n', 'V3'), + ('o', '?')] x = np.array([(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - asbytes('aaaa'), 'bbbb')], + asbytes('aaaa'), 'bbbb', asbytes('xxx'), True)], dtype=dt) self._check_roundtrip(x) @@ -1549,17 +1551,19 @@ if sys.version_info >= (2, 6): ('j', np.double), ('k', np.longdouble), ('l', 'S4'), - ('m', 'U4')] + ('m', 'U4'), + ('n', 'V3'), + ('o', '?')] x = np.array([(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - asbytes('aaaa'), 'bbbb')], + asbytes('aaaa'), 'bbbb', asbytes(' '), True)], dtype=dt) y = memoryview(x) - assert y.format == 'T{b:a:=h:b:=l:c:=q:d:B:e:=H:f:=L:g:=Q:h:=d:i:=d:j:=g:k:4s:l:=4w:m:}' + assert y.format == 'T{b:a:=h:b:=l:c:=q:d:B:e:=H:f:=L:g:=Q:h:=d:i:=d:j:=g:k:4s:l:=4w:m:3x:n:?:o:}' assert y.shape == (1,) assert y.ndim == 1 - assert y.strides == (78,) + assert y.strides == (82,) assert y.suboffsets is None - assert y.itemsize == 78 + assert y.itemsize == 82 def test_export_subarray(self): x = np.array(([[1,2],[3,4]],), dtype=[('a', (int, (2,2)))]) |