summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_weakref.py
diff options
context:
space:
mode:
authorPablo Galindo <Pablogsal@gmail.com>2019-10-08 15:30:50 (GMT)
committerGitHub <noreply@github.com>2019-10-08 15:30:50 (GMT)
commit10cd00a9e3c22af37c748ea5a417f6fb66601e21 (patch)
treeb1f56966388b990f6ac7838f1435ae48ad5c7409 /Lib/test/test_weakref.py
parent03ab6b4fc6f59a4452756e7a3a46310ce30ec4b2 (diff)
downloadcpython-10cd00a9e3c22af37c748ea5a417f6fb66601e21.zip
cpython-10cd00a9e3c22af37c748ea5a417f6fb66601e21.tar.gz
cpython-10cd00a9e3c22af37c748ea5a417f6fb66601e21.tar.bz2
bpo-38395: Fix ownership in weakref.proxy methods (GH-16632)
The implementation of weakref.proxy's methods call back into the Python API using a borrowed references of the weakly referenced object (acquired via PyWeakref_GET_OBJECT). This API call may delete the last reference to the object (either directly or via GC), leaving a dangling pointer, which can be subsequently dereferenced. To fix this, claim a temporary ownership of the referenced object when calling the appropriate method. Some functions because at the moment they do not need to access the borrowed referent, but to protect against future changes to these functions, ownership need to be fixed in all potentially affected methods.
Diffstat (limited to 'Lib/test/test_weakref.py')
-rw-r--r--Lib/test/test_weakref.py20
1 files changed, 20 insertions, 0 deletions
diff --git a/Lib/test/test_weakref.py b/Lib/test/test_weakref.py
index e14df9e..228bc17 100644
--- a/Lib/test/test_weakref.py
+++ b/Lib/test/test_weakref.py
@@ -391,6 +391,26 @@ class ReferencesTestCase(TestBase):
lyst = List()
self.assertEqual(bool(weakref.proxy(lyst)), bool(lyst))
+ def test_proxy_iter(self):
+ # Test fails with a debug build of the interpreter
+ # (see bpo-38395).
+
+ obj = None
+
+ class MyObj:
+ def __iter__(self):
+ nonlocal obj
+ del obj
+ return NotImplemented
+
+ obj = MyObj()
+ p = weakref.proxy(obj)
+ with self.assertRaises(TypeError):
+ # "blech" in p calls MyObj.__iter__ through the proxy,
+ # without keeping a reference to the real object, so it
+ # can be killed in the middle of the call
+ "blech" in p
+
def test_getweakrefcount(self):
o = C()
ref1 = weakref.ref(o)