Merge pull request #9447 from bluetech/code-cut-pathlike

code: accept any `os.PathLike[str]` in `Traceback.cut`
This commit is contained in:
Ran Benita 2021-12-27 15:02:20 +02:00 committed by GitHub
commit fcef7e49fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 4 deletions

View File

@ -1,5 +1,6 @@
import ast import ast
import inspect import inspect
import os
import re import re
import sys import sys
import traceback import traceback
@ -343,10 +344,10 @@ class Traceback(List[TracebackEntry]):
def cut( def cut(
self, self,
path: Optional[Union[Path, str]] = None, path: Optional[Union["os.PathLike[str]", str]] = None,
lineno: Optional[int] = None, lineno: Optional[int] = None,
firstlineno: Optional[int] = None, firstlineno: Optional[int] = None,
excludepath: Optional[Path] = None, excludepath: Optional["os.PathLike[str]"] = None,
) -> "Traceback": ) -> "Traceback":
"""Return a Traceback instance wrapping part of this Traceback. """Return a Traceback instance wrapping part of this Traceback.
@ -357,15 +358,17 @@ class Traceback(List[TracebackEntry]):
for formatting reasons (removing some uninteresting bits that deal for formatting reasons (removing some uninteresting bits that deal
with handling of the exception/traceback). with handling of the exception/traceback).
""" """
path_ = None if path is None else os.fspath(path)
excludepath_ = None if excludepath is None else os.fspath(excludepath)
for x in self: for x in self:
code = x.frame.code code = x.frame.code
codepath = code.path codepath = code.path
if path is not None and codepath != path: if path is not None and str(codepath) != path_:
continue continue
if ( if (
excludepath is not None excludepath is not None
and isinstance(codepath, Path) and isinstance(codepath, Path)
and excludepath in codepath.parents and excludepath_ in (str(p) for p in codepath.parents) # type: ignore[operator]
): ):
continue continue
if lineno is not None and x.lineno != lineno: if lineno is not None and x.lineno != lineno: