Used inside a function to check whether it was called globally. Args: depth: The depth to get the frame. Returns: A tuple contains `module_name` and `called_globally`. Raises: RuntimeError: If the function is not called inside a function.
(depth: int = 2)
| 150 | |
| 151 | |
| 152 | def _get_caller_frame_info(depth: int = 2) -> tuple[str | None, bool]: |
| 153 | """Used inside a function to check whether it was called globally. |
| 154 | |
| 155 | Args: |
| 156 | depth: The depth to get the frame. |
| 157 | |
| 158 | Returns: |
| 159 | A tuple contains `module_name` and `called_globally`. |
| 160 | |
| 161 | Raises: |
| 162 | RuntimeError: If the function is not called inside a function. |
| 163 | """ |
| 164 | try: |
| 165 | previous_caller_frame = sys._getframe(depth) |
| 166 | except ValueError as e: |
| 167 | raise RuntimeError('This function must be used inside another function') from e |
| 168 | except AttributeError: # sys module does not have _getframe function, so there's nothing we can do about it |
| 169 | return None, False |
| 170 | frame_globals = previous_caller_frame.f_globals |
| 171 | return frame_globals.get('__name__'), previous_caller_frame.f_locals is frame_globals |
| 172 | |
| 173 | |
| 174 | DictValues: type[Any] = {}.values().__class__ |