Skip to content
Open
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
32 changes: 32 additions & 0 deletions Lib/test/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -1869,6 +1869,38 @@ def test_copy(self):
self.assertEqual(d2, frozendict(x=1, y=2))
self.assertEqual(type(d2), frozendict)

def test_gc_tracking(self):
self.assertFalse(gc.is_tracked(frozendict()))
self.assertFalse(gc.is_tracked(frozendict({1: 2})))
self.assertFalse(gc.is_tracked(frozendict.fromkeys('ab', 1)))
self.assertFalse(gc.is_tracked(frozendict({1: 2}) | {3: 4}))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please, test nested frozendicts :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found that the current implementation doesn't support nested frozendicts / frozensets. The outer frozendict is conservatively tracked even if the nested one is untracked. This is because the current implementation of _PyObject_GC_MAY_BE_TRACKED only has special support for tuples:

static inline int _PyObject_GC_MAY_BE_TRACKED(PyObject *obj) {
if (!PyObject_IS_GC(obj)) {
return 0;
}
if (PyTuple_CheckExact(obj)) {
return _PyObject_GC_IS_TRACKED(obj);
}
return 1;
}

frozenset has the same limitation. We can update _PyObject_GC_MAY_BE_TRACKED to support frozenset and frozendict too. But I think this could be out of the current PR's scope, and adding a test to check that nested frozendicts are not supported and then updating it in another PR seems meaningless. If you think it's OK, I'll create another issue to track the process.


# a tuple is tracked by the GC when created; a collection untracks
# it if it contains no gc-tracked items, and then it can never be
# tracked again, so the frozendict is left untracked as well
t = (2,)
gc.collect()
self.assertFalse(gc.is_tracked(t))
self.assertFalse(gc.is_tracked(frozendict({1: t})))

self.assertTrue(gc.is_tracked(frozendict({1: [2]})))
Comment thread
aisk marked this conversation as resolved.
class Key:
pass
self.assertTrue(gc.is_tracked(frozendict({Key(): 1})))
# subclasses can create reference cycles, they are always tracked
self.assertTrue(gc.is_tracked(FrozenDict({1: 2})))

def test_gc_collect_reference_cycle(self):
# a reference cycle through a tracked frozendict is collectable
Comment thread
aisk marked this conversation as resolved.
class Obj:
pass
obj = Obj()
obj.fd = frozendict({1: obj})
ref = weakref.ref(obj)
del obj
gc.collect()
self.assertIsNone(ref())

def test_merge(self):
# test "a | b" operator
self.assertEqual(frozendict(x=1) | frozendict(y=2),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:class:`frozendict` objects whose keys and values can never be tracked by
the garbage collector are no longer tracked, like :class:`frozenset`
objects.
59 changes: 53 additions & 6 deletions Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ As a consequence of this, split keys have a maximum size of 16.
static PyObject* frozendict_new(PyTypeObject *type, PyObject *args,
PyObject *kwds);
static PyObject* frozendict_new_untracked(PyTypeObject *type);
static void frozendict_maybe_track(PyObject *op);
static PyObject* dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
static PyObject* dict_new_untracked(PyTypeObject *type);
static int dict_merge(PyObject *a, PyObject *b, int override, PyObject **dupkey);
Expand Down Expand Up @@ -3479,11 +3480,18 @@ _PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
// gh-151722: If cls constructor returns a frozendict which is tracked by
// the GC, create a frozendict copy which is not tracked by the GC.
//
// gh-155176: Since frozendicts whose contents can never be tracked by the
// GC are left untracked, being untracked no longer implies that d is
// private: also make a copy if d is not uniquely referenced, otherwise
// inserting keys below would mutate an object visible elsewhere.
//
// At the function exit, return cls(fd) where fd is a frozendict.
//
// Untracking the frozendict requires tracking again the frozendict on
// error which is more complicated. It's easier to work on a copy.
if (PyFrozenDict_Check(d) && _PyObject_GC_IS_TRACKED(d)) {
if (PyFrozenDict_Check(d)
&& (_PyObject_GC_IS_TRACKED(d)
|| !_PyObject_IsUniquelyReferenced(d))) {
need_copy = 1;

PyObject *copy = frozendict_new_untracked(&PyFrozenDict_Type);
Expand Down Expand Up @@ -3637,7 +3645,12 @@ _PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
Py_SETREF(d, copy);
}
else if (!_PyObject_GC_IS_TRACKED(d)) {
_PyObject_GC_TRACK(d);
if (PyFrozenDict_Check(d)) {
frozendict_maybe_track(d);
}
else {
_PyObject_GC_TRACK(d);
}
}
return d;
}
Expand Down Expand Up @@ -5202,7 +5215,12 @@ _PyDict_Or(PyObject *self, PyObject *other)
Py_DECREF(new);
return NULL;
}
_PyObject_GC_TRACK(new);
if (PyFrozenDict_Check(new)) {
frozendict_maybe_track(new);
}
else {
_PyObject_GC_TRACK(new);
}
return new;
}

Expand Down Expand Up @@ -5472,7 +5490,7 @@ frozendict_vectorcall(PyObject *type, PyObject * const*args,
}
}

_PyObject_GC_TRACK(self);
frozendict_maybe_track(self);
return self;
}

Expand Down Expand Up @@ -8502,6 +8520,35 @@ frozendict_new_untracked(PyTypeObject *type)
return d;
}

/* Track a fully built frozendict in the GC, unless it can never be part of
a reference cycle: an exact frozendict whose keys and values are all
guaranteed to never be tracked by the GC. Subclasses can create reference
cycles, so they are always tracked. */
static void
frozendict_maybe_track(PyObject *op)
{
assert(PyFrozenDict_Check(op));
assert(!_PyObject_GC_IS_TRACKED(op));

if (PyFrozenDict_CheckExact(op)) {
PyObject *key, *value;
Py_ssize_t pos = 0;
int track = 0;
while (_PyDict_Next(op, &pos, &key, &value, NULL)) {
if (_PyObject_GC_MAY_BE_TRACKED(key)
|| _PyObject_GC_MAY_BE_TRACKED(value))
{
track = 1;
break;
}
}
if (!track) {
return;
}
}
_PyObject_GC_TRACK(op);
}

static PyObject *
frozendict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
Expand All @@ -8520,7 +8567,7 @@ frozendict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
assert(kwds == NULL);
}

_PyObject_GC_TRACK(d);
frozendict_maybe_track(d);
return d;
}

Expand Down Expand Up @@ -8566,7 +8613,7 @@ frozendict_copy_impl(PyFrozenDictObject *self)

PyObject *copy = anydict_copy_untracked((PyObject*)self);
if (copy != NULL) {
_PyObject_GC_TRACK(copy);
frozendict_maybe_track(copy);
}
return copy;
}
Expand Down
Loading