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 "ProblematicFactory()"
+
+ dd = defaultdict(ProblematicFactory())
+ # Should not raise RecursionError
+ r = repr(dd)
+ self.assertIn('ProblematicFactory()', r)
+
if __name__ == "__main__":
unittest.main()
}
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);