]> granicus.if.org Git - python/commitdiff
Issue #11603: Fix a crash when __str__ is rebound as __repr__.
authorAntoine Pitrou <solipsis@pitrou.net>
Fri, 15 Jul 2011 19:22:50 +0000 (21:22 +0200)
committerAntoine Pitrou <solipsis@pitrou.net>
Fri, 15 Jul 2011 19:22:50 +0000 (21:22 +0200)
Patch by Andreas Stührk.

Lib/test/test_descr.py
Misc/NEWS
Objects/typeobject.c

index b5d98900e23ab3547fa739439164111a7501c8df..964cc5cf564bf8549fb46d3096d506b1c897d54f 100644 (file)
@@ -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):
index 867ef9ce6cc9a26ae631b210fd6c3f841ea74e47..2ec44d9a6a71342787db721ab9dbcc52c09d9c1c 100644 (file)
--- 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
index 8326d073f101e25ee46c69577528003866e82bbe..3864b48b4bba6c36a20010a1e277b30ed80dec3b 100644 (file)
@@ -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);
 }