Skip to content

Commit 16fcef8

Browse files
AlexWaygoodGlyphack
authored andcommitted
pythongh-101100: Improve documentation of code object attributes (python#112781)
1 parent e1061c0 commit 16fcef8

File tree

10 files changed

+112
-53
lines changed

10 files changed

+112
-53
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
@@ -154,7 +154,7 @@ Importing Modules
154154
:class:`~importlib.machinery.SourceFileLoader` otherwise.
155155
156156
The module's :attr:`__file__` attribute will be set to the code object's
157-
:attr:`!co_filename`. If applicable, :attr:`__cached__` will also
157+
:attr:`~codeobject.co_filename`. If applicable, :attr:`__cached__` will also
158158
be set.
159159
160160
This function will reload the module if it was already imported. See

Doc/library/dis.rst

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

348348
.. versionchanged:: 3.10
349-
The :pep:`626` ``co_lines`` method is used instead of the ``co_firstlineno``
350-
and ``co_lnotab`` attributes of the code object.
349+
The :pep:`626` ``co_lines`` method is used instead of the
350+
:attr:`~codeobject.co_firstlineno` and :attr:`~codeobject.co_lnotab`
351+
attributes of the code object.
351352

352353
.. versionchanged:: 3.13
353354
Line numbers can be ``None`` for bytecode that does not map to source lines.
@@ -983,13 +984,13 @@ iterations of the loop.
983984
.. opcode:: STORE_NAME (namei)
984985

985986
Implements ``name = STACK.pop()``. *namei* is the index of *name* in the attribute
986-
:attr:`!co_names` of the :ref:`code object <code-objects>`.
987+
:attr:`~codeobject.co_names` of the :ref:`code object <code-objects>`.
987988
The compiler tries to use :opcode:`STORE_FAST` or :opcode:`STORE_GLOBAL` if possible.
988989

989990

990991
.. opcode:: DELETE_NAME (namei)
991992

992-
Implements ``del name``, where *namei* is the index into :attr:`!co_names`
993+
Implements ``del name``, where *namei* is the index into :attr:`~codeobject.co_names`
993994
attribute of the :ref:`code object <code-objects>`.
994995

995996

@@ -1029,7 +1030,7 @@ iterations of the loop.
10291030
value = STACK.pop()
10301031
obj.name = value
10311032

1032-
where *namei* is the index of name in :attr:`!co_names` of the
1033+
where *namei* is the index of name in :attr:`~codeobject.co_names` of the
10331034
:ref:`code object <code-objects>`.
10341035

10351036
.. opcode:: DELETE_ATTR (namei)
@@ -1039,7 +1040,7 @@ iterations of the loop.
10391040
obj = STACK.pop()
10401041
del obj.name
10411042

1042-
where *namei* is the index of name into :attr:`!co_names` of the
1043+
where *namei* is the index of name into :attr:`~codeobject.co_names` of the
10431044
:ref:`code object <code-objects>`.
10441045

10451046

@@ -1402,7 +1403,7 @@ iterations of the loop.
14021403
Pushes a reference to the object the cell contains on the stack.
14031404

14041405
.. versionchanged:: 3.11
1405-
``i`` is no longer offset by the length of ``co_varnames``.
1406+
``i`` is no longer offset by the length of :attr:`~codeobject.co_varnames`.
14061407

14071408

14081409
.. opcode:: LOAD_FROM_DICT_OR_DEREF (i)
@@ -1424,7 +1425,7 @@ iterations of the loop.
14241425
storage.
14251426

14261427
.. versionchanged:: 3.11
1427-
``i`` is no longer offset by the length of ``co_varnames``.
1428+
``i`` is no longer offset by the length of :attr:`~codeobject.co_varnames`.
14281429

14291430

14301431
.. opcode:: DELETE_DEREF (i)
@@ -1435,7 +1436,7 @@ iterations of the loop.
14351436
.. versionadded:: 3.2
14361437

14371438
.. versionchanged:: 3.11
1438-
``i`` is no longer offset by the length of ``co_varnames``.
1439+
``i`` is no longer offset by the length of :attr:`~codeobject.co_varnames`.
14391440

14401441

14411442
.. opcode:: COPY_FREE_VARS (n)

Doc/library/inspect.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,8 +1596,8 @@ updated as expected:
15961596
Code Objects Bit Flags
15971597
----------------------
15981598

1599-
Python code objects have a ``co_flags`` attribute, which is a bitmap of
1600-
the following flags:
1599+
Python code objects have a :attr:`~codeobject.co_flags` attribute,
1600+
which is a bitmap of the following flags:
16011601

16021602
.. data:: CO_OPTIMIZED
16031603

Doc/reference/datamodel.rst

Lines changed: 84 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,57 +1077,111 @@ indirectly) to mutable objects.
10771077
single: co_freevars (code object attribute)
10781078
single: co_qualname (code object attribute)
10791079

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

11041152
.. index:: pair: object; generator
11051153

