Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-130327: Always traverse managed dictionaries, even when inline values are available #130469

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Lib/test/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -1560,6 +1560,26 @@ def make_pairs():
self.assertEqual(d.get(key3_3), 44)
self.assertGreaterEqual(eq_count, 1)

def test_overwrite_managed_dict(self):
# GH-130327: Overwriting an object's managed dictionary with another object's
# skipped traversal in favor of inline values, causing the GC to believe that
# the __dict__ wasn't reachable.
import gc

class Shenanigans:
pass

to_be_deleted = Shenanigans()
to_be_deleted.attr = "whatever"
holds_reference = Shenanigans()
holds_reference.__dict__ = to_be_deleted.__dict__
holds_reference.ref = {"circular": to_be_deleted, "data": 42}

del to_be_deleted
gc.collect()
self.assertEqual(holds_reference.ref['data'], 42)
self.assertEqual(holds_reference.attr, "whatever")

def test_unhashable_key(self):
d = {'a': 1}
key = [1, 2, 3]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix erroneous clearing of an object's :attr:`~object.__dict__` if
overwritten at runtime.
14 changes: 9 additions & 5 deletions Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -4619,10 +4619,8 @@ dict_traverse(PyObject *op, visitproc visit, void *arg)

if (DK_IS_UNICODE(keys)) {
if (_PyDict_HasSplitTable(mp)) {
if (!mp->ma_values->embedded) {
for (i = 0; i < n; i++) {
Py_VISIT(mp->ma_values->values[i]);
}
for (i = 0; i < n; i++) {
Py_VISIT(mp->ma_values->values[i]);
}
}
else {
Expand Down Expand Up @@ -7190,6 +7188,13 @@ PyObject_VisitManagedDict(PyObject *obj, visitproc visit, void *arg)
if((tp->tp_flags & Py_TPFLAGS_MANAGED_DICT) == 0) {
return 0;
}
PyDictObject *dict = _PyObject_ManagedDictPointer(obj)->dict;
if (dict != NULL) {
// GH-130327: If there's a managed dictionary available, we should
// *always* traverse it, including when inline values are available.
Py_VISIT(dict);
return 0;
}
if (tp->tp_flags & Py_TPFLAGS_INLINE_VALUES) {
PyDictValues *values = _PyObject_InlineValues(obj);
if (values->valid) {
Expand All @@ -7199,7 +7204,6 @@ PyObject_VisitManagedDict(PyObject *obj, visitproc visit, void *arg)
return 0;
}
}
Py_VISIT(_PyObject_ManagedDictPointer(obj)->dict);
return 0;
}

Expand Down
Loading