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

Skip to content

Commit 4bc2ed9

Browse files
committed
Merge pull request #6717 from remram44/use-metaclass-repr-master
Use metaclass repr [master]
2 parents 3c1a39f + 3a581c3 commit 4bc2ed9

2 files changed

Lines changed: 54 additions & 7 deletions

File tree

IPython/lib/pretty.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -517,11 +517,7 @@ def _default_pprint(obj, p, cycle):
517517
klass = _safe_getattr(obj, '__class__', None) or type(obj)
518518
if _safe_getattr(klass, '__repr__', None) not in _baseclass_reprs:
519519
# A user-provided repr. Find newlines and replace them with p.break_()
520-
output = repr(obj)
521-
for idx,output_line in enumerate(output.splitlines()):
522-
if idx:
523-
p.break_()
524-
p.text(output_line)
520+
_repr_pprint(obj, p, cycle)
525521
return
526522
p.begin_group(1, '<')
527523
p.pretty(klass)
@@ -682,6 +678,12 @@ def _type_pprint(obj, p, cycle):
682678
"""The pprint for classes and types."""
683679
# Heap allocated types might not have the module attribute,
684680
# and others may set it to None.
681+
682+
# Checks for a __repr__ override in the metaclass
683+
if type(obj).__repr__ is not type.__repr__:
684+
_repr_pprint(obj, p, cycle)
685+
return
686+
685687
mod = _safe_getattr(obj, '__module__', None)
686688
name = _safe_getattr(obj, '__qualname__', obj.__name__)
687689

@@ -693,7 +695,12 @@ def _type_pprint(obj, p, cycle):
693695

694696
def _repr_pprint(obj, p, cycle):
695697
"""A pprint that just redirects to the normal repr function."""
696-
p.text(repr(obj))
698+
# Find newlines and replace them with p.break_()
699+
output = repr(obj)
700+
for idx,output_line in enumerate(output.splitlines()):
701+
if idx:
702+
p.break_()
703+
p.text(output_line)
697704

698705

699706
def _function_pprint(obj, p, cycle):

IPython/lib/tests/test_pretty.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
# Our own imports
1212
from IPython.lib import pretty
1313
from IPython.testing.decorators import skip_without
14+
from IPython.utils.py3compat import PY3
15+
16+
if PY3:
17+
from io import StringIO
18+
else:
19+
from StringIO import StringIO
1420

1521

1622
class MyList(object):
@@ -213,4 +219,38 @@ def test_long_dict():
213219

214220
def test_unbound_method():
215221
output = pretty.pretty(MyObj.somemethod)
216-
nt.assert_in('MyObj.somemethod', output)
222+
nt.assert_in('MyObj.somemethod', output)
223+
224+
225+
class MetaClass(type):
226+
def __new__(cls, name):
227+
return type.__new__(cls, name, (object,), {'name': name})
228+
229+
def __repr__(self):
230+
return "[CUSTOM REPR FOR CLASS %s]" % self.name
231+
232+
233+
ClassWithMeta = MetaClass('ClassWithMeta')
234+
235+
236+
def test_metaclass_repr():
237+
output = pretty.pretty(ClassWithMeta)
238+
nt.assert_equal(output, "[CUSTOM REPR FOR CLASS ClassWithMeta]")
239+
240+
241+
def test_basic_class():
242+
def type_pprint_wrapper(obj, p, cycle):
243+
if obj is MyObj:
244+
type_pprint_wrapper.called = True
245+
return pretty._type_pprint(obj, p, cycle)
246+
type_pprint_wrapper.called = False
247+
248+
stream = StringIO()
249+
printer = pretty.RepresentationPrinter(stream)
250+
printer.type_pprinters[type] = type_pprint_wrapper
251+
printer.pretty(MyObj)
252+
printer.flush()
253+
output = stream.getvalue()
254+
255+
nt.assert_equal(output, '%s.MyObj' % __name__)
256+
nt.assert_true(type_pprint_wrapper.called)

0 commit comments

Comments
 (0)