summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2021-07-24 09:45:13 (GMT)
committerGitHub <noreply@github.com>2021-07-24 09:45:13 (GMT)
commit659030c7d56a329c1aa559678f2df15e306215e4 (patch)
tree2472aacf20968fc67df0fea1efb6d6c9d67b144e /Lib
parentd15949a845a6db66675bca7105ad508ba9e79639 (diff)
downloadcpython-659030c7d56a329c1aa559678f2df15e306215e4.zip
cpython-659030c7d56a329c1aa559678f2df15e306215e4.tar.gz
cpython-659030c7d56a329c1aa559678f2df15e306215e4.tar.bz2
bpo-44720: Don't crash when calling weakref.proxy(not_an_iterator).__next__ (GH-27316) (GH-27324)
(cherry picked from commit 5370f0a82aaa4ba617070d5c71d2b18236096ac0) Co-authored-by: Dennis Sweeney <36520290+sweeneyde@users.noreply.github.com>
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_weakref.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/Lib/test/test_weakref.py b/Lib/test/test_weakref.py
index dd5a781..1a5314c 100644
--- a/Lib/test/test_weakref.py
+++ b/Lib/test/test_weakref.py
@@ -411,6 +411,36 @@ class ReferencesTestCase(TestBase):
# can be killed in the middle of the call
"blech" in p
+ def test_proxy_next(self):
+ arr = [4, 5, 6]
+ def iterator_func():
+ yield from arr
+ it = iterator_func()
+
+ class IteratesWeakly:
+ def __iter__(self):
+ return weakref.proxy(it)
+
+ weak_it = IteratesWeakly()
+
+ # Calls proxy.__next__
+ self.assertEqual(list(weak_it), [4, 5, 6])
+
+ def test_proxy_bad_next(self):
+ # bpo-44720: PyIter_Next() shouldn't be called if the reference
+ # isn't an iterator.
+
+ not_an_iterator = lambda: 0
+
+ class A:
+ def __iter__(self):
+ return weakref.proxy(not_an_iterator)
+ a = A()
+
+ msg = "Weakref proxy referenced a non-iterator"
+ with self.assertRaisesRegex(TypeError, msg):
+ list(a)
+
def test_proxy_reversed(self):
class MyObj:
def __len__(self):