diff --git a/Lib/test/test_dict.py b/Lib/test/test_dict.py index 26bdaa9d6a35b7c..9d013e3d2caa1c3 100644 --- a/Lib/test/test_dict.py +++ b/Lib/test/test_dict.py @@ -479,6 +479,13 @@ def test_copy_noncompact(self): d2 = d.copy() self.assertEqual(d2, d) + @support.nomemtest + def test_copy_empty_no_memory(self): + import _testcapi + d = {} + with self.assertRaises(MemoryError): + _testcapi.call_with_nomemory(0, 0, d.copy) + def test_get(self): d = {} self.assertIs(d.get('c'), None) diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-12-23-32-50.gh-issue-159285.nlLuwQ.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-12-23-32-50.gh-issue-159285.nlLuwQ.rst new file mode 100644 index 000000000000000..0cb795f8309f73e --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-12-23-32-50.gh-issue-159285.nlLuwQ.rst @@ -0,0 +1,2 @@ +Fix a crash in a debug build when copying an empty :class:`dict` fails with +:exc:`MemoryError`. Patch by Christian Aurich Zanettini Martins. diff --git a/Objects/dictobject.c b/Objects/dictobject.c index eed9bd92c55bf2b..13c9e2e4b395abd 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -4497,7 +4497,7 @@ copy_lock_held_untracked(PyObject *o, int as_frozendict) else { d = dict_new_untracked(&PyDict_Type); } - assert(!_PyObject_GC_IS_TRACKED(d)); + assert(d == NULL || !_PyObject_GC_IS_TRACKED(d)); return d; }