| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -235,35 +235,41 @@ def _format(self, object, stream, indent, allowance, context, level): | |||
| 235 | 235 | return | |
| 236 | 236 | ||
| 237 | 237 | if issubclass(typ, str) and len(object) > 0 and r is str.__repr__: | |
| 238 | - def _str_parts(s): | ||
| 239 | - """ | ||
| 240 | - Return a list of string literals comprising the repr() | ||
| 241 | - of the given string using literal concatenation. | ||
| 242 | - """ | ||
| 243 | - lines = s.splitlines(True) | ||
| 244 | - for i, line in enumerate(lines): | ||
| 245 | - rep = repr(line) | ||
| 246 | - if len(rep) <= max_width: | ||
| 247 | - yield rep | ||
| 248 | - else: | ||
| 249 | - # A list of alternating (non-space, space) strings | ||
| 250 | - parts = re.split(r'(\s+)', line) + [''] | ||
| 251 | - current = '' | ||
| 252 | - for i in range(0, len(parts), 2): | ||
| 253 | - part = parts[i] + parts[i+1] | ||
| 254 | - candidate = current + part | ||
| 255 | - if len(repr(candidate)) > max_width: | ||
| 256 | - if current: | ||
| 257 | - yield repr(current) | ||
| 258 | - current = part | ||
| 259 | - else: | ||
| 260 | - current = candidate | ||
| 261 | - if current: | ||
| 262 | - yield repr(current) | ||
| 263 | - for i, rep in enumerate(_str_parts(object)): | ||
| 238 | + chunks = [] | ||
| 239 | + lines = object.splitlines(True) | ||
| 240 | + if level == 1: | ||
| 241 | + indent += 1 | ||
| 242 | + max_width -= 2 | ||
| 243 | + for i, line in enumerate(lines): | ||
| 244 | + rep = repr(line) | ||
| 245 | + if len(rep) <= max_width: | ||
| 246 | + chunks.append(rep) | ||
| 247 | + else: | ||
| 248 | + # A list of alternating (non-space, space) strings | ||
| 249 | + parts = re.split(r'(\s+)', line) + [''] | ||
| 250 | + current = '' | ||
| 251 | + for i in range(0, len(parts), 2): | ||
| 252 | + part = parts[i] + parts[i+1] | ||
| 253 | + candidate = current + part | ||
| 254 | + if len(repr(candidate)) > max_width: | ||
| 255 | + if current: | ||
| 256 | + chunks.append(repr(current)) | ||
| 257 | + current = part | ||
| 258 | + else: | ||
| 259 | + current = candidate | ||
| 260 | + if current: | ||
| 261 | + chunks.append(repr(current)) | ||
| 262 | + if len(chunks) == 1: | ||
| 263 | + write(rep) | ||
| 264 | + return | ||
| 265 | + if level == 1: | ||
| 266 | + write('(') | ||
| 267 | + for i, rep in enumerate(chunks): | ||
| 264 | 268 | if i > 0: | |
| 265 | 269 | write('\n' + ' '*indent) | |
| 266 | 270 | write(rep) | |
| 271 | + if level == 1: | ||
| 272 | + write(')') | ||
| 267 | 273 | return | |
| 268 | 274 | write(rep) | |
| 269 | 275 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -536,9 +536,10 @@ def test_str_wrap(self): | |||
| 536 | 536 | # pprint tries to wrap strings intelligently | |
| 537 | 537 | fox = 'the quick brown fox jumped over a lazy dog' | |
| 538 | 538 | self.assertEqual(pprint.pformat(fox, width=20), """\ | |
| 539 | - 'the quick brown ' | ||
| 540 | - 'fox jumped over ' | ||
| 541 | - 'a lazy dog'""") | ||
| 539 | + ('the quick ' | ||
| 540 | + 'brown fox ' | ||
| 541 | + 'jumped over a ' | ||
| 542 | + 'lazy dog')""") | ||
| 542 | 543 | self.assertEqual(pprint.pformat({'a': 1, 'b': fox, 'c': 2}, | |
| 543 | 544 | width=26), """\ | |
| 544 | 545 | {'a': 1, | |
@@ -552,12 +553,12 @@ def test_str_wrap(self): | |||
| 552 | 553 | # - non-ASCII is allowed | |
| 553 | 554 | # - an apostrophe doesn't disrupt the pprint | |
| 554 | 555 | special = "Portons dix bons \"whiskys\"\nà l'avocat goujat\t qui fumait au zoo" | |
| 555 | - self.assertEqual(pprint.pformat(special, width=20), """\ | ||
| 556 | - 'Portons dix bons ' | ||
| 557 | - '"whiskys"\\n' | ||
| 558 | - "à l'avocat " | ||
| 559 | - 'goujat\\t qui ' | ||
| 560 | - 'fumait au zoo'""") | ||
| 556 | + self.assertEqual(pprint.pformat(special, width=21), """\ | ||
| 557 | + ('Portons dix ' | ||
| 558 | + 'bons "whiskys"\\n' | ||
| 559 | + "à l'avocat " | ||
| 560 | + 'goujat\\t qui ' | ||
| 561 | + 'fumait au zoo')""") | ||
| 561 | 562 | # An unwrappable string is formatted as its repr | |
| 562 | 563 | unwrappable = "x" * 100 | |
| 563 | 564 | self.assertEqual(pprint.pformat(unwrappable, width=80), repr(unwrappable)) | |
@@ -566,7 +567,9 @@ def test_str_wrap(self): | |||
| 566 | 567 | special *= 10 | |
| 567 | 568 | for width in range(3, 40): | |
| 568 | 569 | formatted = pprint.pformat(special, width=width) | |
| 569 | - self.assertEqual(eval("(" + formatted + ")"), special) | ||
| 570 | + self.assertEqual(eval(formatted), special) | ||
| 571 | + formatted = pprint.pformat([special] * 2, width=width) | ||
| 572 | + self.assertEqual(eval(formatted), [special] * 2) | ||
| 570 | 573 | ||
| 571 | 574 | def test_compact(self): | |
| 572 | 575 | o = ([list(range(i * i)) for i in range(5)] + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -41,6 +41,8 @@ Core and Builtins | |||
| 41 | 41 | Library | |
| 42 | 42 | ------- | |
| 43 | 43 | ||
| 44 | + - Issue #19104: pprint now produces evaluable output for wrapped strings. | ||
| 45 | + | ||
| 44 | 46 | - Issue #23071: Added missing names to codecs.__all__. Patch by Martin Panter. | |
| 45 | 47 | ||
| 46 | 48 | - Issue #15513: Added a __sizeof__ implementation for pickle classes. | |
| Back | FazBrowse Home | New Git URL |
0 commit comments