]> 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:15:07 +0000 (21:15 +0200)
committerAntoine Pitrou <solipsis@pitrou.net>
Fri, 15 Jul 2011 19:15:07 +0000 (21:15 +0200)
Patch by Andreas Stührk.

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

index c74e2327769f7f5c1b62d0374ef5ea2798fbde77..0ce85f0b6975ff81ab3261f0cb596571c27bb256 100644 (file)
@@ -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):
index 63d5c24e6ef18a8f66462b7f71c13390c1ae8511..b14e169a883429e185a1b86d08a94c1e9bfa7cfb 100644 (file)
--- 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?
 ===========================
index 5c20e0da1100a4ca3622803692ac2a72f3ba4fb3..70971f38eefe5ac23de4bff2519e02f4f0afb69d 100644 (file)
@@ -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);
 }