]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-145492: Fix defaultdict __repr__ infinite recursion (GH-145659) (GH-145746)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Thu, 12 Mar 2026 09:45:43 +0000 (10:45 +0100)
committerGitHub <noreply@github.com>
Thu, 12 Mar 2026 09:45:43 +0000 (10:45 +0100)
(cherry picked from commit 2d35f9bc1cf61b27639ed992dfbf363ab436fd8b)

Includes test fix-up from GH-145788
(cherry picked from commit aa4240ebea1aacc907b1efa58e7f547d90cff3b1)

Co-authored-by: Thomas Kowalski <thom.kowa@gmail.com>
Co-authored-by: Matt Van Horn <mvanhorn@users.noreply.github.com>
Lib/test/test_defaultdict.py
Misc/NEWS.d/next/Library/2026-03-09-00-00-00.gh-issue-145492.457Afc.rst [new file with mode: 0644]
Modules/_collectionsmodule.c

index fbd7354a915a0a001a2c7ed8a976a82315be71d3..a193eb10f16d1784c33dfc0e7e4e01ee1b61c52f 100644 (file)
@@ -204,5 +204,20 @@ class TestDefaultDict(unittest.TestCase):
         self.assertEqual(test_dict[key], 2)
         self.assertEqual(count, 2)
 
+    def test_repr_recursive_factory(self):
+        # gh-145492: defaultdict.__repr__ should not cause infinite recursion
+        # when the factory's __repr__ calls repr() on the defaultdict.
+        class ProblematicFactory:
+            def __call__(self):
+                return {}
+            def __repr__(self):
+                repr(dd)
+                return f"ProblematicFactory for {dd}"
+
+        dd = defaultdict(ProblematicFactory())
+        # Should not raise RecursionError
+        r = repr(dd)
+        self.assertIn("ProblematicFactory for", r)
+
 if __name__ == "__main__":
     unittest.main()
diff --git a/Misc/NEWS.d/next/Library/2026-03-09-00-00-00.gh-issue-145492.457Afc.rst b/Misc/NEWS.d/next/Library/2026-03-09-00-00-00.gh-issue-145492.457Afc.rst
new file mode 100644 (file)
index 0000000..297ee40
--- /dev/null
@@ -0,0 +1,3 @@
+Fix infinite recursion in :class:`collections.defaultdict` ``__repr__``
+when a ``defaultdict`` contains itself. Based on analysis by KowalskiThomas
+in :gh:`145492`.
index f5fc0d02a166079f0a899d9a59c8f6f12a1088b4..b2c3d0e42be4cb24dceed163cdc4a2dec96635c3 100644 (file)
@@ -2337,9 +2337,10 @@ defdict_repr(defdictobject *dd)
             }
             defrepr = PyUnicode_FromString("...");
         }
-        else
+        else {
             defrepr = PyObject_Repr(dd->default_factory);
-        Py_ReprLeave(dd->default_factory);
+            Py_ReprLeave(dd->default_factory);
+        }
     }
     if (defrepr == NULL) {
         Py_DECREF(baserepr);