diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index 0fae2dfe9fb..4a02ec178e6 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -4247,6 +4247,14 @@ order (MRO) for bases """ with self.assertRaises(TypeError): str.__add__(fake_str, "abc") + def test_repr_as_str(self): + # Issue #11603: crash or infinite loop when rebinding __str__ as + # __repr__. + class Foo: + pass + Foo.__repr__ = Foo.__str__ + foo = Foo() + str(foo) class DictProxyTests(unittest.TestCase): def setUp(self): diff --git a/Misc/NEWS b/Misc/NEWS index 28f8d0ae189..8b7e02c237e 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -225,6 +225,9 @@ Core and Builtins Library ------- +- Issue #11603: Fix a crash when __str__ is rebound as __repr__. Patch by + Andreas Stührk. + - Issue #11321: Fix a crash with multiple imports of the _pickle module when embedding Python. Patch by Andreas Stührk. diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 1642dade540..d80b594e9a6 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -2968,7 +2968,7 @@ object_str(PyObject *self) unaryfunc f; f = Py_TYPE(self)->tp_repr; - if (f == NULL) + if (f == NULL || f == object_str) f = object_repr; return f(self); }