MCPcopy
hub / github.com/pydantic/pydantic / _get_caller_frame_info

Function _get_caller_frame_info

pydantic/_internal/_generics.py:152–171  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

150
151
152def _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
174DictValues: type[Any] = {}.values().__class__

Callers 6

functionFunction · 0.90
another_functionFunction · 0.90
third_functionFunction · 0.90
moduleFunction · 0.90
create_generic_submodelFunction · 0.85

Calls 1

getMethod · 0.45

Tested by 5

functionFunction · 0.72
another_functionFunction · 0.72
third_functionFunction · 0.72
moduleFunction · 0.72