Skip to content

Commit 5fa2d24

Browse files
authored
[3.11] gh-101100: Improve documentation of code object attributes (#112781) (#112817)
(cherry-picked from commit e9707d3)
1 parent 1e719d3 commit 5fa2d24

File tree

8 files changed

+104
-48
lines changed

8 files changed

+104
-48
lines changed

Doc/c-api/function.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ There are a few functions specific to Python functions.
3737
The function's docstring and name are retrieved from the code object. *__module__*
3838
is retrieved from *globals*. The argument defaults, annotations and closure are
3939
set to ``NULL``. *__qualname__* is set to the same value as the code object's
40-
``co_qualname`` field.
40+
:attr:`~codeobject.co_qualname` field.
4141
4242
4343
.. c:function:: PyObject* PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname)
4444
4545
As :c:func:`PyFunction_New`, but also allows setting the function object's
4646
``__qualname__`` attribute. *qualname* should be a unicode object or ``NULL``;
4747
if ``NULL``, the ``__qualname__`` attribute is set to the same value as the
48-
code object's ``co_qualname`` field.
48+
code object's :attr:`~codeobject.co_qualname` field.
4949
5050
.. versionadded:: 3.3
5151

Doc/c-api/import.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ Importing Modules
138138
:class:`~importlib.machinery.SourceFileLoader` otherwise.
139139
140140
The module's :attr:`__file__` attribute will be set to the code object's
141-
:attr:`!co_filename`. If applicable, :attr:`__cached__` will also
141+
:attr:`~codeobject.co_filename`. If applicable, :attr:`__cached__` will also
142142
be set.
143143
144144
This function will reload the module if it was already imported. See

Doc/library/dis.rst

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,9 @@ operation is being performed, so the intermediate analysis object isn't useful:
322322
Line numbers can be decreasing. Before, they were always increasing.
323323

324324
.. versionchanged:: 3.10
325-
The :pep:`626` ``co_lines`` method is used instead of the ``co_firstlineno``
326-
and ``co_lnotab`` attributes of the code object.
325+
The :pep:`626` ``co_lines`` method is used instead of the
326+
:attr:`~codeobject.co_firstlineno` and :attr:`~codeobject.co_lnotab`
327+
attributes of the code object.
327328

328329

329330
.. function:: findlabels(code)
@@ -806,14 +807,15 @@ iterations of the loop.
806807
.. opcode:: STORE_NAME (namei)
807808

808809
Implements ``name = TOS``. *namei* is the index of *name* in the attribute
809-
:attr:`co_names` of the code object. The compiler tries to use
810+
:attr:`~codeobject.co_names` of the :ref:`code object <code-objects>`.
811+
The compiler tries to use
810812
:opcode:`STORE_FAST` or :opcode:`STORE_GLOBAL` if possible.
811813

812814

813815
.. opcode:: DELETE_NAME (namei)
814816

815-
Implements ``del name``, where *namei* is the index into :attr:`co_names`
816-
attribute of the code object.
817+
Implements ``del name``, where *namei* is the index into :attr:`~codeobject.co_names`
818+
attribute of the :ref:`code object <code-objects>`.
817819

818820

819821
.. opcode:: UNPACK_SEQUENCE (count)
@@ -842,7 +844,8 @@ iterations of the loop.
842844

843845
.. opcode:: DELETE_ATTR (namei)
844846

845-
Implements ``del TOS.name``, using *namei* as index into :attr:`co_names`.
847+
Implements ``del TOS.name``, using *namei* as index into
848+
:attr:`~codeobject.co_names` of the :ref:`code object <code-objects>`.
846849

847850

848851
.. opcode:: STORE_GLOBAL (namei)
@@ -1138,7 +1141,7 @@ iterations of the loop.
11381141
Pushes a reference to the object the cell contains on the stack.
11391142

11401143
.. versionchanged:: 3.11
1141-
``i`` is no longer offset by the length of ``co_varnames``.
1144+
``i`` is no longer offset by the length of :attr:`~codeobject.co_varnames`.
11421145

11431146

11441147
.. opcode:: LOAD_CLASSDEREF (i)
@@ -1159,7 +1162,7 @@ iterations of the loop.
11591162
storage.
11601163

11611164
.. versionchanged:: 3.11
1162-
``i`` is no longer offset by the length of ``co_varnames``.
1165+
``i`` is no longer offset by the length of :attr:`~codeobject.co_varnames`.
11631166

11641167

11651168
.. opcode:: DELETE_DEREF (i)
@@ -1170,7 +1173,7 @@ iterations of the loop.
11701173
.. versionadded:: 3.2
11711174

11721175
.. versionchanged:: 3.11
1173-
``i`` is no longer offset by the length of ``co_varnames``.
1176+
``i`` is no longer offset by the length of :attr:`~codeobject.co_varnames`.
11741177

11751178

11761179
.. opcode:: COPY_FREE_VARS (n)

