[Python-checkins] cpython (3.4): Defer deleted item decref until after the deque is restored to a consistent

raymond.hettinger python-checkins at python.org
Sat May 2 19:45:41 CEST 2015


https://hg.python.org/cpython/rev/2d26f0f92b11
changeset:   95842:2d26f0f92b11
branch:      3.4
parent:      95839:813854f49f9d
user:        Raymond Hettinger <python at rcn.com>
date:        Sat May 02 10:44:17 2015 -0700
summary:
  Defer deleted item decref until after the deque is restored to a consistent state.

files:
  Misc/NEWS                    |   4 ++++
  Modules/_collectionsmodule.c |  12 ++++++------
  2 files changed, 10 insertions(+), 6 deletions(-)


diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -45,6 +45,10 @@
 - Issues #24099, #24100, and #24101: Fix free-after-use bug in heapq's siftup
   and siftdown functions.
 
+- Backport collections.deque fixes from Python 3.5.  Prevents reentrant badness
+  during deletion by deferring the decref until the container has been restored
+  to a consistent state.
+
 - Issue #23008: Fixed resolving attributes with boolean value is False in pydoc.
 
 - Fix asyncio issue 235: LifoQueue and PriorityQueue's put didn't
diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c
--- a/Modules/_collectionsmodule.c
+++ b/Modules/_collectionsmodule.c
@@ -698,9 +698,9 @@
         if (cmp > 0) {
             PyObject *tgt = deque_popleft(deque, NULL);
             assert (tgt != NULL);
+            if (_deque_rotate(deque, i))
+                return NULL;
             Py_DECREF(tgt);
-            if (_deque_rotate(deque, i) == -1)
-                return NULL;
             Py_RETURN_NONE;
         }
         else if (cmp < 0) {
@@ -781,16 +781,16 @@
 deque_del_item(dequeobject *deque, Py_ssize_t i)
 {
     PyObject *item;
+    int rv;
 
     assert (i >= 0 && i < Py_SIZE(deque));
-    if (_deque_rotate(deque, -i) == -1)
+    if (_deque_rotate(deque, -i))
         return -1;
-
     item = deque_popleft(deque, NULL);
+    rv = _deque_rotate(deque, i);
     assert (item != NULL);
     Py_DECREF(item);
-
-    return _deque_rotate(deque, i);
+    return rv;
 }
 
 static int

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list