bpo-39590: make deque.__contains__ and deque.count hold strong references (GH-18421) (GH-18423)
(cherry picked from commit c6dedde160
)
Co-authored-by: sweeneyde <36520290+sweeneyde@users.noreply.github.com>
Co-authored-by: sweeneyde <36520290+sweeneyde@users.noreply.github.com>
This commit is contained in:
parent
0c915e620d
commit
dc56f5f488
|
@ -183,6 +183,18 @@ class TestBasic(unittest.TestCase):
|
||||||
with self.assertRaises(RuntimeError):
|
with self.assertRaises(RuntimeError):
|
||||||
n in d
|
n in d
|
||||||
|
|
||||||
|
def test_contains_count_stop_crashes(self):
|
||||||
|
class A:
|
||||||
|
def __eq__(self, other):
|
||||||
|
d.clear()
|
||||||
|
return NotImplemented
|
||||||
|
d = deque([A(), A()])
|
||||||
|
with self.assertRaises(RuntimeError):
|
||||||
|
_ = 3 in d
|
||||||
|
d = deque([A(), A()])
|
||||||
|
with self.assertRaises(RuntimeError):
|
||||||
|
_ = d.count(3)
|
||||||
|
|
||||||
def test_extend(self):
|
def test_extend(self):
|
||||||
d = deque('a')
|
d = deque('a')
|
||||||
self.assertRaises(TypeError, d.extend, 1)
|
self.assertRaises(TypeError, d.extend, 1)
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Collections.deque now holds strong references during deque.__contains__ and deque.count, fixing crashes.
|
|
@ -966,7 +966,9 @@ deque_count(dequeobject *deque, PyObject *v)
|
||||||
while (--n >= 0) {
|
while (--n >= 0) {
|
||||||
CHECK_NOT_END(b);
|
CHECK_NOT_END(b);
|
||||||
item = b->data[index];
|
item = b->data[index];
|
||||||
|
Py_INCREF(item);
|
||||||
cmp = PyObject_RichCompareBool(item, v, Py_EQ);
|
cmp = PyObject_RichCompareBool(item, v, Py_EQ);
|
||||||
|
Py_DECREF(item);
|
||||||
if (cmp < 0)
|
if (cmp < 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
count += cmp;
|
count += cmp;
|
||||||
|
@ -1003,7 +1005,9 @@ deque_contains(dequeobject *deque, PyObject *v)
|
||||||
while (--n >= 0) {
|
while (--n >= 0) {
|
||||||
CHECK_NOT_END(b);
|
CHECK_NOT_END(b);
|
||||||
item = b->data[index];
|
item = b->data[index];
|
||||||
|
Py_INCREF(item);
|
||||||
cmp = PyObject_RichCompareBool(item, v, Py_EQ);
|
cmp = PyObject_RichCompareBool(item, v, Py_EQ);
|
||||||
|
Py_DECREF(item);
|
||||||
if (cmp) {
|
if (cmp) {
|
||||||
return cmp;
|
return cmp;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue