[Python-checkins] bpo-44720: Don't crash when calling weakref.proxy(not_an_iterator).__next__ (GH-27316) (#27325)
ambv
webhook-mailer at python.org
Sat Jul 24 05:45:44 EDT 2021
https://github.com/python/cpython/commit/0a08f22184aef6e36bb8bb7ad84207e47594f46e
commit: 0a08f22184aef6e36bb8bb7ad84207e47594f46e
branch: 3.9
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: ambv <lukasz at langa.pl>
date: 2021-07-24T11:45:40+02:00
summary:
bpo-44720: Don't crash when calling weakref.proxy(not_an_iterator).__next__ (GH-27316) (#27325)
(cherry picked from commit 5370f0a82aaa4ba617070d5c71d2b18236096ac0)
Co-authored-by: Dennis Sweeney <36520290+sweeneyde at users.noreply.github.com>
files:
A Misc/NEWS.d/next/Library/2021-07-24-02-17-59.bpo-44720.shU5Qm.rst
M Lib/test/test_weakref.py
M Objects/weakrefobject.c
diff --git a/Lib/test/test_weakref.py b/Lib/test/test_weakref.py
index dd5a781ed59d8..1a5314ccff315 100644
--- a/Lib/test/test_weakref.py
+++ b/Lib/test/test_weakref.py
@@ -411,6 +411,36 @@ def __iter__(self):
# 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):
diff --git a/Misc/NEWS.d/next/Library/2021-07-24-02-17-59.bpo-44720.shU5Qm.rst b/Misc/NEWS.d/next/Library/2021-07-24-02-17-59.bpo-44720.shU5Qm.rst
new file mode 100644
index 0000000000000..83694f3988ae9
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-07-24-02-17-59.bpo-44720.shU5Qm.rst
@@ -0,0 +1 @@
+``weakref.proxy`` objects referencing non-iterators now raise ``TypeError`` rather than dereferencing the null ``tp_iternext`` slot and crashing.
\ No newline at end of file
diff --git a/Objects/weakrefobject.c b/Objects/weakrefobject.c
index c36d2395cc8c5..bb56c7dbdb832 100644
--- a/Objects/weakrefobject.c
+++ b/Objects/weakrefobject.c
@@ -657,6 +657,12 @@ proxy_iternext(PyWeakReference *proxy)
return NULL;
PyObject *obj = PyWeakref_GET_OBJECT(proxy);
+ if (!PyIter_Check(obj)) {
+ PyErr_Format(PyExc_TypeError,
+ "Weakref proxy referenced a non-iterator '%.200s' object",
+ Py_TYPE(obj)->tp_name);
+ return NULL;
+ }
Py_INCREF(obj);
PyObject* res = PyIter_Next(obj);
Py_DECREF(obj);
More information about the Python-checkins
mailing list