Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b6a880d

Browse files
committed
Fix "super" objects pretty printing in PyPy
In PyPy there's no __self__ attribute for super(.) objects. As objects always have a __repr__, the found alternative was to use its curried argument. Previously, only the class name was verified as part of the result, now the tests include a regex.
1 parent 4518b9a commit b6a880d

2 files changed

Lines changed: 10 additions & 4 deletions

File tree

IPython/lib/pretty.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def _repr_pretty_(self, p, cycle):
8585
import datetime
8686
from collections import deque
8787

88-
from IPython.utils.py3compat import PY3, cast_unicode, string_types
88+
from IPython.utils.py3compat import PY3, PYPY, cast_unicode, string_types
8989
from IPython.utils.encoding import get_stream_enc
9090

9191
from io import StringIO
@@ -632,7 +632,11 @@ def _super_pprint(obj, p, cycle):
632632
p.pretty(obj.__thisclass__)
633633
p.text(',')
634634
p.breakable()
635-
p.pretty(obj.__self__)
635+
if PYPY: # In PyPy, super() objects doesn't have __self__ attributes
636+
dself = obj.__repr__.__self__
637+
p.pretty(None if dself is obj else dself)
638+
else:
639+
p.pretty(obj.__self__)
636640
p.end_group(8, '>')
637641

638642

IPython/lib/tests/test_pretty.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,14 @@ class SB(SA):
188188
pass
189189

190190
def test_super_repr():
191+
# "<super: module_name.SA, None>"
191192
output = pretty.pretty(super(SA))
192-
nt.assert_in("SA", output)
193+
nt.assert_regexp_matches(output, r"<super: \S+.SA, None>")
193194

195+
# "<super: module_name.SA, <module_name.SB at 0x...>>"
194196
sb = SB()
195197
output = pretty.pretty(super(SA, sb))
196-
nt.assert_in("SA", output)
198+
nt.assert_regexp_matches(output, r"<super: \S+.SA,\s+<\S+.SB at 0x\S+>>")
197199

198200

199201
def test_long_list():

0 commit comments

Comments
 (0)