MCPcopy Index your code
hub / github.com/ipython/ipython / format2

Method format2

IPython/utils/PyColorize.py:450–537  ·  view source on GitHub ↗

Parse and send the colored source. If out is not specified, the defaults (given to constructor) are used. out should be a file-type object. Optionally, out can be given as the string 'str' and the parser will automatically return the output in a string.

(self, raw: str, out: Any = None)

Source from the content-addressed store, hash-verified

448 return self.format2(raw, out)[0]
449
450 def format2(self, raw: str, out: Any = None) -> tuple[str | None, bool]:
451 """Parse and send the colored source.
452
453 If out is not specified, the defaults (given to constructor) are used.
454
455 out should be a file-type object. Optionally, out can be given as the
456 string 'str' and the parser will automatically return the output in a
457 string."""
458
459 string_output = 0
460 if out == "str" or self.out == "str" or isinstance(self.out, StringIO):
461 # XXX - I don't really like this state handling logic, but at this
462 # point I don't want to make major changes, so adding the
463 # isinstance() check is the simplest I can do to ensure correct
464 # behavior.
465 out_old = self.out
466 self.out = StringIO()
467 string_output = 1
468 elif out is not None:
469 self.out = out
470 else:
471 raise ValueError(
472 '`out` or `self.out` should be file-like or the value `"str"`'
473 )
474
475 # Fast return of the unmodified input for nocolor scheme
476 # TODO:
477 if self.theme_name == "nocolor":
478 error = False
479 self.out.write(raw)
480 if string_output:
481 return raw, error
482 return None, error
483
484 # local shorthands
485
486 # Remove trailing whitespace and normalize tabs
487 self.raw = raw.expandtabs().rstrip()
488
489 # store line offsets in self.lines
490 self.lines = [0, 0]
491 pos = 0
492 raw_find = self.raw.find
493 lines_append = self.lines.append
494 while True:
495 pos = raw_find("\n", pos) + 1
496 if not pos:
497 break
498 lines_append(pos)
499 lines_append(len(self.raw))
500
501 # parse the source and write it
502 self.pos = 0
503 text = StringIO(self.raw)
504
505 error = False
506 try:
507 for atoken in generate_tokens(text.readline):

Callers 2

formatMethod · 0.95
__line_contentMethod · 0.80

Calls 3

generate_tokensFunction · 0.85
writeMethod · 0.45
formatMethod · 0.45

Tested by

no test coverage detected