MCPcopy Index your code
hub / github.com/python/cpython / _pprint_str

Method _pprint_str

Lib/pprint.py:404–457  ·  view source on GitHub ↗
(self, object, stream, indent, allowance, context, level)

Source from the content-addressed store, hash-verified

402 _dispatch[frozenset.__repr__] = _pprint_set
403
404 def _pprint_str(self, object, stream, indent, allowance, context, level):
405 write = stream.write
406 if not len(object):
407 write(repr(object))
408 return
409 chunks = []
410 lines = object.splitlines(True)
411 if level == 1:
412 if self._expand:
413 indent += self._indent_per_level
414 else:
415 indent += 1
416 allowance += 1
417 max_width1 = max_width = self._width - indent
418 for i, line in enumerate(lines):
419 rep = repr(line)
420 if i == len(lines) - 1:
421 max_width1 -= allowance
422 if len(rep) <= max_width1:
423 chunks.append(rep)
424 else:
425 # Lazy import to improve module import time
426 import re
427
428 # A list of alternating (non-space, space) strings
429 parts = re.findall(r'\S*\s*', line)
430 assert parts
431 assert not parts[-1]
432 parts.pop() # drop empty last part
433 max_width2 = max_width
434 current = ''
435 for j, part in enumerate(parts):
436 candidate = current + part
437 if j == len(parts) - 1 and i == len(lines) - 1:
438 max_width2 -= allowance
439 if len(repr(candidate)) > max_width2:
440 if current:
441 chunks.append(repr(current))
442 current = part
443 else:
444 current = candidate
445 if current:
446 chunks.append(repr(current))
447 if len(chunks) == 1:
448 write(rep)
449 return
450 if level == 1:
451 write(self._format_block_start("(", indent))
452 for i, rep in enumerate(chunks):
453 if i > 0:
454 write('\n' + ' '*indent)
455 write(rep)
456 if level == 1:
457 write(self._format_block_end(")", indent - self._indent_per_level))
458
459 _dispatch[str.__repr__] = _pprint_str
460

Callers

nothing calls this directly

Calls 8

_format_block_startMethod · 0.95
_format_block_endMethod · 0.95
enumerateFunction · 0.85
writeFunction · 0.70
splitlinesMethod · 0.45
appendMethod · 0.45
findallMethod · 0.45
popMethod · 0.45

Tested by

no test coverage detected