gh-94936: C getters: co_varnames, co_cellvars, co_freevars (#95008)

This commit is contained in:
Ken Jin 2022-08-04 21:53:31 +08:00 committed by GitHub
parent 0342c93a6b
commit 42b102bbf9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 128 additions and 15 deletions

View file

@ -90,3 +90,28 @@ bound into a function.
.. versionadded:: 3.11
.. c:function:: PyObject* PyCode_GetVarnames(PyCodeObject *co)
Equivalent to the Python code ``getattr(co, 'co_varnames')``.
Returns a new reference to a :c:type:`PyTupleObject` containing the names of
the local variables. On error, ``NULL`` is returned and an exception
is raised.
.. versionadded:: 3.11
.. c:function:: PyObject* PyCode_GetCellvars(PyCodeObject *co)
Equivalent to the Python code ``getattr(co, 'co_cellvars')``.
Returns a new reference to a :c:type:`PyTupleObject` containing the names of
the local variables that are referenced by nested functions. On error, ``NULL``
is returned and an exception is raised.
.. versionadded:: 3.11
.. c:function:: PyObject* PyCode_GetFreevars(PyCodeObject *co)
Equivalent to the Python code ``getattr(co, 'co_freevars')``.
Returns a new reference to a :c:type:`PyTupleObject` containing the names of
the free variables. On error, ``NULL`` is returned and an exception is raised.
.. versionadded:: 3.11

View file

@ -1719,10 +1719,13 @@ Porting to Python 3.11
To get a custom code object: create a code object using the compiler,
then get a modified version with the ``replace`` method.
* :c:type:`PyCodeObject` no longer has a ``co_code`` field. Instead,
use ``PyObject_GetAttrString(code_object, "co_code")`` or
:c:func:`PyCode_GetCode` to get the underlying bytes object.
(Contributed by Brandt Bucher in :issue:`46841` and Ken Jin in :gh:`92154`.)
* :c:type:`PyCodeObject` no longer has the ``co_code``, ``co_varnames``,
``co_cellvars`` and ``co_freevars`` fields. Instead, use
:c:func:`PyCode_GetCode`, :c:func:`PyCode_GetVarnames`,
:c:func:`PyCode_GetCellvars` and :c:func:`PyCode_GetFreevars` respectively
to access them via the C API.
(Contributed by Brandt Bucher in :issue:`46841` and Ken Jin in :gh:`92154`
and :gh:`94936`.)
* The old trashcan macros (``Py_TRASHCAN_SAFE_BEGIN``/``Py_TRASHCAN_SAFE_END``)
are now deprecated. They should be replaced by the new macros