Skip to content

Commit c26d6c1

Browse files
authored
[3.11] gh-101100: Properly document frame object attributes (#112735) (#112775)
* [3.11] gh-101100: Properly document frame object attributes (#112735) (cherry-picked from commit d109f63)
1 parent 36f6fa4 commit c26d6c1

File tree

12 files changed

+99
-59
lines changed

12 files changed

+99
-59
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
@@ -99,7 +99,7 @@ See also :ref:`Reflection <reflection>`.
9999
100100
.. c:function:: PyObject* PyFrame_GetLocals(PyFrameObject *frame)
101101
102-
Get the *frame*'s ``f_locals`` attribute (:class:`dict`).
102+
Get the *frame*'s :attr:`~frame.f_locals` attribute (:class:`dict`).
103103
104104
Return a :term:`strong reference`.
105105

Doc/c-api/init.rst

+3-2
Original file line numberDiff line numberDiff line change
@@ -1620,7 +1620,8 @@ Python-level trace functions in previous versions.
16201620
16211621
The value passed as the *what* parameter to a :c:type:`Py_tracefunc` function
16221622
(but not a profiling function) when a line-number event is being reported.
1623-
It may be disabled for a frame by setting :attr:`f_trace_lines` to *0* on that frame.
1623+
It may be disabled for a frame by setting :attr:`~frame.f_trace_lines` to
1624+
*0* on that frame.
16241625
16251626
16261627
.. c:var:: int PyTrace_RETURN
@@ -1652,7 +1653,7 @@ Python-level trace functions in previous versions.
16521653
The value for the *what* parameter to :c:type:`Py_tracefunc` functions (but not
16531654
profiling functions) when a new opcode is about to be executed. This event is
16541655
not emitted by default: it must be explicitly requested by setting
1655-
:attr:`f_trace_opcodes` to *1* on the frame.
1656+
:attr:`~frame.f_trace_opcodes` to *1* on the frame.
16561657
16571658
16581659
.. c:function:: void PyEval_SetProfile(Py_tracefunc func, PyObject *obj)

Doc/library/dis.rst

+12-12
Original file line numberDiff line numberDiff line change
@@ -673,27 +673,27 @@ iterations of the loop.
673673

674674
Pops a value from the stack, which is used to restore the exception state.
675675

676-
.. versionchanged:: 3.11
677-
Exception representation on the stack now consist of one, not three, items.
676+
.. versionchanged:: 3.11
677+
Exception representation on the stack now consist of one, not three, items.
678678

679679
.. opcode:: RERAISE
680680

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

685-
.. versionadded:: 3.9
685+
.. versionadded:: 3.9
686686

687-
.. versionchanged:: 3.11
688-
Exception representation on the stack now consist of one, not three, items.
687+
.. versionchanged:: 3.11
688+
Exception representation on the stack now consist of one, not three, items.
689689

690690
.. opcode:: PUSH_EXC_INFO
691691

692-
Pops a value from the stack. Pushes the current exception to the top of the stack.
693-
Pushes the value originally popped back to the stack.
694-
Used in exception handlers.
692+
Pops a value from the stack. Pushes the current exception to the top of the stack.
693+
Pushes the value originally popped back to the stack.
694+
Used in exception handlers.
695695

696-
.. versionadded:: 3.11
696+
.. versionadded:: 3.11
697697

698698
.. opcode:: CHECK_EXC_MATCH
699699

Doc/library/inspect.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -1541,8 +1541,8 @@ the following flags:
15411541

15421542
.. data:: CO_NEWLOCALS
15431543

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

15471547
.. data:: CO_VARARGS
15481548

Doc/library/sys.rst

+5-4
Original file line numberDiff line numberDiff line change
@@ -1554,7 +1554,8 @@ always available.
15541554
:file:`Objects/lnotab_notes.txt` for a detailed explanation of how this
15551555
works.
15561556
Per-line events may be disabled for a frame by setting
1557-
:attr:`!f_trace_lines` to :const:`False` on that :ref:`frame <frame-objects>`.
1557+
:attr:`~frame.f_trace_lines` to :const:`False` on that
1558+
:ref:`frame <frame-objects>`.
15581559

15591560
``'return'``
15601561
A function (or other code block) is about to return. The local trace
@@ -1572,7 +1573,7 @@ always available.
15721573
opcode details). The local trace function is called; *arg* is
15731574
``None``; the return value specifies the new local trace function.
15741575
Per-opcode events are not emitted by default: they must be explicitly
1575-
requested by setting :attr:`!f_trace_opcodes` to :const:`True` on the
1576+
requested by setting :attr:`~frame.f_trace_opcodes` to :const:`True` on the
15761577
:ref:`frame <frame-objects>`.
15771578

15781579
Note that as an exception is propagated down the chain of callers, an
@@ -1602,8 +1603,8 @@ always available.
16021603

16031604
.. versionchanged:: 3.7
16041605

1605-
``'opcode'`` event type added; :attr:`!f_trace_lines` and
1606-
:attr:`!f_trace_opcodes` attributes added to frames
1606+
``'opcode'`` event type added; :attr:`~frame.f_trace_lines` and
1607+
:attr:`~frame.f_trace_opcodes` attributes added to frames
16071608

16081609
.. function:: set_asyncgen_hooks(firstiter, finalizer)
16091610

Doc/library/types.rst

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

348348
The type of objects defined in extension modules with ``PyGetSetDef``, such
349-
as ``FrameType.f_locals`` or ``array.array.typecode``. This type is used as
349+
as :attr:`FrameType.f_locals <frame.f_locals>` or ``array.array.typecode``.
350+
This type is used as
350351
descriptor for object attributes; it has the same purpose as the
351352
:class:`property` type, but for classes defined in extension modules.
352353

Doc/reference/datamodel.rst

+60-25
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,7 @@ mentioned here for completeness.
10311031

10321032
.. index:: bytecode, object; code, code object
10331033

1034+
.. _code-objects:
10341035

10351036
Code objects
10361037
^^^^^^^^^^^^
@@ -1159,47 +1160,81 @@ Frame objects represent execution frames. They may occur in traceback objects
11591160
single: f_lasti (frame attribute)
11601161
single: f_builtins (frame attribute)
11611162

1162-
Special read-only attributes: :attr:`f_back` is to the previous stack frame
1163-
(towards the caller), or ``None`` if this is the bottom stack frame;
1164-
:attr:`f_code` is the code object being executed in this frame; :attr:`f_locals`
1165-
is the dictionary used to look up local variables; :attr:`f_globals` is used for
1166-
global variables; :attr:`f_builtins` is used for built-in (intrinsic) names;
1167-
:attr:`f_lasti` gives the precise instruction (this is an index into the
1168-
bytecode string of the code object).
1163+
Special read-only attributes
1164+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11691165

1170-
Accessing ``f_code`` raises an :ref:`auditing event <auditing>`
1171-
``object.__getattr__`` with arguments ``obj`` and ``"f_code"``.
1166+
.. list-table::
1167+
1168+
* - .. attribute:: frame.f_back
1169+
- Points to the previous stack frame (towards the caller),
1170+
or ``None`` if this is the bottom stack frame
1171+
1172+
* - .. attribute:: frame.f_code
1173+
- The :ref:`code object <code-objects>` being executed in this frame.
1174+
Accessing this attribute raises an :ref:`auditing event <auditing>`
1175+
``object.__getattr__`` with arguments ``obj`` and ``"f_code"``.
1176+
1177+
* - .. attribute:: frame.f_locals
1178+
- The dictionary used by the frame to look up
1179+
:ref:`local variables <naming>`
1180+
1181+
* - .. attribute:: frame.f_globals
1182+
- The dictionary used by the frame to look up
1183+
:ref:`global variables <naming>`
1184+
1185+
* - .. attribute:: frame.f_builtins
1186+
- The dictionary used by the frame to look up
1187+
:ref:`built-in (intrinsic) names <naming>`
1188+
1189+
* - .. attribute:: frame.f_lasti
1190+
- The "precise instruction" of the frame object
1191+
(this is an index into the :term:`bytecode` string of the
1192+
:ref:`code object <code-objects>`)
11721193

11731194
.. index::
11741195
single: f_trace (frame attribute)
11751196
single: f_trace_lines (frame attribute)
11761197
single: f_trace_opcodes (frame attribute)
11771198
single: f_lineno (frame attribute)
11781199

1179-
Special writable attributes: :attr:`f_trace`, if not ``None``, is a function
1180-
called for various events during code execution (this is used by the debugger).
1181-
Normally an event is triggered for each new source line - this can be
1182-
disabled by setting :attr:`f_trace_lines` to :const:`False`.
1200+
Special writable attributes
1201+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
1202+
1203+
.. list-table::
1204+
1205+
* - .. attribute:: frame.f_trace
1206+
- If not ``None``, this is a function called for various events during
1207+
code execution (this is used by debuggers). Normally an event is
1208+
triggered for each new source line (see :attr:`~frame.f_trace_lines`).
1209+
1210+
* - .. attribute:: frame.f_trace_lines
1211+
- Set this attribute to :const:`False` to disable triggering a tracing
1212+
event for each source line.
1213+
1214+
* - .. attribute:: frame.f_trace_opcodes
1215+
- Set this attribute to :const:`True` to allow per-opcode events to be
1216+
requested. Note that this may lead to
1217+
undefined interpreter behaviour if exceptions raised by the trace
1218+
function escape to the function being traced.
11831219

1184-
Implementations *may* allow per-opcode events to be requested by setting
1185-
:attr:`f_trace_opcodes` to :const:`True`. Note that this may lead to
1186-
undefined interpreter behaviour if exceptions raised by the trace
1187-
function escape to the function being traced.
1220+
* - .. attribute:: frame.f_lineno
1221+
- The current line number of the frame -- writing to this
1222+
from within a trace function jumps to the given line (only for the bottom-most
1223+
frame). A debugger can implement a Jump command (aka Set Next Statement)
1224+
by writing to this attribute.
11881225

1189-
:attr:`f_lineno` is the current line number of the frame --- writing to this
1190-
from within a trace function jumps to the given line (only for the bottom-most
1191-
frame). A debugger can implement a Jump command (aka Set Next Statement)
1192-
by writing to f_lineno.
1226+
Frame object methods
1227+
~~~~~~~~~~~~~~~~~~~~
11931228

11941229
Frame objects support one method:
11951230

11961231
.. method:: frame.clear()
11971232

1198-
This method clears all references to local variables held by the
1199-
frame. Also, if the frame belonged to a generator, the generator
1233+
This method clears all references to :ref:`local variables <naming>` held by the
1234+
frame. Also, if the frame belonged to a :term:`generator`, the generator
12001235
is finalized. This helps break reference cycles involving frame
1201-
objects (for example when catching an exception and storing its
1202-
traceback for later use).
1236+
objects (for example when catching an :ref:`exception <bltin-exceptions>`
1237+
and storing its :ref:`traceback <traceback-objects>` for later use).
12031238

12041239
:exc:`RuntimeError` is raised if the frame is currently executing.
12051240

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.
@@ -1958,11 +1959,11 @@ Changes in the C API
19581959
source_buf = PyBytes_AsString(source_bytes_object);
19591960
code = Py_CompileString(source_buf, filename, Py_file_input);
19601961
1961-
* For ``FrameObject`` objects, the ``f_lasti`` member now represents a wordcode
1962+
* For ``FrameObject`` objects, the :attr:`~frame.f_lasti` member now represents a wordcode
19621963
offset instead of a simple offset into the bytecode string. This means that this
19631964
number needs to be multiplied by 2 to be used with APIs that expect a byte offset
19641965
instead (like :c:func:`PyCode_Addr2Line` for example). Notice as well that the
1965-
``f_lasti`` member of ``FrameObject`` objects is not considered stable: please
1966+
:attr:`!f_lasti` member of ``FrameObject`` objects is not considered stable: please
19661967
use :c:func:`PyFrame_GetLineNumber` instead.
19671968
19681969
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)