Skip to content

Commit 592e8f0

Browse files
gh-130327: Always traverse managed dictionaries, even when inline values are available (#130469)
1 parent eb611a8 commit 592e8f0

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

Lib/test/test_dict.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,6 +1569,26 @@ def make_pairs():
15691569
self.assertEqual(d.get(key3_3), 44)
15701570
self.assertGreaterEqual(eq_count, 1)
15711571

1572+
def test_overwrite_managed_dict(self):
1573+
# GH-130327: Overwriting an object's managed dictionary with another object's
1574+
# skipped traversal in favor of inline values, causing the GC to believe that
1575+
# the __dict__ wasn't reachable.
1576+
import gc
1577+
1578+
class Shenanigans:
1579+
pass
1580+
1581+
to_be_deleted = Shenanigans()
1582+
to_be_deleted.attr = "whatever"
1583+
holds_reference = Shenanigans()
1584+
holds_reference.__dict__ = to_be_deleted.__dict__
1585+
holds_reference.ref = {"circular": to_be_deleted, "data": 42}
1586+
1587+
del to_be_deleted
1588+
gc.collect()
1589+
self.assertEqual(holds_reference.ref['data'], 42)
1590+
self.assertEqual(holds_reference.attr, "whatever")
1591+
15721592
def test_unhashable_key(self):
15731593
d = {'a': 1}
15741594
key = [1, 2, 3]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix erroneous clearing of an object's :attr:`~object.__dict__` if
2+
overwritten at runtime.

Objects/dictobject.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4825,10 +4825,8 @@ dict_traverse(PyObject *op, visitproc visit, void *arg)
48254825

48264826
if (DK_IS_UNICODE(keys)) {
48274827
if (_PyDict_HasSplitTable(mp)) {
4828-
if (!mp->ma_values->embedded) {
4829-
for (i = 0; i < n; i++) {
4830-
Py_VISIT(mp->ma_values->values[i]);
4831-
}
4828+
for (i = 0; i < n; i++) {
4829+
Py_VISIT(mp->ma_values->values[i]);
48324830
}
48334831
}
48344832
else {
@@ -7413,16 +7411,21 @@ PyObject_VisitManagedDict(PyObject *obj, visitproc visit, void *arg)
74137411
if((tp->tp_flags & Py_TPFLAGS_MANAGED_DICT) == 0) {
74147412
return 0;
74157413
}
7416-
if (tp->tp_flags & Py_TPFLAGS_INLINE_VALUES) {
7414+
PyDictObject *dict = _PyObject_ManagedDictPointer(obj)->dict;
7415+
if (dict != NULL) {
7416+
// GH-130327: If there's a managed dictionary available, we should
7417+
// *always* traverse it. The dict is responsible for traversing the
7418+
// inline values if it points to them.
7419+
Py_VISIT(dict);
7420+
}
7421+
else if (tp->tp_flags & Py_TPFLAGS_INLINE_VALUES) {
74177422
PyDictValues *values = _PyObject_InlineValues(obj);
74187423
if (values->valid) {
74197424
for (Py_ssize_t i = 0; i < values->capacity; i++) {
74207425
Py_VISIT(values->values[i]);
74217426
}
7422-
return 0;
74237427
}
74247428
}
7425-
Py_VISIT(_PyObject_ManagedDictPointer(obj)->dict);
74267429
return 0;
74277430
}
74287431

0 commit comments

Comments
 (0)