[Python-checkins] bpo-39590: make deque.__contains__ and deque.count hold strong references (GH-18421) (GH-18423)

Miss Islington (bot) webhook-mailer at python.org
Sun Feb 9 03:39:40 EST 2020


https://github.com/python/cpython/commit/dc56f5f48866bf3c5412642bba00890d9a090cfb
commit: dc56f5f48866bf3c5412642bba00890d9a090cfb
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2020-02-09T00:39:28-08:00
summary:

bpo-39590: make deque.__contains__ and deque.count hold strong references (GH-18421) (GH-18423)

(cherry picked from commit c6dedde160a9fce5d049e860f586ad8f93aec822)

Co-authored-by: sweeneyde <36520290+sweeneyde at users.noreply.github.com>

Co-authored-by: sweeneyde <36520290+sweeneyde at users.noreply.github.com>

files:
A Misc/NEWS.d/next/Library/2020-02-09-05-51-05.bpo-39590.rf98GU.rst
M Lib/test/test_deque.py
M Modules/_collectionsmodule.c

diff --git a/Lib/test/test_deque.py b/Lib/test/test_deque.py
index 51b66b76aca91..c0f7138254f3f 100644
--- a/Lib/test/test_deque.py
+++ b/Lib/test/test_deque.py
@@ -183,6 +183,18 @@ def test_contains(self):
         with self.assertRaises(RuntimeError):
             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):
         d = deque('a')
         self.assertRaises(TypeError, d.extend, 1)
diff --git a/Misc/NEWS.d/next/Library/2020-02-09-05-51-05.bpo-39590.rf98GU.rst b/Misc/NEWS.d/next/Library/2020-02-09-05-51-05.bpo-39590.rf98GU.rst
new file mode 100644
index 0000000000000..68625028fb7af
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-02-09-05-51-05.bpo-39590.rf98GU.rst
@@ -0,0 +1 @@
+Collections.deque now holds strong references during deque.__contains__ and deque.count, fixing crashes.
\ No newline at end of file
diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c
index 45169ecd11af0..cc2b90eaa283e 100644
--- a/Modules/_collectionsmodule.c
+++ b/Modules/_collectionsmodule.c
@@ -966,7 +966,9 @@ deque_count(dequeobject *deque, PyObject *v)
     while (--n >= 0) {
         CHECK_NOT_END(b);
         item = b->data[index];
+        Py_INCREF(item);
         cmp = PyObject_RichCompareBool(item, v, Py_EQ);
+        Py_DECREF(item);
         if (cmp < 0)
             return NULL;
         count += cmp;
@@ -1003,7 +1005,9 @@ deque_contains(dequeobject *deque, PyObject *v)
     while (--n >= 0) {
         CHECK_NOT_END(b);
         item = b->data[index];
+        Py_INCREF(item);
         cmp = PyObject_RichCompareBool(item, v, Py_EQ);
+        Py_DECREF(item);
         if (cmp) {
             return cmp;
         }



More information about the Python-checkins mailing list