diff --git a/Lib/test/test_capi/test_dict.py b/Lib/test/test_capi/test_dict.py index 9a2e79d4da7e715..21caa67fda9521a 100644 --- a/Lib/test/test_capi/test_dict.py +++ b/Lib/test/test_capi/test_dict.py @@ -635,6 +635,18 @@ class DictSubtype(dict): ... self.assertIs(type(d), DictSubtype) self.assertEqual(d, DictSubtype({})) + # instance dict that still has inline values + class A: ... + a = A() + a.x = 1 + f = as_frozendict(a.__dict__) + self.assertEqual(vars(a), {}) + a.x = 2 + self.assertEqual(f, frozendict(x=1)) + self.assertEqual(vars(a), {'x': 2}) + del a + self.assertEqual(f, frozendict(x=1)) + for wrong_input in (frozendict(), [], None): with self.subTest(wrong_input=wrong_input): with self.assertRaises(SystemError): diff --git a/Misc/NEWS.d/next/C_API/2026-11-10-18-00-09.gh-issue-161767.2EoShw.rst b/Misc/NEWS.d/next/C_API/2026-11-10-18-00-09.gh-issue-161767.2EoShw.rst new file mode 100644 index 000000000000000..2cbf227f1d236a4 --- /dev/null +++ b/Misc/NEWS.d/next/C_API/2026-11-10-18-00-09.gh-issue-161767.2EoShw.rst @@ -0,0 +1,2 @@ +Fix a crash in :c:func:`PyDict_AsFrozenDictAndClear` when called on an +instance :attr:`~object.__dict__` that still uses inline values. diff --git a/Objects/dictobject.c b/Objects/dictobject.c index ce6f4a61ad3c262..4b3127c321ed632 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -8662,33 +8662,50 @@ frozendict_new(PyTypeObject *type, PyObject *args, PyObject *kwds) return d; } -static void +static int transfer_keys_and_values_lock_held(PyObject *res, PyObject *dict) { PyDictObject *new = (PyDictObject *)res; PyDictObject *old = (PyDictObject *)dict; assert(can_modify_dict(old)); - // Fast path: do nothing on an empty dict: if (old->ma_keys == Py_EMPTY_KEYS) { - return; + return 0; } Py_ssize_t used = old->ma_used; PyDictKeysObject *keys = old->ma_keys; PyDictValues *values = old->ma_values; - // Clear the old dict keys and values, but do not decref them: - clear_common(old); - set_keys(old, Py_EMPTY_KEYS); - set_values(old, NULL); + if (values && values->embedded) { + // values are inline in the object, copy them and clear the object's copy + PyDictValues *v = values; + values = copy_values(v); + if (!values) { + PyErr_NoMemory(); + return -1; + } + dictkeys_incref(keys); + clear_common(old); + for (Py_ssize_t i = 0; i < v->capacity; i++) { + FT_ATOMIC_STORE_PTR_RELEASE(v->values[i], NULL); + } + FT_ATOMIC_STORE_UINT8_RELEASE(v->size, 0); + } + else { + clear_common(old); + set_keys(old, Py_EMPTY_KEYS); + set_values(old, NULL); + } ASSERT_CONSISTENT(old); - // Transfer keys and values from dict to frozendict: + // Only transfer keys and values to the new dict + // once the old dict is cleared new->ma_used = used; new->ma_keys = keys; new->ma_values = values; ASSERT_CONSISTENT(new); + return 0; } PyObject * @@ -8704,9 +8721,14 @@ PyDict_AsFrozenDictAndClear(PyObject *dict) return NULL; } + int err; Py_BEGIN_CRITICAL_SECTION(dict); - transfer_keys_and_values_lock_held(res, dict); + err = transfer_keys_and_values_lock_held(res, dict); Py_END_CRITICAL_SECTION(); + if (err < 0) { + Py_DECREF(res); + return NULL; + } _PyObject_GC_TRACK(res); assert(_PyFrozenDictObject_CAST(res)->ma_hash == -1);