Issue #11603: Fix a crash when __str__ is rebound as __repr__.

Patch by Andreas Stührk.
This commit is contained in:
Antoine Pitrou 2011-07-15 21:22:50 +02:00
parent c3349cd22e
commit 304f0f952d
3 changed files with 12 additions and 1 deletions

View File

@ -4581,6 +4581,14 @@ order (MRO) for bases """
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
str.__add__(fake_str, "abc") 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): class DictProxyTests(unittest.TestCase):
def setUp(self): def setUp(self):

View File

@ -33,6 +33,9 @@ Core and Builtins
Library 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 #12502: asyncore: fix polling loop with AF_UNIX sockets.
- Issue #4376: ctypes now supports nested structures in a endian different than - Issue #4376: ctypes now supports nested structures in a endian different than

View File

@ -2980,7 +2980,7 @@ object_str(PyObject *self)
unaryfunc f; unaryfunc f;
f = Py_TYPE(self)->tp_repr; f = Py_TYPE(self)->tp_repr;
if (f == NULL) if (f == NULL || f == object_str)
f = object_repr; f = object_repr;
return f(self); return f(self);
} }