8000 Issue #11603: Fix a crash when __str__ is rebound as __repr__. · python/cpython@8cdc40e · GitHub
[go: up one dir, main page]

Skip to content

Commit 8cdc40e

Browse files
committed
Issue #11603: Fix a crash when __str__ is rebound as __repr__.
Patch by Andreas Stührk.
1 parent e228130 commit 8cdc40e

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

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
8000
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Core and Builtins
1313
Library
1414
-------
1515

16+
- Issue #11603: Fix a crash when __str__ is rebound as __repr__. Patch by
17+
Andreas Stührk.
18+
1619

1720
What's New in Python 3.1.4?
1821
===========================

Objects/typeobject.c

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

28232823
f = Py_TYPE(self)->tp_repr;
2824-
if (f == NULL)
2824+
if (f == NULL || f == object_str)
28252825
f = object_repr;
28262826
return f(self);
28272827
}

0 commit comments

Comments
 (0)
0