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

Method cut

src/_pytest/_code/code.py:378–412  ·  view source on GitHub ↗

Return a Traceback instance wrapping part of this Traceback. By providing any combination of path, lineno and firstlineno, the first frame to start the to-be-returned traceback is determined. This allows cutting the first part of a Traceback instance e.g. for format

(
        self,
        path: os.PathLike[str] | str | None = None,
        lineno: int | None = None,
        firstlineno: int | None = None,
        excludepath: os.PathLike[str] | None = None,
    )

Source from the content-addressed store, hash-verified

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.95
_traceback_filterMethod · 0.80
test_traceback_cutMethod · 0.80

Calls 2

TracebackClass · 0.85
fspathMethod · 0.80

Tested by 2

test_traceback_cutMethod · 0.64