Skip to content

Commit

Permalink
fix(python): Exclude functools wrapper frames in find_stacklevel (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mcrumiller authored Sep 25, 2023
1 parent 34043ce commit f08641a
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions py-polars/polars/utils/various.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,15 +362,19 @@ def find_stacklevel() -> int:
Taken from:
https://github.com/pandas-dev/pandas/blob/ab89c53f48df67709a533b6a95ce3d911871a0a8/pandas/util/_exceptions.py#L30-L51
"""
pkg_dir = Path(pl.__file__).parent
pkg_dir = str(Path(pl.__file__).parent)

# https://stackoverflow.com/questions/17407119/python-inspect-stack-is-slow
frame = inspect.currentframe()
n = 0
try:
while frame:
fname = inspect.getfile(frame)
if fname.startswith(str(pkg_dir)):
if fname.startswith(pkg_dir) or (
(qualname := getattr(frame.f_code, "co_qualname", None))
# ignore @singledispatch wrappers
and qualname.startswith("singledispatch.")
):
frame = frame.f_back
n += 1
else:
Expand Down

0 comments on commit f08641a

Please sign in to comment.