[Numpy-svn] r8221 - in trunk/numpy/core: . src/multiarray tests

numpy-svn@scip... numpy-svn@scip...
Sat Feb 20 20:52:39 CST 2010


Author: ptvirtan
Date: 2010-02-20 20:52:39 -0600 (Sat, 20 Feb 2010)
New Revision: 8221

Modified:
   trunk/numpy/core/_internal.py
   trunk/numpy/core/src/multiarray/buffer.c
   trunk/numpy/core/tests/test_multiarray.py
Log:
3K: ENH: core: support BOOL and VOID better in PEP 3118 buffers

Modified: trunk/numpy/core/_internal.py
===================================================================
--- trunk/numpy/core/_internal.py	2010-02-21 02:52:20 UTC (rev 8220)
+++ trunk/numpy/core/_internal.py	2010-02-21 02:52:39 UTC (rev 8221)
@@ -351,6 +351,7 @@
 # construct a Numpy dtype
 
 _pep3118_map = {
+    '?': '?',
     'b': 'b',
     'B': 'B',
     'h': 'h',
@@ -373,6 +374,7 @@
     '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 @@
             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 @@
             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
 

Modified: trunk/numpy/core/src/multiarray/buffer.c
===================================================================
--- trunk/numpy/core/src/multiarray/buffer.c	2010-02-21 02:52:20 UTC (rev 8220)
+++ trunk/numpy/core/src/multiarray/buffer.c	2010-02-21 02:52:39 UTC (rev 8221)
@@ -238,6 +238,7 @@
         }
 
         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 @@
         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 @@
             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;
         }

Modified: trunk/numpy/core/tests/test_multiarray.py
===================================================================
--- trunk/numpy/core/tests/test_multiarray.py	2010-02-21 02:52:20 UTC (rev 8220)
+++ trunk/numpy/core/tests/test_multiarray.py	2010-02-21 02:52:39 UTC (rev 8221)
@@ -1491,9 +1491,11 @@
                   ('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 @@
                   ('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)))])



More information about the Numpy-svn mailing list