[Python-checkins] bpo-39778: Don't traverse weak-reference lists OrderedDict's tp_traverse and tp_clear (GH-18749)
Pablo Galindo
webhook-mailer at python.org
Mon Mar 2 18:12:58 EST 2020
https://github.com/python/cpython/commit/0c2b509f9d1d3a9065bc62c2407e1dc2ed70e9c2
commit: 0c2b509f9d1d3a9065bc62c2407e1dc2ed70e9c2
branch: master
author: Pablo Galindo <Pablogsal at gmail.com>
committer: GitHub <noreply at github.com>
date: 2020-03-02T23:12:54Z
summary:
bpo-39778: Don't traverse weak-reference lists OrderedDict's tp_traverse and tp_clear (GH-18749)
Objects do not own weak references to them directly through the __weakref__ list so these
do not need to be traversed by the GC.
files:
A Misc/NEWS.d/next/Core and Builtins/2020-03-02-19-21-21.bpo-39778._YGLEc.rst
M Lib/test/test_ordered_dict.py
M Objects/odictobject.c
diff --git a/Lib/test/test_ordered_dict.py b/Lib/test/test_ordered_dict.py
index f337be8eb8551..eb0a8f4ba0d48 100644
--- a/Lib/test/test_ordered_dict.py
+++ b/Lib/test/test_ordered_dict.py
@@ -753,6 +753,26 @@ def test_iterators_pickling(self):
self.assertEqual(list(unpickled), expected)
self.assertEqual(list(it), expected)
+ @support.cpython_only
+ def test_weakref_list_is_not_traversed(self):
+ # Check that the weakref list is not traversed when collecting
+ # OrderedDict objects. See bpo-39778 for more information.
+
+ gc.collect()
+
+ x = self.OrderedDict()
+ x.cycle = x
+
+ cycle = []
+ cycle.append(cycle)
+
+ x_ref = weakref.ref(x)
+ cycle.append(x_ref)
+
+ del x, cycle, x_ref
+
+ gc.collect()
+
class PurePythonOrderedDictSubclassTests(PurePythonOrderedDictTests):
diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-03-02-19-21-21.bpo-39778._YGLEc.rst b/Misc/NEWS.d/next/Core and Builtins/2020-03-02-19-21-21.bpo-39778._YGLEc.rst
new file mode 100644
index 0000000000000..dc49512167365
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2020-03-02-19-21-21.bpo-39778._YGLEc.rst
@@ -0,0 +1,2 @@
+Fixed a crash due to incorrect handling of weak references in
+``collections.OrderedDict`` classes. Patch by Pablo Galindo.
diff --git a/Objects/odictobject.c b/Objects/odictobject.c
index f412220e8cc02..6813cddfddca5 100644
--- a/Objects/odictobject.c
+++ b/Objects/odictobject.c
@@ -1454,7 +1454,6 @@ odict_traverse(PyODictObject *od, visitproc visit, void *arg)
_ODictNode *node;
Py_VISIT(od->od_inst_dict);
- Py_VISIT(od->od_weakreflist);
_odict_FOREACH(od, node) {
Py_VISIT(_odictnode_KEY(node));
}
@@ -1467,7 +1466,6 @@ static int
odict_tp_clear(PyODictObject *od)
{
Py_CLEAR(od->od_inst_dict);
- Py_CLEAR(od->od_weakreflist);
PyDict_Clear((PyObject *)od);
_odict_clear_nodes(od);
return 0;
More information about the Python-checkins
mailing list