mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Reviewed by Benjamin Peterson.
This commit is contained in:
parent
d26782e863
commit
c6b09ebe58
3 changed files with 101 additions and 34 deletions
|
@ -6,6 +6,8 @@ XXX We need more tests! Some tests are in test_bytes
|
|||
import unittest
|
||||
import test.support
|
||||
import sys
|
||||
import gc
|
||||
import weakref
|
||||
|
||||
|
||||
class CommonMemoryTests:
|
||||
|
@ -157,6 +159,36 @@ class CommonMemoryTests:
|
|||
m = self.check_attributes_with_type(bytearray)
|
||||
self.assertEquals(m.readonly, False)
|
||||
|
||||
def test_getbuffer(self):
|
||||
# Test PyObject_GetBuffer() on a memoryview object.
|
||||
b = self.base_object
|
||||
oldrefcount = sys.getrefcount(b)
|
||||
m = self._view(b)
|
||||
oldviewrefcount = sys.getrefcount(m)
|
||||
s = str(m, "utf-8")
|
||||
self._check_contents(b, s.encode("utf-8"))
|
||||
self.assertEquals(sys.getrefcount(m), oldviewrefcount)
|
||||
m = None
|
||||
self.assertEquals(sys.getrefcount(b), oldrefcount)
|
||||
|
||||
def test_gc(self):
|
||||
class MyBytes(bytes):
|
||||
pass
|
||||
class MyObject:
|
||||
pass
|
||||
|
||||
# Create a reference cycle through a memoryview object
|
||||
b = MyBytes(b'abc')
|
||||
m = self._view(b)
|
||||
o = MyObject()
|
||||
b.m = m
|
||||
b.o = o
|
||||
wr = weakref.ref(o)
|
||||
b = m = o = None
|
||||
# The cycle must be broken
|
||||
gc.collect()
|
||||
self.assert_(wr() is None, wr())
|
||||
|
||||
|
||||
class MemoryviewTest(unittest.TestCase, CommonMemoryTests):
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue