Find the first place in the stack that is not inside pandas (tests notwithstanding).
()
| 35 | |
| 36 | |
| 37 | def find_stack_level() -> int: |
| 38 | """ |
| 39 | Find the first place in the stack that is not inside pandas |
| 40 | (tests notwithstanding). |
| 41 | """ |
| 42 | |
| 43 | import pandas as pd |
| 44 | |
| 45 | pkg_dir = os.path.dirname(pd.__file__) |
| 46 | test_dir = os.path.join(pkg_dir, "tests") |
| 47 | |
| 48 | # https://stackoverflow.com/questions/17407119/python-inspect-stack-is-slow |
| 49 | frame: FrameType | None = inspect.currentframe() |
| 50 | try: |
| 51 | n = 0 |
| 52 | while frame: |
| 53 | filename = inspect.getfile(frame) |
| 54 | if filename.startswith(pkg_dir) and not filename.startswith(test_dir): |
| 55 | frame = frame.f_back |
| 56 | n += 1 |
| 57 | else: |
| 58 | break |
| 59 | finally: |
| 60 | # See note in |
| 61 | # https://docs.python.org/3/library/inspect.html#inspect.Traceback |
| 62 | del frame |
| 63 | return n |
| 64 | |
| 65 | |
| 66 | @contextlib.contextmanager |
no test coverage detected