mirror of
https://github.com/python/cpython.git
synced 2025-10-03 13:45:29 +00:00
bpo-44720: Don't crash when calling weakref.proxy(not_an_iterator).__next__ (GH-27316) (#27325)
(cherry picked from commit 5370f0a82a
)
Co-authored-by: Dennis Sweeney <36520290+sweeneyde@users.noreply.github.com>
This commit is contained in:
parent
097801844c
commit
0a08f22184
3 changed files with 37 additions and 0 deletions
|
@ -411,6 +411,36 @@ class ReferencesTestCase(TestBase):
|
||||||
# can be killed in the middle of the call
|
# can be killed in the middle of the call
|
||||||
"blech" in p
|
"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):
|
def test_proxy_reversed(self):
|
||||||
class MyObj:
|
class MyObj:
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
``weakref.proxy`` objects referencing non-iterators now raise ``TypeError`` rather than dereferencing the null ``tp_iternext`` slot and crashing.
|
|
@ -657,6 +657,12 @@ proxy_iternext(PyWeakReference *proxy)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
PyObject *obj = PyWeakref_GET_OBJECT(proxy);
|
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);
|
Py_INCREF(obj);
|
||||||
PyObject* res = PyIter_Next(obj);
|
PyObject* res = PyIter_Next(obj);
|
||||||
Py_DECREF(obj);
|
Py_DECREF(obj);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue