Show More
@@ -196,19 +196,22 b' class PrettyPrinter(_PrettyPrinterBase):' | |||||
196 | self.group_queue = GroupQueue(root_group) |
|
196 | self.group_queue = GroupQueue(root_group) | |
197 | self.indentation = 0 |
|
197 | self.indentation = 0 | |
198 |
|
198 | |||
|
199 | def _break_one_group(self, group): | |||
|
200 | while group.breakables: | |||
|
201 | x = self.buffer.popleft() | |||
|
202 | self.output_width = x.output(self.output, self.output_width) | |||
|
203 | self.buffer_width -= x.width | |||
|
204 | while self.buffer and isinstance(self.buffer[0], Text): | |||
|
205 | x = self.buffer.popleft() | |||
|
206 | self.output_width = x.output(self.output, self.output_width) | |||
|
207 | self.buffer_width -= x.width | |||
|
208 | ||||
199 | def _break_outer_groups(self): |
|
209 | def _break_outer_groups(self): | |
200 | while self.max_width < self.output_width + self.buffer_width: |
|
210 | while self.max_width < self.output_width + self.buffer_width: | |
201 | group = self.group_queue.deq() |
|
211 | group = self.group_queue.deq() | |
202 | if not group: |
|
212 | if not group: | |
203 | return |
|
213 | return | |
204 | while group.breakables: |
|
214 | self._break_one_group(group) | |
205 | x = self.buffer.popleft() |
|
|||
206 | self.output_width = x.output(self.output, self.output_width) |
|
|||
207 | self.buffer_width -= x.width |
|
|||
208 | while self.buffer and isinstance(self.buffer[0], Text): |
|
|||
209 | x = self.buffer.popleft() |
|
|||
210 | self.output_width = x.output(self.output, self.output_width) |
|
|||
211 | self.buffer_width -= x.width |
|
|||
212 |
|
215 | |||
213 | def text(self, obj): |
|
216 | def text(self, obj): | |
214 | """Add literal text to the output.""" |
|
217 | """Add literal text to the output.""" | |
@@ -248,6 +251,9 b' class PrettyPrinter(_PrettyPrinterBase):' | |||||
248 | """ |
|
251 | """ | |
249 | Explicitly insert a newline into the output, maintaining correct indentation. |
|
252 | Explicitly insert a newline into the output, maintaining correct indentation. | |
250 | """ |
|
253 | """ | |
|
254 | group = self.group_queue.deq() | |||
|
255 | if group: | |||
|
256 | self._break_one_group(group) | |||
251 | self.flush() |
|
257 | self.flush() | |
252 | self.output.write(self.newline) |
|
258 | self.output.write(self.newline) | |
253 | self.output.write(' ' * self.indentation) |
|
259 | self.output.write(' ' * self.indentation) | |
@@ -687,10 +693,12 b' def _repr_pprint(obj, p, cycle):' | |||||
687 | """A pprint that just redirects to the normal repr function.""" |
|
693 | """A pprint that just redirects to the normal repr function.""" | |
688 | # Find newlines and replace them with p.break_() |
|
694 | # Find newlines and replace them with p.break_() | |
689 | output = repr(obj) |
|
695 | output = repr(obj) | |
690 |
|
|
696 | lines = output.splitlines() | |
691 | if idx: |
|
697 | with p.group(): | |
692 | p.break_() |
|
698 | for idx, output_line in enumerate(lines): | |
693 | p.text(output_line) |
|
699 | if idx: | |
|
700 | p.break_() | |||
|
701 | p.text(output_line) | |||
694 |
|
702 | |||
695 |
|
703 | |||
696 | def _function_pprint(obj, p, cycle): |
|
704 | def _function_pprint(obj, p, cycle): |
@@ -68,11 +68,6 b' class BreakingRepr(object):' | |||||
68 | def __repr__(self): |
|
68 | def __repr__(self): | |
69 | return "Breaking(\n)" |
|
69 | return "Breaking(\n)" | |
70 |
|
70 | |||
71 | class BreakingReprParent(object): |
|
|||
72 | def _repr_pretty_(self, p, cycle): |
|
|||
73 | with p.group(4,"TG: ",":"): |
|
|||
74 | p.pretty(BreakingRepr()) |
|
|||
75 |
|
||||
76 | class BadRepr(object): |
|
71 | class BadRepr(object): | |
77 |
|
72 | |||
78 | def __repr__(self): |
|
73 | def __repr__(self): | |
@@ -151,8 +146,12 b' def test_pprint_break_repr():' | |||||
151 | """ |
|
146 | """ | |
152 | Test that p.break_ is used in repr |
|
147 | Test that p.break_ is used in repr | |
153 | """ |
|
148 | """ | |
154 |
output = pretty.pretty(BreakingRepr |
|
149 | output = pretty.pretty([[BreakingRepr()]]) | |
155 |
expected = " |
|
150 | expected = "[[Breaking(\n )]]" | |
|
151 | nt.assert_equal(output, expected) | |||
|
152 | ||||
|
153 | output = pretty.pretty([[BreakingRepr()]*2]) | |||
|
154 | expected = "[[Breaking(\n ),\n Breaking(\n )]]" | |||
156 | nt.assert_equal(output, expected) |
|
155 | nt.assert_equal(output, expected) | |
157 |
|
156 | |||
158 | def test_bad_repr(): |
|
157 | def test_bad_repr(): |
General Comments 0
You need to be logged in to leave comments.
Login now