[Python-checkins] cpython (merge 3.1 -> 3.2): Issue #11603: Fix a crash when __str__ is rebound as __repr__.

antoine.pitrou python-checkins at python.org
Fri Jul 15 21:21:05 CEST 2011


http://hg.python.org/cpython/rev/8b52ac4a0c9f
changeset:   71356:8b52ac4a0c9f
branch:      3.2
parent:      71347:1ae0b7b8de0b
parent:      71355:b488e027c952
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Fri Jul 15 21:17:14 2011 +0200
summary:
  Issue #11603: Fix a crash when __str__ is rebound as __repr__.
Patch by Andreas Stührk.

files:
  Lib/test/test_descr.py |  8 ++++++++
  Misc/NEWS              |  3 +++
  Objects/typeobject.c   |  2 +-
  3 files changed, 12 insertions(+), 1 deletions(-)


diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py
--- a/Lib/test/test_descr.py
+++ b/Lib/test/test_descr.py
@@ -4252,6 +4252,14 @@
         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
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -27,6 +27,9 @@
 Library
 -------
 
+- Issue #11603: Fix a crash when __str__ is rebound as __repr__.  Patch by
+  Andreas Stührk.
+
 - Issue #11321: Fix a crash with multiple imports of the _pickle module when
   embedding Python.  Patch by Andreas Stührk.
 
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -2890,7 +2890,7 @@
     unaryfunc f;
 
     f = Py_TYPE(self)->tp_repr;
-    if (f == NULL)
+    if (f == NULL || f == object_str)
         f = object_repr;
     return f(self);
 }

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list