[pypy-commit] pypy object-dtype2: reflect attributes of object, start to write gc test

mattip noreply at buildbot.pypy.org
Fri Mar 6 16:02:52 CET 2015


Author: mattip <matti.picus at gmail.com>
Branch: object-dtype2
Changeset: r76254:a4a38e2aec66
Date: 2015-03-06 17:03 +0200
http://bitbucket.org/pypy/pypy/changeset/a4a38e2aec66/

Log:	reflect attributes of object, start to write gc test

diff --git a/pypy/module/micronumpy/boxes.py b/pypy/module/micronumpy/boxes.py
--- a/pypy/module/micronumpy/boxes.py
+++ b/pypy/module/micronumpy/boxes.py
@@ -616,6 +616,8 @@
     def convert_to(self, space, dtype):
         return self # XXX
 
+    def descr__getattr__(self, space, w_key):
+        return space.getattr(self.w_obj, w_key)
 
 W_GenericBox.typedef = TypeDef("numpy.generic",
     __new__ = interp2app(W_GenericBox.descr__new__.im_func),
@@ -865,3 +867,9 @@
     __new__ = interp2app(W_UnicodeBox.descr__new__unicode_box.im_func),
     __len__ = interp2app(W_UnicodeBox.descr_len),
 )
+
+W_ObjectBox.typedef = TypeDef("numpy.object", W_ObjectBox.typedef,
+    __new__ = interp2app(W_ObjectBox.descr__new__.im_func),
+    __getattr__ = interp2app(W_ObjectBox.descr__getattr__),
+)
+
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
@@ -473,7 +473,12 @@
             pass
         for o in [object, O]:
             print np.dtype(o).byteorder
-            assert np.dtype(o).str == '|O8'
+            if self.ptr_size == 4:
+                assert np.dtype(o).str == '|O4'
+            elif self.ptr_size == 8:
+                assert np.dtype(o).str == '|O8'
+            else:
+                assert False,'self._ptr_size unknown'
 
 class AppTestTypes(BaseAppTestDtypes):
     def test_abstract_types(self):
@@ -1350,9 +1355,11 @@
         from numpy import array
         import sys
         class Polynomial(object):
-            pass
+            def whatami(self):
+                return 'an object'
         a = array(Polynomial())
         assert a.shape == ()
+        assert a.sum().whatami() == 'an object'
 
     def test_uninitialized_object_array_is_filled_by_None(self):
         import numpy as np
@@ -1369,3 +1376,16 @@
 
         res = a + b
         assert res[0] == "foobar"
+
+    def test_keep_object_alive(self):
+        import numpy as np
+        import gc
+        class O(object):
+            def whatami(self):
+                return 'an object'
+        fiveOs = [O()] * 5
+        a = np.array(fiveOs, dtype=object)
+        del fiveOs
+        gc.collect()
+        gc.collect()
+        assert a[2].whatami() == 'an object'


More information about the pypy-commit mailing list