MCPcopy Create free account
hub / github.com/pytest-dev/pytest / _write_entry_lines

Method _write_entry_lines

src/_pytest/_code/code.py:1081–1123  ·  view source on GitHub ↗

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 i

(self, tw: TerminalWriter)

Source from the content-addressed store, hash-verified

1079 style: "_TracebackStyle"
1080
1081 def _write_entry_lines(self, tw: TerminalWriter) -> None:
1082 """Write the source code portions of a list of traceback entries with syntax highlighting.
1083
1084 Usually entries are lines like these:
1085
1086 " x = 1"
1087 "> assert x == 2"
1088 "E assert 1 == 2"
1089
1090 This function takes care of rendering the "source" portions of it (the lines without
1091 the "E" prefix) using syntax highlighting, taking care to not highlighting the ">"
1092 character, as doing so might break line continuations.
1093 """
1094
1095 if not self.lines:
1096 return
1097
1098 # separate indents and source lines that are not failures: we want to
1099 # highlight the code but not the indentation, which may contain markers
1100 # such as "> assert 0"
1101 fail_marker = f"{FormattedExcinfo.fail_marker} "
1102 indent_size = len(fail_marker)
1103 indents: List[str] = []
1104 source_lines: List[str] = []
1105 failure_lines: List[str] = []
1106 for index, line in enumerate(self.lines):
1107 is_failure_line = line.startswith(fail_marker)
1108 if is_failure_line:
1109 # from this point on all lines are considered part of the failure
1110 failure_lines.extend(self.lines[index:])
1111 break
1112 else:
1113 if self.style == "value":
1114 source_lines.append(line)
1115 else:
1116 indents.append(line[:indent_size])
1117 source_lines.append(line[indent_size:])
1118
1119 tw._write_source(source_lines, indents)
1120
1121 # failure lines are always completely red and bold
1122 for line in failure_lines:
1123 tw.line(line, bold=True, red=True)
1124
1125 def toterminal(self, tw: TerminalWriter) -> None:
1126 if self.style == "short":

Callers 1

toterminalMethod · 0.95

Calls 3

appendMethod · 0.80
_write_sourceMethod · 0.45
lineMethod · 0.45

Tested by

no test coverage detected