1106-
The following flag bits are defined for :attr:`co_flags`: bit ``0x04`` is set if
1154+
The following flag bits are defined for :attr:`~codeobject.co_flags`:
1155+
bit ``0x04`` is set if
11071156
the function uses the ``*arguments`` syntax to accept an arbitrary number of
11081157
positional arguments; bit ``0x08`` is set if the function uses the
11091158
``**keywords`` syntax to accept arbitrary keyword arguments; bit ``0x20`` is set
1110-
if the function is a generator.
1159+
if the function is a generator. See :ref:`inspect-module-co-flags` for details
1160+
on the semantics of each flags that might be present.
11111161

11121162
Future feature declarations (``from __future__ import division``) also use bits
1113-
in :attr:`co_flags` to indicate whether a code object was compiled with a
1163+
in :attr:`~codeobject.co_flags` to indicate whether a code object was compiled with a
11141164
particular feature enabled: bit ``0x2000`` is set if the function was compiled
11151165
with future division enabled; bits ``0x10`` and ``0x1000`` were used in earlier
11161166
versions of Python.
11171167

1118-
Other bits in :attr:`co_flags` are reserved for internal use.
1168+
Other bits in :attr:`~codeobject.co_flags` are reserved for internal use.
11191169

11201170
.. index:: single: documentation string
11211171

1122-
If a code object represents a function, the first item in :attr:`co_consts` is
1172+
If a code object represents a function, the first item in
1173+
:attr:`~codeobject.co_consts` is
11231174
the documentation string of the function, or ``None`` if undefined.
11241175

1176+
The :meth:`!co_positions` method
1177+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1178+
11251179
.. method:: codeobject.co_positions()
11261180

1127-
Returns an iterable over the source code positions of each bytecode
1181+
Returns an iterable over the source code positions of each :term:`bytecode`
11281182
instruction in the code object.
11291183

1130-
The iterator returns tuples containing the ``(start_line, end_line,
1184+
The iterator returns :class:`tuple`\s containing the ``(start_line, end_line,
11311185
start_column, end_column)``. The *i-th* tuple corresponds to the
11321186
position of the source code that compiled to the *i-th* instruction.
11331187
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
@@ -2405,7 +2405,7 @@ Other Changes and Fixes
24052405
:issue:`5464`.)
24062406

24072407
* When importing a module from a :file:`.pyc` or :file:`.pyo` file
2408-
with an existing :file:`.py` counterpart, the :attr:`co_filename`
2408+
with an existing :file:`.py` counterpart, the :attr:`~codeobject.co_filename`
24092409
attributes of the resulting code objects are overwritten when the
24102410
original filename is obsolete. This can happen if the file has been
24112411
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.12.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,7 +1323,8 @@ Deprecated
13231323
``int``, convert to int explicitly: ``~int(x)``. (Contributed by Tim Hoffmann
13241324
in :gh:`103487`.)
13251325

1326-
* Accessing ``co_lnotab`` on code objects was deprecated in Python 3.10 via :pep:`626`,
1326+
* Accessing :attr:`~codeobject.co_lnotab` on code objects was deprecated in
1327+
Python 3.10 via :pep:`626`,
13271328
but it only got a proper :exc:`DeprecationWarning` in 3.12,
13281329
therefore it will be removed in 3.14.
13291330
(Contributed by Nikita Sobolev in :gh:`101866`.)
@@ -1430,7 +1431,7 @@ and will be removed in Python 3.14.
14301431

14311432
* The ``__package__`` and ``__cached__`` attributes on module objects.
14321433

1433-
* The ``co_lnotab`` attribute of code objects.
1434+
* The :attr:`~codeobject.co_lnotab` attribute of code objects.
14341435

14351436
Pending Removal in Python 3.15
14361437
------------------------------

Doc/whatsnew/3.13.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,8 @@ Pending Removal in Python 3.14
572572
* date and datetime adapter, date and timestamp converter:
573573
see the :mod:`sqlite3` documentation for suggested replacement recipes.
574574

575-
* :class:`types.CodeType`: Accessing ``co_lnotab`` was deprecated in :pep:`626`
575+
* :class:`types.CodeType`: Accessing :attr:`~codeobject.co_lnotab` was
576+
deprecated in :pep:`626`
576577
since 3.10 and was planned to be removed in 3.12,
577578
but it only got a proper :exc:`DeprecationWarning` in 3.12.
578579
May be removed in 3.14.
@@ -735,7 +736,7 @@ although there is currently no date scheduled for their removal.
735736

736737
* :mod:`!sre_compile`, :mod:`!sre_constants` and :mod:`!sre_parse` modules.
737738

738-
* ``types.CodeType.co_lnotab``: use the ``co_lines`` attribute instead.
739+
* :attr:`~codeobject.co_lnotab`: use the ``co_lines`` attribute instead.
739740

740741
* :class:`typing.Text` (:gh:`92332`).
741742

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)