From 304f0f952da3c2f1d354b1701aa5ad61b53ff179 Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Fri, 15 Jul 2011 21:22:50 +0200 Subject: [PATCH] =?UTF-8?q?Issue=20#11603:=20Fix=20a=20crash=20when=20=5F?= =?UTF-8?q?=5Fstr=5F=5F=20is=20rebound=20as=20=5F=5Frepr=5F=5F.=20Patch=20?= =?UTF-8?q?by=20Andreas=20St=C3=BChrk.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Lib/test/test_descr.py | 8 ++++++++ Misc/NEWS | 3 +++ Objects/typeobject.c | 2 +- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index b5d98900e23..964cc5cf564 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -4581,6 +4581,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(object): + 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 867ef9ce6cc..2ec44d9a6a7 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -33,6 +33,9 @@ Core and Builtins Library ------- +- Issue #11603: Fix a crash when __str__ is rebound as __repr__. Patch by + Andreas Stührk. + - Issue #12502: asyncore: fix polling loop with AF_UNIX sockets. - Issue #4376: ctypes now supports nested structures in a endian different than diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 8326d073f10..3864b48b4bb 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -2980,7 +2980,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); }