Repository: pytest-dev/pytest
File: src/_pytest/_code/code.py (Python)

Code before:
    reprlocals: ReprLocals | None
    reprfileloc: ReprFileLocation | None
    style: TracebackStyle

    def _write_entry_lines(self, tw: TerminalWriter) -> None:
        """Write the source code portions of a list of traceback entries with syntax highlighting.

        Usually entries are lines like these:

            "     x = 1"
            ">    assert x == 2"
            "E    assert 1 == 2"

        This function takes care of rendering the "source" portions of it (the lines without
        the "E" prefix) using syntax highlighting, taking care to not highlighting the ">"
        character, as doing so might break line continuations.
        """
        if not self.lines:
            return

        if self.style == "value":
            for line in self.lines:
                tw.write(line)
                tw.write("\n")
            return

        # separate indents and source lines that are not failures: we want to
        # highlight the code but not the indentation, which may contain markers
        # such as ">   assert 0"
        fail_marker = f"{FormattedExcinfo.fail_marker}   "
        indent_size = len(fail_marker)
        indents: list[str] = []
        source_lines: list[str] = []
        failure_lines: list[str] = []
        for index, line in enumerate(self.lines):
            is_failure_line = line.startswith(fail_marker)
            if is_failure_line:
                # from this point on all lines are considered part of the failure
                failure_lines.extend(self.lines[index:])
                break
            else:
                indents.append(line[:indent_size])
                source_lines.append(line[indent_size:])

        tw._write_source(source_lines, indents)

        # failure lines are always completely red and bold
        for line in failure_lines:
            tw.line(line, bold=True, red=True)

    def toterminal(self, tw: TerminalWriter) -> None:

Review comment (suggestion):
Using `TerminalWriter.write` instead of `TerminalWriter.line` because when using `TWMock` . we can't distinguish lines written with `TWMock._write_source` and lines written with `line` directly, whereas with `TWMock.write` we can, with the flag.

We could add a flag for `TWMock._write_source` as well, like so:
```python
class TWMock:
    WRITE = object()
    WRITE_SOURCE = object()

    def _write_source(self, lines, indents=()):
        if not indents:
            indents = [""] * len(lines)
        for indent, line in zip(indents, lines):
            newline = indent + line
            self.line((TWMock.WRITE_SOURCE, newline))
```

But then we'll have to change around half the test cases in `test_excinfo.py`

Code after:
    reprlocals: ReprLocals | None
    reprfileloc: ReprFileLocation | None
    style: TracebackStyle

    def _write_entry_lines(self, tw: TerminalWriter) -> None:
        """Write the source code portions of a list of traceback entries with syntax highlighting.

        Usually entries are lines like these:

            "     x = 1"
            ">    assert x == 2"
            "E    assert 1 == 2"

        This function takes care of rendering the "source" portions of it (the lines without
        the "E" prefix) using syntax highlighting, taking care to not highlighting the ">"
        character, as doing so might break line continuations.
        """
        if not self.lines:
            return

        if self.style == "value":
            # Using tw.write instead of tw.line for testing purposes due to TWMock implementation;
            # lines written with TWMock.line and TWMock._write_source cannot be distinguished
            # from each other, whereas lines written with TWMock.write are marked with TWMock.WRITE
            for line in self.lines:
                tw.write(line)
                tw.write("\n")
            return

        # separate indents and source lines that are not failures: we want to
        # highlight the code but not the indentation, which may contain markers
        # such as ">   assert 0"
        fail_marker = f"{FormattedExcinfo.fail_marker}   "
        indent_size = len(fail_marker)
        indents: list[str] = []
        source_lines: list[str] = []
        failure_lines: list[str] = []
        for index, line in enumerate(self.lines):
            is_failure_line = line.startswith(fail_marker)
            if is_failure_line:
                # from this point on all lines are considered part of the failure
                failure_lines.extend(self.lines[index:])
                break
            else:
                indents.append(line[:indent_size])
                source_lines.append(line[indent_size:])

        tw._write_source(source_lines, indents)

        # failure lines are always completely red and bold
        for line in failure_lines: