MCPcopy Create free account
hub / github.com/pytest-dev/pytest / Frame

Class Frame

src/_pytest/_code/code.py:129–188  ·  view source on GitHub ↗

Wrapper around a Python frame holding f_locals and f_globals in which expressions can be evaluated.

Source from the content-addressed store, hash-verified

127
128
129class Frame:
130 """Wrapper around a Python frame holding f_locals and f_globals
131 in which expressions can be evaluated."""
132
133 __slots__ = ("raw",)
134
135 def __init__(self, frame: FrameType) -> None:
136 self.raw = frame
137
138 @property
139 def lineno(self) -> int:
140 return self.raw.f_lineno - 1
141
142 @property
143 def f_globals(self) -> Dict[str, Any]:
144 return self.raw.f_globals
145
146 @property
147 def f_locals(self) -> Dict[str, Any]:
148 return self.raw.f_locals
149
150 @property
151 def code(self) -> Code:
152 return Code(self.raw.f_code)
153
154 @property
155 def statement(self) -> "Source":
156 """Statement this frame is at."""
157 if self.code.fullsource is None:
158 return Source("")
159 return self.code.fullsource.getstatement(self.lineno)
160
161 def eval(self, code, **vars):
162 """Evaluate 'code' in the frame.
163
164 'vars' are optional additional local variables.
165
166 Returns the result of the evaluation.
167 """
168 f_locals = self.f_locals.copy()
169 f_locals.update(vars)
170 return eval(code, self.f_globals, f_locals)
171
172 def repr(self, object: object) -> str:
173 """Return a 'safe' (non-recursive, one-line) string repr for 'object'."""
174 return saferepr(object)
175
176 def getargs(self, var: bool = False):
177 """Return a list of tuples (name, value) for all arguments.
178
179 If 'var' is set True, also include the variable and keyword arguments
180 when present.
181 """
182 retval = []
183 for arg in self.code.getargs(var):
184 try:
185 retval.append((arg, self.f_locals[arg]))
186 except KeyError:

Callers 5

test_frame_getargsFunction · 0.90
__init__Method · 0.90
frameMethod · 0.85

Calls

no outgoing calls

Tested by 4

test_frame_getargsFunction · 0.72
__init__Method · 0.72