MCPcopy Index your code
hub / github.com/pytest-dev/pytest / Traceback

Class Traceback

src/_pytest/_code/code.py:358–464  ·  view source on GitHub ↗

Traceback objects encapsulate and offer higher level access to Traceback entries.

Source from the content-addressed store, hash-verified

356
357
358class Traceback(list[TracebackEntry]):
359 """Traceback objects encapsulate and offer higher level access to Traceback entries."""
360
361 def __init__(
362 self,
363 tb: TracebackType | Iterable[TracebackEntry],
364 ) -> None:
365 """Initialize from given python traceback object and ExceptionInfo."""
366 if isinstance(tb, TracebackType):
367
368 def f(cur: TracebackType) -> Iterable[TracebackEntry]:
369 cur_: TracebackType | None = cur
370 while cur_ is not None:
371 yield TracebackEntry(cur_)
372 cur_ = cur_.tb_next
373
374 super().__init__(f(tb))
375 else:
376 super().__init__(tb)
377
378 def cut(
379 self,
380 path: os.PathLike[str] | str | None = None,
381 lineno: int | None = None,
382 firstlineno: int | None = None,
383 excludepath: os.PathLike[str] | None = None,
384 ) -> Traceback:
385 """Return a Traceback instance wrapping part of this Traceback.
386
387 By providing any combination of path, lineno and firstlineno, the
388 first frame to start the to-be-returned traceback is determined.
389
390 This allows cutting the first part of a Traceback instance e.g.
391 for formatting reasons (removing some uninteresting bits that deal
392 with handling of the exception/traceback).
393 """
394 path_ = None if path is None else os.fspath(path)
395 excludepath_ = None if excludepath is None else os.fspath(excludepath)
396 for x in self:
397 code = x.frame.code
398 codepath = code.path
399 if path is not None and str(codepath) != path_:
400 continue
401 if (
402 excludepath is not None
403 and isinstance(codepath, Path)
404 and excludepath_ in (str(p) for p in codepath.parents) # type: ignore[operator]
405 ):
406 continue
407 if lineno is not None and x.lineno != lineno:
408 continue
409 if firstlineno is not None and x.frame.code.firstlineno != firstlineno:
410 continue
411 return Traceback(x._rawentry)
412 return self
413
414 @overload
415 def __getitem__(self, key: SupportsIndex) -> TracebackEntry: ...

Callers 4

_traceback_filterMethod · 0.90
cutMethod · 0.85
filterMethod · 0.85
tracebackMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…