Skip to content

Commit d109f63

Browse files
authored
pythongh-101100: Properly document frame object attributes (python#112735)
1 parent c2e2df8 commit d109f63

File tree

12 files changed

+88
-49
lines changed

12 files changed

+88
-49
lines changed

Doc/c-api/frame.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ See also :ref:`Reflection <reflection>`.
5050
5151
.. c:function:: PyObject* PyFrame_GetBuiltins(PyFrameObject *frame)
5252
53-
Get the *frame*'s ``f_builtins`` attribute.
53+
Get the *frame*'s :attr:`~frame.f_builtins` attribute.
5454
5555
Return a :term:`strong reference`. The result cannot be ``NULL``.
5656
@@ -81,7 +81,7 @@ See also :ref:`Reflection <reflection>`.
8181
8282
.. c:function:: PyObject* PyFrame_GetGlobals(PyFrameObject *frame)
8383
84-
Get the *frame*'s ``f_globals`` attribute.
84+
Get the *frame*'s :attr:`~frame.f_globals` attribute.
8585
8686
Return a :term:`strong reference`. The result cannot be ``NULL``.
8787
@@ -90,7 +90,7 @@ See also :ref:`Reflection <reflection>`.
9090
9191
.. c:function:: int PyFrame_GetLasti(PyFrameObject *frame)
9292
93-
Get the *frame*'s ``f_lasti`` attribute.
93+
Get the *frame*'s :attr:`~frame.f_lasti` attribute.
9494
9595
Returns -1 if ``frame.f_lasti`` is ``None``.
9696
@@ -120,7 +120,7 @@ See also :ref:`Reflection <reflection>`.
120120
121121
.. c:function:: PyObject* PyFrame_GetLocals(PyFrameObject *frame)
122122
123-
Get the *frame*'s ``f_locals`` attribute (:class:`dict`).
123+
Get the *frame*'s :attr:`~frame.f_locals` attribute (:class:`dict`).
124124
125125
Return a :term:`strong reference`.
126126

Doc/c-api/init.rst

+3-2
Original file line numberDiff line numberDiff line change
@@ -1662,7 +1662,8 @@ Python-level trace functions in previous versions.
16621662
16631663
The value passed as the *what* parameter to a :c:type:`Py_tracefunc` function
16641664
(but not a profiling function) when a line-number event is being reported.
1665-
It may be disabled for a frame by setting :attr:`f_trace_lines` to *0* on that frame.
1665+
It may be disabled for a frame by setting :attr:`~frame.f_trace_lines` to
1666+
*0* on that frame.
16661667
16671668
16681669
.. c:var:: int PyTrace_RETURN
@@ -1694,7 +1695,7 @@ Python-level trace functions in previous versions.
16941695
The value for the *what* parameter to :c:type:`Py_tracefunc` functions (but not
16951696
profiling functions) when a new opcode is about to be executed. This event is
16961697
not emitted by default: it must be explicitly requested by setting
1697-
:attr:`f_trace_opcodes` to *1* on the frame.
1698+
:attr:`~frame.f_trace_opcodes` to *1* on the frame.
16981699
16991700
17001701
.. c:function:: void PyEval_SetProfile(Py_tracefunc func, PyObject *obj)

Doc/library/dis.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -865,8 +865,8 @@ iterations of the loop.
865865
.. opcode:: RERAISE
866866

867867
Re-raises the exception currently on top of the stack. If oparg is non-zero,
868-
pops an additional value from the stack which is used to set ``f_lasti``
869-
of the current frame.
868+
pops an additional value from the stack which is used to set
869+
:attr:`~frame.f_lasti` of the current frame.
870870

871871
.. versionadded:: 3.9
872872

Doc/library/inspect.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -1605,8 +1605,8 @@ the following flags:
16051605

16061606
.. data:: CO_NEWLOCALS
16071607

1608-
If set, a new dict will be created for the frame's ``f_locals`` when
1609-
the code object is executed.
1608+
If set, a new dict will be created for the frame's :attr:`~frame.f_locals`
1609+
when the code object is executed.
16101610

16111611
.. data:: CO_VARARGS
16121612

Doc/library/sys.rst

+5-4
Original file line numberDiff line numberDiff line change
@@ -1600,7 +1600,8 @@ always available.
16001600
:file:`Objects/lnotab_notes.txt` for a detailed explanation of how this
16011601
works.
16021602
Per-line events may be disabled for a frame by setting
1603-
:attr:`!f_trace_lines` to :const:`False` on that :ref:`frame <frame-objects>`.
1603+
:attr:`~frame.f_trace_lines` to :const:`False` on that
1604+
:ref:`frame <frame-objects>`.
16041605

16051606
``'return'``
16061607
A function (or other code block) is about to return. The local trace
@@ -1618,7 +1619,7 @@ always available.
16181619
opcode details). The local trace function is called; *arg* is
16191620
``None``; the return value specifies the new local trace function.
16201621
Per-opcode events are not emitted by default: they must be explicitly
1621-
requested by setting :attr:`!f_trace_opcodes` to :const:`True` on the
1622+
requested by setting :attr:`~frame.f_trace_opcodes` to :const:`True` on the
16221623
:ref:`frame <frame-objects>`.
16231624

