From: Antoine Pitrou Date: Fri, 15 Jul 2011 19:15:07 +0000 (+0200) Subject: Issue #11603: Fix a crash when __str__ is rebound as __repr__. X-Git-Tag: v3.1.5rc1~15 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8cdc40e3b0622ea4eeb8b2c9b0e6796be685d16d;p=python Issue #11603: Fix a crash when __str__ is rebound as __repr__. Patch by Andreas Stührk. --- diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index c74e232776..0ce85f0b69 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -4252,6 +4252,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 63d5c24e6e..b14e169a88 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -13,6 +13,9 @@ Core and Builtins Library ------- +- Issue #11603: Fix a crash when __str__ is rebound as __repr__. Patch by + Andreas Stührk. + What's New in Python 3.1.4? =========================== diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 5c20e0da11..70971f38ee 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -2821,7 +2821,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); }