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

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


http://hg.python.org/cpython/rev/cd9eca1bf531
changeset:   71360:cd9eca1bf531
branch:      2.7
parent:      71354:45b1ae1ef318
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Fri Jul 15 21:22:50 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
@@ -4581,6 +4581,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(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
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -33,6 +33,9 @@
 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
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -2980,7 +2980,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