Doc/library/inspect.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,8 +1532,8 @@ updated as expected:
15321532
Code Objects Bit Flags
15331533
----------------------
15341534

1535-
Python code objects have a ``co_flags`` attribute, which is a bitmap of
1536-
the following flags:
1535+
Python code objects have a :attr:`~codeobject.co_flags` attribute,
1536+
which is a bitmap of the following flags:
15371537

15381538
.. data:: CO_OPTIMIZED
15391539

Doc/reference/datamodel.rst

Lines changed: 80 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,56 +1064,107 @@ indirectly) to mutable objects.
10641064
single: co_freevars (code object attribute)
10651065
single: co_qualname (code object attribute)
10661066

1067-
Special read-only attributes: :attr:`co_name` gives the function name;
1068-
:attr:`co_qualname` gives the fully qualified function name;
1069-
:attr:`co_argcount` is the total number of positional arguments
1070-
(including positional-only arguments and arguments with default values);
1071-
:attr:`co_posonlyargcount` is the number of positional-only arguments
1072-
(including arguments with default values); :attr:`co_kwonlyargcount` is
1073-
the number of keyword-only arguments (including arguments with default
1074-
values); :attr:`co_nlocals` is the number of local variables used by the
1075-
function (including arguments); :attr:`co_varnames` is a tuple containing
1076-
the names of the local variables (starting with the argument names);
1077-
:attr:`co_cellvars` is a tuple containing the names of local variables
1078-
that are referenced by nested functions; :attr:`co_freevars` is a tuple
1079-
containing the names of free variables; :attr:`co_code` is a string
1080-
representing the sequence of bytecode instructions; :attr:`co_consts` is
1081-
a tuple containing the literals used by the bytecode; :attr:`co_names` is
1082-
a tuple containing the names used by the bytecode; :attr:`co_filename` is
1083-
the filename from which the code was compiled; :attr:`co_firstlineno` is
1084-
the first line number of the function; :attr:`co_lnotab` is a string
1085-
encoding the mapping from bytecode offsets to line numbers (for details
1086-
see the source code of the interpreter); :attr:`co_stacksize` is the
1087-
required stack size; :attr:`co_flags` is an integer encoding a number
1088-
of flags for the interpreter.
1067+
Special read-only attributes
1068+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1069+
1070+
.. list-table::
1071+
1072+
* - .. attribute:: codeobject.co_name
1073+
- The function name
1074+
1075+
* - .. attribute:: codeobject.co_qualname
1076+
- The fully qualified function name
1077+
1078+
* - .. attribute:: codeobject.co_argcount
1079+
- The total number of positional :term:`parameters <parameter>`
1080+
(including positional-only parameters and parameters with default values)
1081+
that the function has
1082+
1083+
* - .. attribute:: codeobject.co_posonlyargcount
1084+
- The number of positional-only :term:`parameters <parameter>`
1085+
(including arguments with default values) that the function has
1086+
1087+
* - .. attribute:: codeobject.co_kwonlyargcount
1088+
- The number of keyword-only :term:`parameters <parameter>`
1089+
(including arguments with default values) that the function has
1090+
1091+
* - .. attribute:: codeobject.co_nlocals
1092+
- The number of :ref:`local variables <naming>` used by the function
1093+
(including parameters)
1094+
1095+
* - .. attribute:: codeobject.co_varnames
1096+
- A :class:`tuple` containing the names of the local variables in the
1097+
function (starting with the parameter names)
1098+
1099+
* - .. attribute:: codeobject.co_cellvars
1100+
- A :class:`tuple` containing the names of :ref:`local variables <naming>`
1101+
that are referenced by nested functions inside the function
1102+
1103+
* - .. attribute:: codeobject.co_freevars
1104+
- A :class:`tuple` containing the names of free variables in the function
1105+
1106+
* - .. attribute:: codeobject.co_code
1107+
- A string representing the sequence of :term:`bytecode` instructions in
1108+
the function
1109+
1110+
* - .. attribute:: codeobject.co_consts
1111+
- A :class:`tuple` containing the literals used by the :term:`bytecode` in
1112+
the function
1113+
1114+
* - .. attribute:: codeobject.co_names
1115+
- A :class:`tuple` containing the names used by the :term:`bytecode` in
1116+
the function
1117+
1118+
* - .. attribute:: codeobject.co_filename
1119+
- The name of the file from which the code was compiled
1120+
1121+
* - .. attribute:: codeobject.co_firstlineno
1122+
- The line number of the first line of the function
1123+
1124+
* - .. attribute:: codeobject.co_lnotab
1125+
- A string encoding the mapping from :term:`bytecode` offsets to line
1126+
numbers. For details, see the source code of the interpreter.
1127+
1128+
* - .. attribute:: codeobject.co_stacksize
1129+
- The required stack size of the code object
1130+
1131+
* - .. attribute:: codeobject.co_flags
1132+
- An :class:`integer <int>` encoding a number of flags for the
1133+
interpreter.
10891134

