From a5c4ee47dba62ff1061faec1aef412e180cdd292 2013-07-31 01:41:38 From: Alex Rudy Date: 2013-07-31 01:41:38 Subject: [PATCH] Uses p.break_() when repr() is used for printing When a newline is found in the result of repr() (using the default printer), the newline is replaced with an appropriate call to p.break_() --- diff --git a/IPython/lib/pretty.py b/IPython/lib/pretty.py index ee5e059..2fee84c 100644 --- a/IPython/lib/pretty.py +++ b/IPython/lib/pretty.py @@ -488,8 +488,12 @@ def _default_pprint(obj, p, cycle): """ klass = getattr(obj, '__class__', None) or type(obj) if getattr(klass, '__repr__', None) not in _baseclass_reprs: - # A user-provided repr. - p.text(repr(obj)) + # A user-provided repr. Find newlines and replace them with p.break_() + output = repr(obj) + for idx,output_line in enumerate(output.splitlines()): + if idx: + p.break_() + p.text(output_line) return p.begin_group(1, '<') p.pretty(klass) diff --git a/IPython/lib/tests/test_pretty.py b/IPython/lib/tests/test_pretty.py index db5224c..2e0aaab 100644 --- a/IPython/lib/tests/test_pretty.py +++ b/IPython/lib/tests/test_pretty.py @@ -65,6 +65,16 @@ class Breaking(object): p.break_() p.text(")") +class BreakingRepr(object): + def __repr__(self): + return "Breaking(\n)" + +class BreakingReprParent(object): + def _repr_pretty_(self, p, cycle): + with p.group(4,"TG: ",":"): + p.pretty(BreakingRepr()) + + def test_indentation(): """Test correct indentation in groups""" @@ -133,3 +143,11 @@ def test_pprint_break(): output = pretty.pretty(Breaking()) expected = "TG: Breaking(\n ):" nt.assert_equal(output, expected) + +def test_pprint_break_repr(): + """ + Test that p.break_ is used in repr + """ + output = pretty.pretty(BreakingReprParent()) + expected = "TG: Breaking(\n ):" + nt.assert_equal(output, expected) \ No newline at end of file