16241625
Note that as an exception is propagated down the chain of callers, an
@@ -1648,8 +1649,8 @@ always available.
16481649

16491650
.. versionchanged:: 3.7
16501651

1651-
``'opcode'`` event type added; :attr:`!f_trace_lines` and
1652-
:attr:`!f_trace_opcodes` attributes added to frames
1652+
``'opcode'`` event type added; :attr:`~frame.f_trace_lines` and
1653+
:attr:`~frame.f_trace_opcodes` attributes added to frames
16531654

16541655
.. function:: set_asyncgen_hooks(firstiter, finalizer)
16551656

Doc/library/types.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,8 @@ Standard names are defined for the following types:
388388
.. data:: GetSetDescriptorType
389389

390390
The type of objects defined in extension modules with ``PyGetSetDef``, such
391-
as ``FrameType.f_locals`` or ``array.array.typecode``. This type is used as
391+
as :attr:`FrameType.f_locals <frame.f_locals>` or ``array.array.typecode``.
392+
This type is used as
392393
descriptor for object attributes; it has the same purpose as the
393394
:class:`property` type, but for classes defined in extension modules.
394395

Doc/reference/datamodel.rst

+59-25
Original file line numberDiff line numberDiff line change
@@ -1174,47 +1174,81 @@ Frame objects represent execution frames. They may occur in traceback objects
11741174
single: f_lasti (frame attribute)
11751175
single: f_builtins (frame attribute)
11761176

1177-
Special read-only attributes: :attr:`f_back` is to the previous stack frame
1178-
(towards the caller), or ``None`` if this is the bottom stack frame;
1179-
:attr:`f_code` is the code object being executed in this frame; :attr:`f_locals`
1180-
is the dictionary used to look up local variables; :attr:`f_globals` is used for
1181-
global variables; :attr:`f_builtins` is used for built-in (intrinsic) names;
1182-
:attr:`f_lasti` gives the precise instruction (this is an index into the
1183-
bytecode string of the code object).
1177+
Special read-only attributes
1178+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11841179

1185-
Accessing ``f_code`` raises an :ref:`auditing event <auditing>`
1186-
``object.__getattr__`` with arguments ``obj`` and ``"f_code"``.
1180+
.. list-table::
1181+
1182+
* - .. attribute:: frame.f_back
1183+
- Points to the previous stack frame (towards the caller),
1184+
or ``None`` if this is the bottom stack frame
1185+
1186+
* - .. attribute:: frame.f_code
1187+
- The :ref:`code object <code-objects>` being executed in this frame.
1188+
Accessing this attribute raises an :ref:`auditing event <auditing>`
1189+
``object.__getattr__`` with arguments ``obj`` and ``"f_code"``.
1190+
1191+
* - .. attribute:: frame.f_locals
1192+
- The dictionary used by the frame to look up
1193+
:ref:`local variables <naming>`
1194+
1195+
* - .. attribute:: frame.f_globals
1196+
- The dictionary used by the frame to look up
1197+
:ref:`global variables <naming>`
1198+
1199+
* - .. attribute:: frame.f_builtins
1200+
- The dictionary used by the frame to look up
1201+
:ref:`built-in (intrinsic) names <naming>`
1202+
1203+
* - .. attribute:: frame.f_lasti
1204+
- The "precise instruction" of the frame object
1205+
(this is an index into the :term:`bytecode` string of the
1206+
:ref:`code object <code-objects>`)
11871207

