Skip to content

Commit ff35050

Browse files
committed
Issue python#11603: Fix a crash when __str__ is rebound as __repr__.
Patch by Andreas Stührk.
2 parents 8391cf4 + 8cdc40e commit ff35050

3 files changed

Lines changed: 12 additions & 1 deletion

File tree

Lib/test/test_descr.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4252,6 +4252,14 @@ class FakeStr:
42524252
with self.assertRaises(TypeError):
42534253
str.__add__(fake_str, "abc")
42544254

4255+
def test_repr_as_str(self):
4256+
# Issue #11603: crash or infinite loop when rebinding __str__ as
4257+
# __repr__.
4258+
class Foo:
4259+
pass
4260+
Foo.__repr__ = Foo.__str__
4261+
foo = Foo()
4262+
str(foo)
42554263

42564264
class DictProxyTests(unittest.TestCase):
42574265
def setUp(self):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ Core and Builtins
2727
Library
2828
-------
2929

30+
- Issue #11603: Fix a crash when __str__ is rebound as __repr__. Patch by
31+
Andreas Stührk.
32+
3033
- Issue #11321: Fix a crash with multiple imports of the _pickle module when
3134
embedding Python. Patch by Andreas Stührk.
3235

Objects/typeobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2890,7 +2890,7 @@ object_str(PyObject *self)
28902890
unaryfunc f;
28912891

28922892
f = Py_TYPE(self)->tp_repr;
2893-
if (f == NULL)
2893+
if (f == NULL || f == object_str)
28942894
f = object_repr;
28952895
return f(self);
28962896
}

0 commit comments

Comments
 (0)