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

Class ReprEntry

src/_pytest/_code/code.py:1395–1479  ·  view source on GitHub ↗

An entry in a traceback with possible extra details (function arguments, function locals, source snippet, function's file and line).

Source from the content-addressed store, hash-verified

1393
1394@dataclasses.dataclass(eq=False)
1395class ReprEntry(TerminalRepr):
1396 """An entry in a traceback with possible extra details (function arguments,
1397 function locals, source snippet, function's file and line)."""
1398
1399 lines: Sequence[str]
1400 reprfuncargs: ReprFuncArgs | None
1401 reprlocals: ReprLocals | None
1402 reprfileloc: ReprFileLocation | None
1403 style: TracebackStyle
1404
1405 def _write_entry_lines(self, tw: TerminalWriter) -> None:
1406 """Write the source code portions of a list of traceback entries with syntax highlighting.
1407
1408 Usually entries are lines like these:
1409
1410 " x = 1"
1411 "> assert x == 2"
1412 "E assert 1 == 2"
1413
1414 This function takes care of rendering the "source" portions of it (the lines without
1415 the "E" prefix) using syntax highlighting, taking care to not highlighting the ">"
1416 character, as doing so might break line continuations.
1417 """
1418 if not self.lines:
1419 return
1420
1421 if self.style == "value":
1422 # Using tw.write instead of tw.line for testing purposes due to TWMock implementation;
1423 # lines written with TWMock.line and TWMock._write_source cannot be distinguished
1424 # from each other, whereas lines written with TWMock.write are marked with TWMock.WRITE
1425 for line in self.lines:
1426 tw.write(line)
1427 tw.write("\n")
1428 return
1429
1430 # separate indents and source lines that are not failures: we want to
1431 # highlight the code but not the indentation, which may contain markers
1432 # such as "> assert 0"
1433 fail_marker = f"{ExceptionInfoFormatter.fail_marker} "
1434 indent_size = len(fail_marker)
1435 indents: list[str] = []
1436 source_lines: list[str] = []
1437 failure_lines: list[str] = []
1438 for index, line in enumerate(self.lines):
1439 is_failure_line = line.startswith(fail_marker)
1440 if is_failure_line:
1441 # from this point on all lines are considered part of the failure
1442 failure_lines.extend(self.lines[index:])
1443 break
1444 else:
1445 indents.append(line[:indent_size])
1446 source_lines.append(line[indent_size:])
1447
1448 tw._write_source(source_lines, indents)
1449
1450 # failure lines are always completely red and bold
1451 for line in failure_lines:
1452 tw.line(line, bold=True, red=True)

Callers 2

deserialize_repr_entryFunction · 0.90
repr_traceback_entryMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected