[pypy-commit] pypy numpy-1.10: consolidate coerce for record and void-with-fields dtypes, fix translation

mattip noreply at buildbot.pypy.org
Fri Nov 20 09:21:57 EST 2015


Author: mattip <matti.picus at gmail.com>
Branch: numpy-1.10
Changeset: r80795:4d46e08b45c3
Date: 2015-11-19 08:21 +0200
http://bitbucket.org/pypy/pypy/changeset/4d46e08b45c3/

Log:	consolidate coerce for record and void-with-fields dtypes, fix
	translation

diff --git a/pypy/module/micronumpy/__init__.py b/pypy/module/micronumpy/__init__.py
--- a/pypy/module/micronumpy/__init__.py
+++ b/pypy/module/micronumpy/__init__.py
@@ -43,10 +43,6 @@
         from pypy.module.micronumpy.concrete import _setup
         _setup()
 
-    def setup_after_space_initialization(self):
-        from pypy.module.micronumpy.support import W_VisibleDeprecationWarning
-        for name, w_type in {'VisibleDeprecationWarning': W_VisibleDeprecationWarning}.items():
-            setattr(self.space, 'w_' + name, self.space.gettypefor(w_type))
 
 class UMathModule(MixedModule):
     appleveldefs = {}
@@ -144,3 +140,10 @@
         'multiarray': MultiArrayModule,
         'umath': UMathModule,
     }
+
+    def setup_after_space_initialization(self):
+        from pypy.module.micronumpy.support import W_VisibleDeprecationWarning
+        print 'in setup_after_space_initialization', self.space
+        for name, w_type in {'VisibleDeprecationWarning': W_VisibleDeprecationWarning}.items():
+            setattr(self.space, 'w_' + name, self.space.gettypefor(w_type))
+
diff --git a/pypy/module/micronumpy/compile.py b/pypy/module/micronumpy/compile.py
--- a/pypy/module/micronumpy/compile.py
+++ b/pypy/module/micronumpy/compile.py
@@ -65,6 +65,7 @@
     w_KeyError = W_TypeObject("KeyError")
     w_SystemExit = W_TypeObject("SystemExit")
     w_KeyboardInterrupt = W_TypeObject("KeyboardInterrupt")
+    w_VisibleDeprecationWarning = W_TypeObject("VisibleDeprecationWarning")
     w_None = None
 
     w_bool = W_TypeObject("bool")
@@ -402,6 +403,9 @@
         assert isinstance(w_check_class, W_TypeObject)
         return w_exc_type.name == w_check_class.name
 
+    def warn(self, w_msg, w_warn_type):
+        pass
+
 class FloatObject(W_Root):
     tp = FakeSpace.w_float
     def __init__(self, floatval):
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
@@ -345,12 +345,22 @@
 
     def test_can_subclass(self):
         import numpy as np
+        import sys
         class xyz(np.void):
             pass
         assert np.dtype(xyz).name == 'xyz'
         # another obscure API, used in numpy record.py
         a = np.dtype((xyz, [('x', 'int32'), ('y', 'float32')]))
-        assert repr(a).endswith("xyz, [('x', '<i4'), ('y', '<f4')]))")
+        assert "[('x', '<i4'), ('y', '<f4')]" in repr(a)
+        assert 'xyz' in repr(a)
+        data = [(1, 'a'), (2, 'bbb')]
+        b = np.dtype((xyz, [('a', int), ('b', object)]))
+        if '__pypy__' in sys.builtin_module_names:
+            raises(NotImplemented, np.array, data, dtype=b)
+        else:
+            arr = np.array(data, dtype=b)
+            assert arr[0][0] == 1
+            assert arr[0][1] == 'a'
 
     def test_index(self):
         import numpy as np
diff --git a/pypy/module/micronumpy/types.py b/pypy/module/micronumpy/types.py
--- a/pypy/module/micronumpy/types.py
+++ b/pypy/module/micronumpy/types.py
@@ -2414,6 +2414,9 @@
                 ofs += size
 
     def coerce(self, space, dtype, w_items):
+        if dtype.is_record():
+            # the dtype is a union of a void and a record,
+            return record_coerce(self, space, dtype, w_items)
         arr = VoidBoxStorage(dtype.elsize, dtype)
         self._coerce(space, arr, 0, dtype, w_items, dtype.shape)
         return boxes.W_VoidBox(arr, 0, dtype)
@@ -2474,17 +2477,7 @@
 class CharType(StringType):
     char = NPY.CHARLTR
 
-class RecordType(FlexibleType):
-    T = lltype.Char
-    num = NPY.VOID
-    kind = NPY.VOIDLTR
-    char = NPY.VOIDLTR
-
-    def read(self, arr, i, offset, dtype):
-        return boxes.W_VoidBox(arr, i + offset, dtype)
-
-    @jit.unroll_safe
-    def coerce(self, space, dtype, w_item):
+def record_coerce(typ, space, dtype, w_item):
         from pypy.module.micronumpy.base import W_NDimArray
         if isinstance(w_item, boxes.W_VoidBox):
             if dtype == w_item.dtype:
@@ -2522,6 +2515,19 @@
             subdtype.store(arr, 0, ofs, w_box)
         return boxes.W_VoidBox(arr, 0, dtype)
 
+class RecordType(FlexibleType):
+    T = lltype.Char
+    num = NPY.VOID
+    kind = NPY.VOIDLTR
+    char = NPY.VOIDLTR
+
+    def read(self, arr, i, offset, dtype):
+        return boxes.W_VoidBox(arr, i + offset, dtype)
+
+    @jit.unroll_safe
+    def coerce(self, space, dtype, w_item):
+        return record_coerce(self, space, dtype, w_item)
+
     def runpack_str(self, space, s, native):
         raise oefmt(space.w_NotImplementedError,
                     "fromstring not implemented for record types")


More information about the pypy-commit mailing list