def repr_traceback_entry( self, entry: TracebackEntry, excinfo: Optional[ExceptionInfo[BaseException]] = None, ) -> "ReprEntry": lines: List[str] = [] style = entry._repr_style if entry._repr_style is not None else self.style if style in ("short", "long"): source = self._getentrysource(entry) if source is None: source = Source("???") line_index = 0 else: line_index = entry.lineno - entry.getfirstlinesource() short = style == "short" reprargs = self.repr_args(entry) if not short else None s = self.get_source(source, line_index, excinfo, short=short) lines.extend(s) if short: message = "in %s" % (entry.name) else: message = excinfo and excinfo.typename or "" entry_path = entry.path path = self._makepath(entry_path) reprfileloc = ReprFileLocation(path, entry.lineno + 1, message) localsrepr = self.repr_locals(entry.locals) return ReprEntry(lines, reprargs, localsrepr, reprfileloc, style) elif style == "value": if excinfo: lines.extend(str(excinfo.value).split("\n")) return ReprEntry(lines, None, None, None, style) else: if excinfo: lines.extend(self.get_exconly(excinfo, indent=4)) return ReprEntry(lines, None, None, None, style)
def get_source( self, source: Optional["Source"], line_index: int = -1, excinfo: Optional[ExceptionInfo[BaseException]] = None, short: bool = False, ) -> List[str]: """Return formatted and marked up source lines.""" lines = [] if source is not None and line_index < 0: line_index += len(source.lines) if source is None or line_index >= len(source.lines) or line_index < 0: source = Source("???") line_index = 0 space_prefix = " " if short: lines.append(space_prefix + source.lines[line_index].strip()) else: for line in source.lines[:line_index]: lines.append(space_prefix + line) lines.append(self.flow_marker + " " + source.lines[line_index]) for line in source.lines[line_index + 1:]: lines.append(space_prefix + line) if excinfo is not None: indent = 4 if short else self._getindent(source) lines.extend(self.get_exconly(excinfo, indent=indent, markall=True)) return lines
def __init__( self, name, line_number, path, local_variables, fixture_source, test_source, raw_entry, ): super(ServicePlanFixtureTestTracebackEntry, self).__init__(raw_entry) self._name = name self.lineno = line_number - 1 self._path = path self._locals = local_variables self._fixture_source = Source(fixture_source) self._test_source = test_source self._frame = self.Faker() self._frame.statement = self.statement self._frame.getargs = lambda *_, **__: list(six.iteritems(local_variables)) self._frame.f_locals = local_variables self._frame.code = self.Faker() self._frame.code.path = path self._frame.code.raw = self.Faker() self._frame.code.raw.co_filename = str(path)
def test_linematcher_with_nonlist() -> None: """Test LineMatcher with regard to passing in a set (accidentally).""" from _pytest._code.source import Source lm = LineMatcher([]) with pytest.raises(TypeError, match="invalid type for lines2: set"): lm.fnmatch_lines(set()) # type: ignore[arg-type] with pytest.raises(TypeError, match="invalid type for lines2: dict"): lm.fnmatch_lines({}) # type: ignore[arg-type] with pytest.raises(TypeError, match="invalid type for lines2: set"): lm.re_match_lines(set()) # type: ignore[arg-type] with pytest.raises(TypeError, match="invalid type for lines2: dict"): lm.re_match_lines({}) # type: ignore[arg-type] with pytest.raises(TypeError, match="invalid type for lines2: Source"): lm.fnmatch_lines(Source()) # type: ignore[arg-type] lm.fnmatch_lines([]) lm.fnmatch_lines(()) lm.fnmatch_lines("") assert lm._getlines({}) == {} # type: ignore[arg-type,comparison-overlap] assert lm._getlines(set()) == set() # type: ignore[arg-type,comparison-overlap] assert lm._getlines(Source()) == [] assert lm._getlines(Source("pass\npass")) == ["pass", "pass"]
def statement(self) -> "Source": """Statement this frame is at.""" if self.code.fullsource is None: return Source("") return self.code.fullsource.getstatement(self.lineno)
def source(self) -> "Source": """Return a _pytest._code.Source object for the code object's source only.""" # return source only for that part of code return Source(self.raw)