10901135
.. index:: pair: object; generator
10911136

1092-
The following flag bits are defined for :attr:`co_flags`: bit ``0x04`` is set if
1137+
The following flag bits are defined for :attr:`~codeobject.co_flags`:
1138+
bit ``0x04`` is set if
10931139
the function uses the ``*arguments`` syntax to accept an arbitrary number of
10941140
positional arguments; bit ``0x08`` is set if the function uses the
10951141
``**keywords`` syntax to accept arbitrary keyword arguments; bit ``0x20`` is set
1096-
if the function is a generator.
1142+
if the function is a generator. See :ref:`inspect-module-co-flags` for details
1143+
on the semantics of each flags that might be present.
10971144

10981145
Future feature declarations (``from __future__ import division``) also use bits
1099-
in :attr:`co_flags` to indicate whether a code object was compiled with a
1146+
in :attr:`~codeobject.co_flags` to indicate whether a code object was compiled with a
11001147
particular feature enabled: bit ``0x2000`` is set if the function was compiled
11011148
with future division enabled; bits ``0x10`` and ``0x1000`` were used in earlier
11021149
versions of Python.
11031150

1104-
Other bits in :attr:`co_flags` are reserved for internal use.
1151+
Other bits in :attr:`~codeobject.co_flags` are reserved for internal use.
11051152

11061153
.. index:: single: documentation string
11071154

1108-
If a code object represents a function, the first item in :attr:`co_consts` is
1155+
If a code object represents a function, the first item in
1156+
:attr:`~codeobject.co_consts` is
11091157
the documentation string of the function, or ``None`` if undefined.
11101158

1159+
The :meth:`!co_positions` method
1160+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1161+
11111162
.. method:: codeobject.co_positions()
11121163

1113-
Returns an iterable over the source code positions of each bytecode
1164+
Returns an iterable over the source code positions of each :term:`bytecode`
11141165
instruction in the code object.
11151166

1116-
The iterator returns tuples containing the ``(start_line, end_line,
1167+
The iterator returns :class:`tuple`\s containing the ``(start_line, end_line,
11171168
start_column, end_column)``. The *i-th* tuple corresponds to the
11181169
position of the source code that compiled to the *i-th* instruction.
11191170
Column information is 0-indexed utf-8 byte offsets on the given source

Doc/whatsnew/2.7.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2404,7 +2404,7 @@ Other Changes and Fixes
24042404
:issue:`5464`.)
24052405

24062406
* When importing a module from a :file:`.pyc` or :file:`.pyo` file
2407-
with an existing :file:`.py` counterpart, the :attr:`co_filename`
2407+
with an existing :file:`.py` counterpart, the :attr:`~codeobject.co_filename`
24082408
attributes of the resulting code objects are overwritten when the
24092409
original filename is obsolete. This can happen if the file has been
24102410
renamed, moved, or is accessed through different paths. (Patch by

Doc/whatsnew/3.10.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,8 @@ Tracing events, with the correct line number, are generated for all lines of cod
402402
The :attr:`~frame.f_lineno` attribute of frame objects will always contain the
403403
expected line number.
404404
405-
The ``co_lnotab`` attribute of code objects is deprecated and will be removed in 3.12.
405+
The :attr:`~codeobject.co_lnotab` attribute of code objects is deprecated and
406+
will be removed in 3.12.
406407
Code that needs to convert from offset to line number should use the new ``co_lines()`` method instead.
407408
408409
PEP 634: Structural Pattern Matching

Doc/whatsnew/3.6.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2160,14 +2160,15 @@ Changes in the Python API
21602160
* :c:func:`PyErr_SetImportError` now sets :exc:`TypeError` when its **msg**
21612161
argument is not set. Previously only ``NULL`` was returned.
21622162

2163-
* The format of the ``co_lnotab`` attribute of code objects changed to support
2163+
* The format of the :attr:`~codeobject.co_lnotab` attribute of code objects
2164+
changed to support
21642165
a negative line number delta. By default, Python does not emit bytecode with
21652166
a negative line number delta. Functions using :attr:`frame.f_lineno`,
21662167
``PyFrame_GetLineNumber()`` or ``PyCode_Addr2Line()`` are not affected.
2167-
Functions directly decoding ``co_lnotab`` should be updated to use a signed
2168+
Functions directly decoding :attr:`!co_lnotab` should be updated to use a signed
21682169
8-bit integer type for the line number delta, but this is only required to
21692170
support applications using a negative line number delta. See
2170-
``Objects/lnotab_notes.txt`` for the ``co_lnotab`` format and how to decode
2171+
``Objects/lnotab_notes.txt`` for the :attr:`!co_lnotab` format and how to decode
21712172
it, and see the :pep:`511` for the rationale.
21722173

21732174
* The functions in the :mod:`compileall` module now return booleans instead

0 commit comments

Comments
 (0)