Commits

Philip Jenvey committed e291f08

workaround lack of sys.getrefcount

Comments (0)

Files changed (1)

lib-python/3.2/test/test_memoryview.py

 import array
 import io
 
+getrefcount = sys.getrefcount if hasattr(sys, 'getrefcount') else lambda o: -1
 
 class AbstractMemoryTests:
     source_bytes = b"abcdef"
     def check_getitem_with_type(self, tp):
         item = self.getitem_type
         b = tp(self._source)
-        oldrefcount = sys.getrefcount(b)
+        oldrefcount = getrefcount(b)
         m = self._view(b)
         self.assertEqual(m[0], item(b"a"))
         self.assertIsInstance(m[0], bytes)
         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:
         if not self.ro_type:
             return
         b = self.ro_type(self._source)
-        oldrefcount = sys.getrefcount(b)
+        oldrefcount = getrefcount(b)
         m = self._view(b)
         def setitem(value):
             m[0] = value
         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:
             return
         tp = self.rw_type
         b = self.rw_type(self._source)
-        oldrefcount = sys.getrefcount(b)
+        oldrefcount = getrefcount(b)
         m = self._view(b)
         m[0] = tp(b"0")
         self._check_contents(tp, b, b"0bcdef")
         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:
         # 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:
     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"