Walk the stack and find the frame of the first caller not in this module.
()
| 88 | |
| 89 | |
| 90 | def _infer_caller(): |
| 91 | """ |
| 92 | Walk the stack and find the frame of the first caller not in this module. |
| 93 | """ |
| 94 | |
| 95 | def is_this_file(frame_info): |
| 96 | return frame_info.filename == stack[0].filename |
| 97 | |
| 98 | def is_wrapper(frame_info): |
| 99 | return frame_info.function == 'wrapper' |
| 100 | |
| 101 | stack = inspect.stack() |
| 102 | not_this_file = itertools.filterfalse(is_this_file, stack) |
| 103 | # also exclude 'wrapper' due to singledispatch in the call stack |
| 104 | callers = itertools.filterfalse(is_wrapper, not_this_file) |
| 105 | return next(callers).frame |
| 106 | |
| 107 | |
| 108 | def from_package(package: types.ModuleType): |