issue 2045: Infinite recursion when printing a subclass of defaultdict,
if default_factory is set to a bound method.

Backport of r60663.
diff --git a/Modules/collectionsmodule.c b/Modules/collectionsmodule.c
index 9d128fc..dd3c0b7 100644
--- a/Modules/collectionsmodule.c
+++ b/Modules/collectionsmodule.c
@@ -1217,7 +1217,17 @@
 	if (dd->default_factory == NULL)
 		defrepr = PyString_FromString("None");
 	else
-		defrepr = PyObject_Repr(dd->default_factory);
+	{
+		int status = Py_ReprEnter(dd->default_factory);
+		if (status != 0) {
+			if (status < 0)
+				return NULL;
+			defrepr = PyString_FromString("...");
+		}
+		else
+			defrepr = PyObject_Repr(dd->default_factory);
+		Py_ReprLeave(dd->default_factory);
+	}
 	if (defrepr == NULL) {
 		Py_DECREF(baserepr);
 		return NULL;