11881208
.. index::
11891209
single: f_trace (frame attribute)
11901210
single: f_trace_lines (frame attribute)
11911211
single: f_trace_opcodes (frame attribute)
11921212
single: f_lineno (frame attribute)
11931213

1194-
Special writable attributes: :attr:`f_trace`, if not ``None``, is a function
1195-
called for various events during code execution (this is used by the debugger).
1196-
Normally an event is triggered for each new source line - this can be
1197-
disabled by setting :attr:`f_trace_lines` to :const:`False`.
1214+
Special writable attributes
1215+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
1216+
1217+
.. list-table::
1218+
1219+
* - .. attribute:: frame.f_trace
1220+
- If not ``None``, this is a function called for various events during
1221+
code execution (this is used by debuggers). Normally an event is
1222+
triggered for each new source line (see :attr:`~frame.f_trace_lines`).
1223+
1224+
* - .. attribute:: frame.f_trace_lines
1225+
- Set this attribute to :const:`False` to disable triggering a tracing
1226+
event for each source line.
1227+
1228+
* - .. attribute:: frame.f_trace_opcodes
1229+
- Set this attribute to :const:`True` to allow per-opcode events to be
1230+
requested. Note that this may lead to
1231+
undefined interpreter behaviour if exceptions raised by the trace
1232+
function escape to the function being traced.
11981233

1199-
Implementations *may* allow per-opcode events to be requested by setting
1200-
:attr:`f_trace_opcodes` to :const:`True`. Note that this may lead to
1201-
undefined interpreter behaviour if exceptions raised by the trace
1202-
function escape to the function being traced.
1234+
* - .. attribute:: frame.f_lineno
1235+
- The current line number of the frame -- writing to this
1236+
from within a trace function jumps to the given line (only for the bottom-most
1237+
frame). A debugger can implement a Jump command (aka Set Next Statement)
1238+
by writing to this attribute.
12031239

1204-
:attr:`f_lineno` is the current line number of the frame --- writing to this
1205-
from within a trace function jumps to the given line (only for the bottom-most
1206-
frame). A debugger can implement a Jump command (aka Set Next Statement)
1207-
by writing to f_lineno.
1240+
Frame object methods
1241+
~~~~~~~~~~~~~~~~~~~~
12081242

12091243
Frame objects support one method:
12101244

12111245
.. method:: frame.clear()
12121246

1213-
This method clears all references to local variables held by the
1214-
frame. Also, if the frame belonged to a generator, the generator
1247+
This method clears all references to :ref:`local variables <naming>` held by the
1248+
frame. Also, if the frame belonged to a :term:`generator`, the generator
12151249
is finalized. This helps break reference cycles involving frame
1216-
objects (for example when catching an exception and storing its
1217-
traceback for later use).
1250+
objects (for example when catching an :ref:`exception <bltin-exceptions>`
1251+
and storing its :ref:`traceback <traceback-objects>` for later use).
12181252

12191253
:exc:`RuntimeError` is raised if the frame is currently executing
12201254
or suspended.

Doc/whatsnew/2.3.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -1998,13 +1998,13 @@ Some of the more notable changes are:
19981998
It would be difficult to detect any resulting difference from Python code, apart
19991999
from a slight speed up when Python is run without :option:`-O`.
20002000

2001-
C extensions that access the :attr:`f_lineno` field of frame objects should
2001+
C extensions that access the :attr:`~frame.f_lineno` field of frame objects should
20022002
instead call ``PyCode_Addr2Line(f->f_code, f->f_lasti)``. This will have the
20032003
added effect of making the code work as desired under "python -O" in earlier
20042004
versions of Python.
20052005

20062006
A nifty new feature is that trace functions can now assign to the
2007-
:attr:`f_lineno` attribute of frame objects, changing the line that will be
2007+
:attr:`~frame.f_lineno` attribute of frame objects, changing the line that will be
20082008
executed next. A ``jump`` command has been added to the :mod:`pdb` debugger
20092009
taking advantage of this new feature. (Implemented by Richie Hindle.)
20102010

