Debian Codegraph @

Doc/c-api/frame.rst

raw
1 .. highlight:: c
2
3 Frame Objects
4 -------------
5
6 .. c:type:: PyFrameObject
7
8 The C structure of the objects used to describe frame objects.
9
10 There are no public members in this structure.
11
12 .. versionchanged:: 3.11
13 The members of this structure were removed from the public C API.
14 Refer to the :ref:`What's New entry <pyframeobject-3.11-hiding>`
15 for details.
16
17 The :c:func:`PyEval_GetFrame` and :c:func:`PyThreadState_GetFrame` functions
18 can be used to get a frame object.
19
20 See also :ref:`Reflection <reflection>`.
21
22 .. c:var:: PyTypeObject PyFrame_Type
23
24 The type of frame objects.
25 It is the same object as :py:class:`types.FrameType` in the Python layer.
26
27 .. versionchanged:: 3.11
28
29 Previously, this type was only available after including
30 ``<frameobject.h>``.
31
32 .. c:function:: PyFrameObject *PyFrame_New(PyThreadState *tstate, PyCodeObject *code, PyObject *globals, PyObject *locals)
33
34 Create a new frame object. This function returns a :term:`strong reference`
35 to the new frame object on success, and returns ``NULL`` with an exception
36 set on failure.
37
38 .. c:function:: int PyFrame_Check(PyObject *obj)
39
40 Return non-zero if *obj* is a frame object.
41
42 .. versionchanged:: 3.11
43
44 Previously, this function was only available after including
45 ``<frameobject.h>``.
46
47 .. c:function:: PyFrameObject* PyFrame_GetBack(PyFrameObject *frame)
48
49 Get the *frame* next outer frame.
50
51 Return a :term:`strong reference`, or ``NULL`` if *frame* has no outer
52 frame.
53
54 .. versionadded:: 3.9
55
56
57 .. c:function:: PyObject* PyFrame_GetBuiltins(PyFrameObject *frame)
58
59 Get the *frame*'s :attr:`~frame.f_builtins` attribute.
60
61 Return a :term:`strong reference`. The result cannot be ``NULL``.
62
63 .. versionadded:: 3.11
64
65
66 .. c:function:: PyCodeObject* PyFrame_GetCode(PyFrameObject *frame)
67
68 Get the *frame* code.
69
70 Return a :term:`strong reference`.
71
72 The result (frame code) cannot be ``NULL``.
73
74 .. versionadded:: 3.9
75
76
77 .. c:function:: PyObject* PyFrame_GetGenerator(PyFrameObject *frame)
78
79 Get the generator, coroutine, or async generator that owns this frame,
80 or ``NULL`` if this frame is not owned by a generator.
81 Does not raise an exception, even if the return value is ``NULL``.
82
83 Return a :term:`strong reference`, or ``NULL``.
84
85 .. versionadded:: 3.11
86
87
88 .. c:function:: PyObject* PyFrame_GetGlobals(PyFrameObject *frame)
89
90 Get the *frame*'s :attr:`~frame.f_globals` attribute.
91
92 Return a :term:`strong reference`. The result cannot be ``NULL``.
93
94 .. versionadded:: 3.11
95
96
97 .. c:function:: int PyFrame_GetLasti(PyFrameObject *frame)
98
99 Get the *frame*'s :attr:`~frame.f_lasti` attribute.
100
101 Returns -1 if ``frame.f_lasti`` is ``None``.
102
103 .. versionadded:: 3.11
104
105
106 .. c:function:: PyObject* PyFrame_GetVar(PyFrameObject *frame, PyObject *name)
107
108 Get the variable *name* of *frame*.
109
110 * Return a :term:`strong reference` to the variable value on success.
111 * Raise :exc:`NameError` and return ``NULL`` if the variable does not exist.
112 * Raise an exception and return ``NULL`` on error.
113
114 *name* type must be a :class:`str`.
115
116 .. versionadded:: 3.12
117
118
119 .. c:function:: PyObject* PyFrame_GetVarString(PyFrameObject *frame, const char *name)
120
121 Similar to :c:func:`PyFrame_GetVar`, but the variable name is a C string
122 encoded in UTF-8.
123
124 .. versionadded:: 3.12
125
126
127 .. c:function:: PyObject* PyFrame_GetLocals(PyFrameObject *frame)
128
129 Get the *frame*'s :attr:`~frame.f_locals` attribute.
130 If the frame refers to an :term:`optimized scope`, this returns a
131 write-through proxy object that allows modifying the locals.
132 In all other cases (classes, modules, :func:`exec`, :func:`eval`) it returns
133 the mapping representing the frame locals directly (as described for
134 :func:`locals`).
135
136 Return a :term:`strong reference`.
137
138 .. versionadded:: 3.11
139
140 .. versionchanged:: 3.13
141 As part of :pep:`667`, return an instance of :c:var:`PyFrameLocalsProxy_Type`.
142
143
144 .. c:function:: int PyFrame_GetLineNumber(PyFrameObject *frame)
145
146 Return the line number that *frame* is currently executing.
147
148
149 Frame Locals Proxies
150 ^^^^^^^^^^^^^^^^^^^^
151
152 .. versionadded:: 3.13
153
154 The :attr:`~frame.f_locals` attribute on a :ref:`frame object <frame-objects>`
155 is an instance of a "frame-locals proxy". The proxy object exposes a
156 write-through view of the underlying locals dictionary for the frame. This
157 ensures that the variables exposed by ``f_locals`` are always up to date with
158 the live local variables in the frame itself.
159
160 See :pep:`667` for more information.
161
162 .. c:var:: PyTypeObject PyFrameLocalsProxy_Type
163
164 The type of frame :func:`locals` proxy objects.
165
166 .. c:function:: int PyFrameLocalsProxy_Check(PyObject *obj)
167
168 Return non-zero if *obj* is a frame :func:`locals` proxy.
169
170
171 Legacy Local Variable APIs
172 ^^^^^^^^^^^^^^^^^^^^^^^^^^
173
174 These APIs are :term:`soft deprecated`. As of Python 3.13, they do nothing.
175 They exist solely for backwards compatibility.
176
177
178 .. c:function:: void PyFrame_LocalsToFast(PyFrameObject *f, int clear)
179
180 This function is :term:`soft deprecated` and does nothing.
181
182 Prior to Python 3.13, this function would copy the :attr:`~frame.f_locals`
183 attribute of *f* to the internal "fast" array of local variables, allowing
184 changes in frame objects to be visible to the interpreter. If *clear* was
185 true, this function would process variables that were unset in the locals
186 dictionary.
187
188 .. versionchanged:: 3.13
189 This function now does nothing.
190
191
192 .. c:function:: void PyFrame_FastToLocals(PyFrameObject *f)
193
194 This function is :term:`soft deprecated` and does nothing.
195
196 Prior to Python 3.13, this function would copy the internal "fast" array
197 of local variables (which is used by the interpreter) to the
198 :attr:`~frame.f_locals` attribute of *f*, allowing changes in local
199 variables to be visible to frame objects.
200
201 .. versionchanged:: 3.13
202 This function now does nothing.
203
204
205 .. c:function:: int PyFrame_FastToLocalsWithError(PyFrameObject *f)
206
207 This function is :term:`soft deprecated` and does nothing.
208
209 Prior to Python 3.13, this function was similar to
210 :c:func:`PyFrame_FastToLocals`, but would return ``0`` on success, and
211 ``-1`` with an exception set on failure.
212
213 .. versionchanged:: 3.13
214 This function now does nothing.
215
216
217 .. seealso::
218 :pep:`667`
219
220
221 Internal Frames
222 ^^^^^^^^^^^^^^^
223
224 Unless using :pep:`523`, you will not need this.
225
226 .. c:struct:: _PyInterpreterFrame
227
228 The interpreter's internal frame representation.
229
230 .. versionadded:: 3.11
231
232 .. c:function:: PyObject* PyUnstable_InterpreterFrame_GetCode(struct _PyInterpreterFrame *frame);
233
234 Return a :term:`strong reference` to the code object for the frame.
235
236 .. versionadded:: 3.12
237
238
239 .. c:function:: int PyUnstable_InterpreterFrame_GetLasti(struct _PyInterpreterFrame *frame);
240
241 Return the byte offset into the last executed instruction.
242
243 .. versionadded:: 3.12
244
245
246 .. c:function:: int PyUnstable_InterpreterFrame_GetLine(struct _PyInterpreterFrame *frame);
247
248 Return the currently executing line number, or -1 if there is no line number.
249
250 .. versionadded:: 3.12
251
252