| 286 | _dispatch[frozenset.__repr__] = _pprint_set |
| 287 | |
| 288 | def _pprint_str(self, object, stream, indent, allowance, context, level): |
| 289 | write = stream.write |
| 290 | if not len(object): |
| 291 | write(repr(object)) |
| 292 | return |
| 293 | chunks = [] |
| 294 | lines = object.splitlines(True) |
| 295 | if level == 1: |
| 296 | indent += 1 |
| 297 | allowance += 1 |
| 298 | max_width1 = max_width = self._width - indent |
| 299 | for i, line in enumerate(lines): |
| 300 | rep = repr(line) |
| 301 | if i == len(lines) - 1: |
| 302 | max_width1 -= allowance |
| 303 | if len(rep) <= max_width1: |
| 304 | chunks.append(rep) |
| 305 | else: |
| 306 | # Lazy import to improve module import time |
| 307 | import re |
| 308 | |
| 309 | # A list of alternating (non-space, space) strings |
| 310 | parts = re.findall(r'\S*\s*', line) |
| 311 | assert parts |
| 312 | assert not parts[-1] |
| 313 | parts.pop() # drop empty last part |
| 314 | max_width2 = max_width |
| 315 | current = '' |
| 316 | for j, part in enumerate(parts): |
| 317 | candidate = current + part |
| 318 | if j == len(parts) - 1 and i == len(lines) - 1: |
| 319 | max_width2 -= allowance |
| 320 | if len(repr(candidate)) > max_width2: |
| 321 | if current: |
| 322 | chunks.append(repr(current)) |
| 323 | current = part |
| 324 | else: |
| 325 | current = candidate |
| 326 | if current: |
| 327 | chunks.append(repr(current)) |
| 328 | if len(chunks) == 1: |
| 329 | write(rep) |
| 330 | return |
| 331 | if level == 1: |
| 332 | write('(') |
| 333 | for i, rep in enumerate(chunks): |
| 334 | if i > 0: |
| 335 | write('\n' + ' '*indent) |
| 336 | write(rep) |
| 337 | if level == 1: |
| 338 | write(')') |
| 339 | |
| 340 | _dispatch[str.__repr__] = _pprint_str |
| 341 | |