Show More
@@ -229,7 +229,17 b' class PrettyPrinter(_PrettyPrinterBase):' | |||
|
229 | 229 | self.buffer.append(Breakable(sep, width, self)) |
|
230 | 230 | self.buffer_width += width |
|
231 | 231 | self._break_outer_groups() |
|
232 | ||
|
232 | ||
|
233 | def break_(self): | |
|
234 | """ | |
|
235 | Explicitly insert a newline into the output, maintaining correct indentation. | |
|
236 | """ | |
|
237 | self.flush() | |
|
238 | self.output.write(self.newline) | |
|
239 | self.output.write(' ' * self.indentation) | |
|
240 | self.output_width = self.indentation | |
|
241 | self.buffer_width = 0 | |
|
242 | ||
|
233 | 243 | |
|
234 | 244 | def begin_group(self, indent=0, open=''): |
|
235 | 245 | """ |
@@ -478,8 +488,12 b' def _default_pprint(obj, p, cycle):' | |||
|
478 | 488 | """ |
|
479 | 489 | klass = getattr(obj, '__class__', None) or type(obj) |
|
480 | 490 | if getattr(klass, '__repr__', None) not in _baseclass_reprs: |
|
481 | # A user-provided repr. | |
|
482 |
|
|
|
491 | # A user-provided repr. Find newlines and replace them with p.break_() | |
|
492 | output = repr(obj) | |
|
493 | for idx,output_line in enumerate(output.splitlines()): | |
|
494 | if idx: | |
|
495 | p.break_() | |
|
496 | p.text(output_line) | |
|
483 | 497 | return |
|
484 | 498 | p.begin_group(1, '<') |
|
485 | 499 | p.pretty(klass) |
@@ -58,6 +58,23 b' class NoModule(object):' | |||
|
58 | 58 | |
|
59 | 59 | NoModule.__module__ = None |
|
60 | 60 | |
|
61 | class Breaking(object): | |
|
62 | def _repr_pretty_(self, p, cycle): | |
|
63 | with p.group(4,"TG: ",":"): | |
|
64 | p.text("Breaking(") | |
|
65 | p.break_() | |
|
66 | p.text(")") | |
|
67 | ||
|
68 | class BreakingRepr(object): | |
|
69 | def __repr__(self): | |
|
70 | return "Breaking(\n)" | |
|
71 | ||
|
72 | class BreakingReprParent(object): | |
|
73 | def _repr_pretty_(self, p, cycle): | |
|
74 | with p.group(4,"TG: ",":"): | |
|
75 | p.pretty(BreakingRepr()) | |
|
76 | ||
|
77 | ||
|
61 | 78 | |
|
62 | 79 | def test_indentation(): |
|
63 | 80 | """Test correct indentation in groups""" |
@@ -118,3 +135,19 b' def test_pprint_nomod():' | |||
|
118 | 135 | """ |
|
119 | 136 | output = pretty.pretty(NoModule) |
|
120 | 137 | nt.assert_equal(output, 'NoModule') |
|
138 | ||
|
139 | def test_pprint_break(): | |
|
140 | """ | |
|
141 | Test that p.break_ produces expected output | |
|
142 | """ | |
|
143 | output = pretty.pretty(Breaking()) | |
|
144 | expected = "TG: Breaking(\n ):" | |
|
145 | nt.assert_equal(output, expected) | |
|
146 | ||
|
147 | def test_pprint_break_repr(): | |
|
148 | """ | |
|
149 | Test that p.break_ is used in repr | |
|
150 | """ | |
|
151 | output = pretty.pretty(BreakingReprParent()) | |
|
152 | expected = "TG: Breaking(\n ):" | |
|
153 | nt.assert_equal(output, expected) No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now