MCPcopy
hub / github.com/pytest-dev/pytest / _write_source

Method _write_source

src/_pytest/_io/terminalwriter.py:189–206  ·  view source on GitHub ↗

Write lines of source code possibly highlighted. Keeping this private for now because the API is clunky. We should discuss how to evolve the terminal writer so we can have more precise color support, for example being able to write part of a line in one color and the rest in

(self, lines: Sequence[str], indents: Sequence[str] = ())

Source from the content-addressed store, hash-verified

187 self._file.flush()
188
189 def _write_source(self, lines: Sequence[str], indents: Sequence[str] = ()) -> None:
190 """Write lines of source code possibly highlighted.
191
192 Keeping this private for now because the API is clunky. We should discuss how
193 to evolve the terminal writer so we can have more precise color support, for example
194 being able to write part of a line in one color and the rest in another, and so on.
195 """
196 if indents and len(indents) != len(lines):
197 raise ValueError(
198 f"indents size ({len(indents)}) should have same size as lines ({len(lines)})"
199 )
200 if not indents:
201 indents = [""] * len(lines)
202 source = "\n".join(lines)
203 new_lines = self._highlight(source).splitlines()
204 # Would be better to strict=True but that fails some CI jobs.
205 for indent, new_line in zip(indents, new_lines, strict=False):
206 self.line(indent + new_line)
207
208 def _get_pygments_lexer(self, lexer: Literal["python", "diff"]) -> Lexer:
209 if lexer == "python":

Callers 3

test_code_highlightFunction · 0.95
_write_entry_linesMethod · 0.45

Calls 3

_highlightMethod · 0.95
lineMethod · 0.95
joinMethod · 0.80

Tested by 2

test_code_highlightFunction · 0.76