Doc/whatsnew/3.10.rst

+4-3
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,8 @@ PEP 626: Precise line numbers for debugging and other tools
399399
PEP 626 brings more precise and reliable line numbers for debugging, profiling and coverage tools.
400400
Tracing events, with the correct line number, are generated for all lines of code executed and only for lines of code that are executed.
401401
402-
The ``f_lineno`` attribute of frame objects will always contain the expected line number.
402+
The :attr:`~frame.f_lineno` attribute of frame objects will always contain the
403+
expected line number.
403404
404405
The ``co_lnotab`` attribute of code objects is deprecated and will be removed in 3.12.
405406
Code that needs to convert from offset to line number should use the new ``co_lines()`` method instead.
@@ -1959,11 +1960,11 @@ Changes in the C API
19591960
source_buf = PyBytes_AsString(source_bytes_object);
19601961
code = Py_CompileString(source_buf, filename, Py_file_input);
19611962
1962-
* For ``FrameObject`` objects, the ``f_lasti`` member now represents a wordcode
1963+
* For ``FrameObject`` objects, the :attr:`~frame.f_lasti` member now represents a wordcode
19631964
offset instead of a simple offset into the bytecode string. This means that this
19641965
number needs to be multiplied by 2 to be used with APIs that expect a byte offset
19651966
instead (like :c:func:`PyCode_Addr2Line` for example). Notice as well that the
1966-
``f_lasti`` member of ``FrameObject`` objects is not considered stable: please
1967+
:attr:`!f_lasti` member of ``FrameObject`` objects is not considered stable: please
19671968
use :c:func:`PyFrame_GetLineNumber` instead.
19681969
19691970
CPython bytecode changes

Doc/whatsnew/3.11.rst

+3-2
Original file line numberDiff line numberDiff line change
@@ -2458,11 +2458,12 @@ Porting to Python 3.11
24582458
* ``f_valuestack``: removed.
24592459

24602460
The Python frame object is now created lazily. A side effect is that the
2461-
``f_back`` member must not be accessed directly, since its value is now also
2461+
:attr:`~frame.f_back` member must not be accessed directly,
2462+
since its value is now also
24622463
computed lazily. The :c:func:`PyFrame_GetBack` function must be called
24632464
instead.
24642465

2465-
Debuggers that accessed the ``f_locals`` directly *must* call
2466+
Debuggers that accessed the :attr:`~frame.f_locals` directly *must* call
24662467
:c:func:`PyFrame_GetLocals` instead. They no longer need to call
24672468
:c:func:`PyFrame_FastToLocalsWithError` or :c:func:`PyFrame_LocalsToFast`,
24682469
in fact they should not call those functions. The necessary updating of the

Doc/whatsnew/3.6.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -2162,7 +2162,7 @@ Changes in the Python API
21622162

21632163
* The format of the ``co_lnotab`` attribute of code objects changed to support
21642164
a negative line number delta. By default, Python does not emit bytecode with
2165-
a negative line number delta. Functions using ``frame.f_lineno``,
2165+
a negative line number delta. Functions using :attr:`frame.f_lineno`,
21662166
``PyFrame_GetLineNumber()`` or ``PyCode_Addr2Line()`` are not affected.
21672167
Functions directly decoding ``co_lnotab`` should be updated to use a signed
21682168
8-bit integer type for the line number delta, but this is only required to

Doc/whatsnew/3.7.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1891,7 +1891,7 @@ Other CPython Implementation Changes
18911891

18921892
* Trace hooks may now opt out of receiving the ``line`` and opt into receiving
18931893
the ``opcode`` events from the interpreter by setting the corresponding new
1894-
``f_trace_lines`` and ``f_trace_opcodes`` attributes on the
1894+
:attr:`~frame.f_trace_lines` and :attr:`~frame.f_trace_opcodes` attributes on the
18951895
frame being traced. (Contributed by Nick Coghlan in :issue:`31344`.)
18961896

18971897
* Fixed some consistency problems with namespace package module attributes.

0 commit comments

Comments
 (0)