From d7cd43d219d57fbf2050a29e4ee2a8262f942679 2020-01-31 22:25:57 From: Matthias Bussonnier Date: 2020-01-31 22:25:57 Subject: [PATCH] Merge pull request #12099 from eric-wieser/patch-1 Start multi-line __repr__s on their own line --- diff --git a/IPython/lib/pretty.py b/IPython/lib/pretty.py index 49c76ad..50cec70 100644 --- a/IPython/lib/pretty.py +++ b/IPython/lib/pretty.py @@ -196,19 +196,22 @@ class PrettyPrinter(_PrettyPrinterBase): self.group_queue = GroupQueue(root_group) self.indentation = 0 + def _break_one_group(self, group): + while group.breakables: + x = self.buffer.popleft() + self.output_width = x.output(self.output, self.output_width) + self.buffer_width -= x.width + while self.buffer and isinstance(self.buffer[0], Text): + x = self.buffer.popleft() + self.output_width = x.output(self.output, self.output_width) + self.buffer_width -= x.width + def _break_outer_groups(self): while self.max_width < self.output_width + self.buffer_width: group = self.group_queue.deq() if not group: return - while group.breakables: - x = self.buffer.popleft() - self.output_width = x.output(self.output, self.output_width) - self.buffer_width -= x.width - while self.buffer and isinstance(self.buffer[0], Text): - x = self.buffer.popleft() - self.output_width = x.output(self.output, self.output_width) - self.buffer_width -= x.width + self._break_one_group(group) def text(self, obj): """Add literal text to the output.""" @@ -248,6 +251,9 @@ class PrettyPrinter(_PrettyPrinterBase): """ Explicitly insert a newline into the output, maintaining correct indentation. """ + group = self.group_queue.deq() + if group: + self._break_one_group(group) self.flush() self.output.write(self.newline) self.output.write(' ' * self.indentation) @@ -687,10 +693,12 @@ def _repr_pprint(obj, p, cycle): """A pprint that just redirects to the normal repr function.""" # 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) + lines = output.splitlines() + with p.group(): + for idx, output_line in enumerate(lines): + if idx: + p.break_() + p.text(output_line) def _function_pprint(obj, p, cycle): diff --git a/IPython/lib/tests/test_pretty.py b/IPython/lib/tests/test_pretty.py index cd9fb36..695012d 100644 --- a/IPython/lib/tests/test_pretty.py +++ b/IPython/lib/tests/test_pretty.py @@ -68,11 +68,6 @@ 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()) - class BadRepr(object): def __repr__(self): @@ -151,8 +146,12 @@ def test_pprint_break_repr(): """ Test that p.break_ is used in repr """ - output = pretty.pretty(BreakingReprParent()) - expected = "TG: Breaking(\n ):" + output = pretty.pretty([[BreakingRepr()]]) + expected = "[[Breaking(\n )]]" + nt.assert_equal(output, expected) + + output = pretty.pretty([[BreakingRepr()]*2]) + expected = "[[Breaking(\n ),\n Breaking(\n )]]" nt.assert_equal(output, expected) def test_bad_repr():