[pypy-commit] pypy default: support auto naming record dtype fields

bdkearns noreply at buildbot.pypy.org
Sat Feb 22 23:46:50 CET 2014


Author: Brian Kearns <bdkearns at gmail.com>
Branch: 
Changeset: r69274:f437d5038346
Date: 2014-02-22 17:24 -0500
http://bitbucket.org/pypy/pypy/changeset/f437d5038346/

Log:	support auto naming record dtype fields

diff --git a/pypy/module/micronumpy/interp_dtype.py b/pypy/module/micronumpy/interp_dtype.py
--- a/pypy/module/micronumpy/interp_dtype.py
+++ b/pypy/module/micronumpy/interp_dtype.py
@@ -357,8 +357,8 @@
     fields = {}
     offset = 0
     fieldnames = []
-    for w_elem in lst_w:
-        size = 1
+    for i in range(len(lst_w)):
+        w_elem = lst_w[i]
         w_shape = space.newtuple([])
         if space.len_w(w_elem) == 3:
             w_fldname, w_flddesc, w_shape = space.fixedview(w_elem)
@@ -368,11 +368,13 @@
             w_fldname, w_flddesc = space.fixedview(w_elem, 2)
         subdtype = descr__new__(space, space.gettypefor(W_Dtype), w_flddesc, w_shape=w_shape)
         fldname = space.str_w(w_fldname)
+        if fldname == '':
+            fldname = 'f%d' % i
         if fldname in fields:
-            raise OperationError(space.w_ValueError, space.wrap("two fields with the same name"))
+            raise oefmt(space.w_ValueError, "two fields with the same name")
         assert isinstance(subdtype, W_Dtype)
         fields[fldname] = (offset, subdtype)
-        offset += subdtype.get_size() * size
+        offset += subdtype.get_size()
         fieldnames.append(fldname)
     itemtype = types.RecordType()
     return W_Dtype(itemtype, NPY_VOID, NPY_VOIDLTR,
diff --git a/pypy/module/micronumpy/test/test_dtypes.py b/pypy/module/micronumpy/test/test_dtypes.py
--- a/pypy/module/micronumpy/test/test_dtypes.py
+++ b/pypy/module/micronumpy/test/test_dtypes.py
@@ -991,6 +991,8 @@
         assert d.type is void
         assert d.char == 'V'
         assert d.names == ("x", "y", "z", "value")
+        d.names = ('a', '', 'c', 'd')
+        assert d.names == ('a', '', 'c', 'd')
         d.names = ('a', 'b', 'c', 'd')
         assert d.names == ('a', 'b', 'c', 'd')
         exc = raises(ValueError, "d.names = ('a', 'b', 'c', 'c')")
@@ -1000,6 +1002,14 @@
         assert d.names == ('a', 'b', 'c', 'd')
         raises(KeyError, 'd["xyz"]')
         raises(KeyError, 'd.fields["xyz"]')
+        d = dtype([('', '<i8'), ('', '<f8')])
+        assert d.descr == [('f0', '<i8'), ('f1', '<f8')]
+        d = dtype([('', '<i8'), ('b', '<f8')])
+        assert d.descr == [('f0', '<i8'), ('b', '<f8')]
+        d = dtype([('a', '<i8'), ('', '<f8')])
+        assert d.descr == [('a', '<i8'), ('f1', '<f8')]
+        exc = raises(ValueError, "dtype([('a', '<i8'), ('a', '<f8')])")
+        assert exc.value[0] == 'two fields with the same name'
 
     def test_create_from_dict(self):
         import numpy as np


More information about the pypy-commit mailing list