From 0f3e1024819b69c9adbefb03ec8e5f6205a5e75b Mon Sep 17 00:00:00 2001 From: sweeneyde Date: Fri, 23 Jul 2021 21:59:57 -0400 Subject: [PATCH 1/3] Don't crash when calling weakref.proxy(not_an_iterator).__next__ --- Lib/test/test_weakref.py | 30 ++++++++++++++++++++++++++++++ Objects/weakrefobject.c | 6 ++++++ 2 files changed, 36 insertions(+) diff --git a/Lib/test/test_weakref.py b/Lib/test/test_weakref.py index dd5a781ed59d8b..acd7284c0d7f09 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 referenced a non-iterator" + with self.assertRaisesRegex(TypeError, msg): + list(a) + def test_proxy_reversed(self): class MyObj: def __len__(self): diff --git a/Objects/weakrefobject.c b/Objects/weakrefobject.c index c36d2395cc8c53..733aa5b57a8bc8 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 referenced a non-iterator '%.200s' object", + Py_TYPE(obj)->tp_name); + return NULL; + } Py_INCREF(obj); PyObject* res = PyIter_Next(obj); Py_DECREF(obj); From 5af7c2c4b3dabd2162ac2b970017213af3e54591 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sat, 24 Jul 2021 02:18:00 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEWS.d/next/Library/2021-07-24-02-17-59.bpo-44720.shU5Qm.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2021-07-24-02-17-59.bpo-44720.shU5Qm.rst 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 00000000000000..83694f3988ae9a --- /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 From 7847f1a5293638d61262ad994a9fcb12c476e460 Mon Sep 17 00:00:00 2001 From: sweeneyde Date: Fri, 23 Jul 2021 22:24:56 -0400 Subject: [PATCH 3/3] Error message: Weakref --> Weakref proxy --- Lib/test/test_weakref.py | 2 +- Objects/weakrefobject.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_weakref.py b/Lib/test/test_weakref.py index acd7284c0d7f09..1a5314ccff315a 100644 --- a/Lib/test/test_weakref.py +++ b/Lib/test/test_weakref.py @@ -437,7 +437,7 @@ def __iter__(self): return weakref.proxy(not_an_iterator) a = A() - msg = "Weakref referenced a non-iterator" + msg = "Weakref proxy referenced a non-iterator" with self.assertRaisesRegex(TypeError, msg): list(a) diff --git a/Objects/weakrefobject.c b/Objects/weakrefobject.c index 733aa5b57a8bc8..bb56c7dbdb8322 100644 --- a/Objects/weakrefobject.c +++ b/Objects/weakrefobject.c @@ -659,8 +659,8 @@ proxy_iternext(PyWeakReference *proxy) PyObject *obj = PyWeakref_GET_OBJECT(proxy); if (!PyIter_Check(obj)) { PyErr_Format(PyExc_TypeError, - "Weakref referenced a non-iterator '%.200s' object", - Py_TYPE(obj)->tp_name); + "Weakref proxy referenced a non-iterator '%.200s' object", + Py_TYPE(obj)->tp_name); return NULL; } Py_INCREF(obj);