Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Doc/c-api/function.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ There are a few functions specific to Python functions.
.. versionadded:: 3.3


.. c:function:: PyObject* PyFunction_NewWithDoc(PyObject *code, PyObject *globals, PyObject *qualname, PyObject *doc)

As :c:func:`PyFunction_NewWithQualName`, but also allows setting the function
object's ``__doc__`` attribute (e.g. docstring). *doc* should be a unicode
object or ``NULL``; if ``NULL``, the ``__doc__`` attribute is set to ``None``.
This API don't use ``code.co_consts[0]`` for the docstring.

.. versionadded:: 3.11


.. c:function:: PyObject* PyFunction_GetCode(PyObject *op)

Return the code object associated with the function object *op*.
Expand Down
4 changes: 4 additions & 0 deletions Doc/library/dis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1226,12 +1226,16 @@ All of the following opcodes use their arguments.
* ``0x02`` a dictionary of keyword-only parameters' default values
* ``0x04`` a tuple of strings containing parameters' annotations
* ``0x08`` a tuple containing cells for free variables, making a closure
* ``0x10`` a unicode object containing functions' docstring
* the code associated with the function (at TOS1)
* the :term:`qualified name` of the function (at TOS)

.. versionchanged:: 3.10
Flag value ``0x04`` is a tuple of strings instead of dictionary

.. versionchanged:: 3.11
Flag value ``0x10`` is added.

.. opcode:: BUILD_SLICE (argc)

.. index:: builtin: slice
Expand Down
5 changes: 5 additions & 0 deletions Doc/whatsnew/3.11.rst
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ Other CPython Implementation Changes
support :class:`typing.SupportsComplex` and :class:`typing.SupportsBytes` protocols.
(Contributed by Mark Dickinson and Dong-hee Na in :issue:`24234`.)

* Until Python 3.10, compiler stored function docstring in ``code.co_consts[0]``.
Python 3.11 don't use code object to store docstring anymore. The docstring
is passed through the VM stack.
(Contributed by Inada Naoki in :issue:`36521`.)


New Modules
===========
Expand Down
1 change: 1 addition & 0 deletions Include/funcobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ PyAPI_DATA(PyTypeObject) PyFunction_Type;

PyAPI_FUNC(PyObject *) PyFunction_New(PyObject *, PyObject *);
PyAPI_FUNC(PyObject *) PyFunction_NewWithQualName(PyObject *, PyObject *, PyObject *);
PyAPI_FUNC(PyObject *) PyFunction_NewWithDoc(PyObject *, PyObject *, PyObject *, PyObject *);
PyAPI_FUNC(PyObject *) PyFunction_GetCode(PyObject *);
PyAPI_FUNC(PyObject *) PyFunction_GetGlobals(PyObject *);
PyAPI_FUNC(PyObject *) PyFunction_GetModule(PyObject *);
Expand Down
2 changes: 1 addition & 1 deletion Lib/dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
(ascii, 'ascii'),
)
MAKE_FUNCTION = opmap['MAKE_FUNCTION']
MAKE_FUNCTION_FLAGS = ('defaults', 'kwdefaults', 'annotations', 'closure')
MAKE_FUNCTION_FLAGS = ('defaults', 'kwdefaults', 'annotations', 'closure', 'doc')

LOAD_CONST = opmap['LOAD_CONST']

Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
freevars: ()
nlocals: 2
flags: 3
consts: ('None', '<code object g>')
consts: ('<code object g>',)

>>> dump(f(4).__code__)
name: g
Expand Down Expand Up @@ -87,7 +87,7 @@
freevars: ()
nlocals: 0
flags: 3
consts: ("'doc string'", 'None')
consts: ('None',)

>>> def keywordonly_args(a,b,*,k1):
... return a,b,k1
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ def test_strip_unused_consts(self):
def f1():
"docstring"
return 42
self.assertEqual(f1.__code__.co_consts, ("docstring", 42))
self.assertEqual(f1.__code__.co_consts, (42,))

# This is a regression test for a CPython specific peephole optimizer
# implementation bug present in a few releases. It's assertion verifies
Expand Down Expand Up @@ -953,7 +953,7 @@ def return_genexp():
y)
genexp_lines = [None, 1, 3, 1]

genexp_code = return_genexp.__code__.co_consts[1]
genexp_code = return_genexp.__code__.co_consts[0]
code_lines = [ None if line is None else line-return_genexp.__code__.co_firstlineno
for (_, _, line) in genexp_code.co_lines() ]
self.assertEqual(genexp_lines, code_lines)
Expand Down
Loading