[pypy-commit] pypy py3.3: Apply same trick as the 3.2 version: replace sys.getrefcount by something slow, but with good results.

amauryfa noreply at buildbot.pypy.org
Mon Oct 27 23:48:19 CET 2014


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: py3.3
Changeset: r74261:65d97fb6906b
Date: 2014-10-27 21:24 +0100
http://bitbucket.org/pypy/pypy/changeset/65d97fb6906b/

Log:	Apply same trick as the 3.2 version: replace sys.getrefcount by
	something slow, but with good results.

diff --git a/lib-python/3/test/test_memoryview.py b/lib-python/3/test/test_memoryview.py
--- a/lib-python/3/test/test_memoryview.py
+++ b/lib-python/3/test/test_memoryview.py
@@ -13,6 +13,13 @@
 import io
 
 
+try:
+    getrefcount = sys.getrefcount
+except AttributeError:
+    # PyPy
+    getrefcount = lambda o: len(gc.get_referents(o))
+
+
 class AbstractMemoryTests:
     source_bytes = b"abcdef"
 
@@ -26,7 +33,7 @@
 
     def check_getitem_with_type(self, tp):
         b = tp(self._source)
-        oldrefcount = sys.getrefcount(b)
+        oldrefcount = getrefcount(b)
         m = self._view(b)
         self.assertEqual(m[0], ord(b"a"))
         self.assertIsInstance(m[0], int)
@@ -43,7 +50,7 @@
         self.assertRaises(TypeError, lambda: m[0.0])
         self.assertRaises(TypeError, lambda: m["a"])
         m = None
-        self.assertEqual(sys.getrefcount(b), oldrefcount)
+        self.assertEqual(getrefcount(b), oldrefcount)
 
     def test_getitem(self):
         for tp in self._types:
@@ -59,7 +66,7 @@
         if not self.ro_type:
             self.skipTest("no read-only type to test")
         b = self.ro_type(self._source)
-        oldrefcount = sys.getrefcount(b)
+        oldrefcount = getrefcount(b)
         m = self._view(b)
         def setitem(value):
             m[0] = value
@@ -67,14 +74,14 @@
         self.assertRaises(TypeError, setitem, 65)
         self.assertRaises(TypeError, setitem, memoryview(b"a"))
         m = None
-        self.assertEqual(sys.getrefcount(b), oldrefcount)
+        self.assertEqual(getrefcount(b), oldrefcount)
 
     def test_setitem_writable(self):
         if not self.rw_type:
             self.skipTest("no writable type to test")
         tp = self.rw_type
         b = self.rw_type(self._source)
-        oldrefcount = sys.getrefcount(b)
+        oldrefcount = getrefcount(b)
         m = self._view(b)
         m[0] = ord(b'1')
         self._check_contents(tp, b, b"1bcdef")
@@ -119,7 +126,7 @@
         self.assertRaises(ValueError, setitem, slice(0,2), b"a")
 
         m = None
-        self.assertEqual(sys.getrefcount(b), oldrefcount)
+        self.assertEqual(getrefcount(b), oldrefcount)
 
     def test_delitem(self):
         for tp in self._types:
@@ -203,14 +210,14 @@
         # Test PyObject_GetBuffer() on a memoryview object.
         for tp in self._types:
             b = tp(self._source)
-            oldrefcount = sys.getrefcount(b)
+            oldrefcount = getrefcount(b)
             m = self._view(b)
-            oldviewrefcount = sys.getrefcount(m)
+            oldviewrefcount = getrefcount(m)
             s = str(m, "utf-8")
             self._check_contents(tp, b, s.encode("utf-8"))
-            self.assertEqual(sys.getrefcount(m), oldviewrefcount)
+            self.assertEqual(getrefcount(m), oldviewrefcount)
             m = None
-            self.assertEqual(sys.getrefcount(b), oldrefcount)
+            self.assertEqual(getrefcount(b), oldrefcount)
 
     def test_gc(self):
         for tp in self._types:
@@ -403,9 +410,9 @@
     def test_refs(self):
         for tp in self._types:
             m = memoryview(tp(self._source))
-            oldrefcount = sys.getrefcount(m)
+            oldrefcount = getrefcount(m)
             m[1:2]
-            self.assertEqual(sys.getrefcount(m), oldrefcount)
+            self.assertEqual(getrefcount(m), oldrefcount)
 
 class BaseMemorySliceSliceTests:
     source_bytes = b"XabcdefY"


More information about the pypy-commit mailing list