Skip to content

Commit 0885999

Browse files
authored
bpo-46355: Document PyFrameObject and PyThreadState changes (GH-30558)
Document PyFrameObject and PyThreadState changes in What's New in Python 3.11 and explain how to port code.
1 parent f779fac commit 0885999

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

Doc/whatsnew/3.11.rst

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,110 @@ Porting to Python 3.11
755755
which are not available in the limited C API.
756756
(Contributed by Victor Stinner in :issue:`46007`.)
757757

758+
* Changes of the :c:type:`PyFrameObject` structure members:
759+
760+
* ``f_code``: removed, use :c:func:`PyFrame_GetCode` instead.
761+
Warning: the function returns a :term:`strong reference`, need to call
762+
:c:func:`Py_DECREF`.
763+
* ``f_back``: changed, use :c:func:`PyFrame_GetBack`.
764+
* ``f_builtins``: removed,
765+
use ``PyObject_GetAttrString(frame, "f_builtins")``.
766+
* ``f_globals``: removed,
767+
use ``PyObject_GetAttrString(frame, "f_globals")``.
768+
* ``f_locals``: removed,
769+
use ``PyObject_GetAttrString(frame, "f_locals")``.
770+
* ``f_lasti``: removed,
771+
use ``PyObject_GetAttrString(frame, "f_lasti")``.
772+
* ``f_valuesstack``: removed.
773+
* ``f_stackdepth``: removed.
774+
* ``f_gen``: removed.
775+
* ``f_iblock``: removed.
776+
* ``f_state``: removed.
777+
* ``f_blockstack``: removed.
778+
* ``f_localsplus``: removed.
779+
780+
The Python frame object is now created lazily. A side effect is that the
781+
``f_back`` member must not be accessed directly, since its value is now also
782+
computed lazily. The :c:func:`PyFrame_GetBack` function must be called
783+
instead.
784+
785+
Code defining ``PyFrame_GetCode()`` on Python 3.8 and older::
786+
787+
#if PY_VERSION_HEX < 0x030900B1
788+
static inline PyCodeObject* PyFrame_GetCode(PyFrameObject *frame)
789+
{
790+
Py_INCREF(frame->f_code);
791+
return frame->f_code;
792+
}
793+
#endif
794+
795+
Code defining ``PyFrame_GetBack()`` on Python 3.8 and older::
796+
797+
#if PY_VERSION_HEX < 0x030900B1
798+
static inline PyFrameObject* PyFrame_GetBack(PyFrameObject *frame)
799+
{
800+
Py_XINCREF(frame->f_back);
801+
return frame->f_back;
802+
}
803+
#endif
804+
805+
Or use `the pythoncapi_compat project
806+
<https://github.com/pythoncapi/pythoncapi_compat>`__ to get these functions
807+
on old Python functions.
808+
809+
* Changes of the :c:type:`PyThreadState` structure members:
810+
811+
* ``frame``: removed, use :c:func:`PyThreadState_GetFrame` (function added
812+
to Python 3.9 by :issue:`40429`).
813+
Warning: the function returns a :term:`strong reference`, need to call
814+
:c:func:`Py_XDECREF`.
815+
* ``tracing``: changed, use :c:func:`PyThreadState_EnterTracing`
816+
and :c:func:`PyThreadState_LeaveTracing`
817+
(functions added to Python 3.11 by :issue:`43760`).
818+
* ``recursion_depth``: removed,
819+
use ``(tstate->recursion_limit - tstate->recursion_remaining)`` instead.
820+
* ``stackcheck_counter``: removed.
821+
822+
Code defining ``PyThreadState_GetFrame()`` on Python 3.8 and older::
823+
824+
#if PY_VERSION_HEX < 0x030900B1
825+
static inline PyFrameObject* PyThreadState_GetFrame(PyThreadState *tstate)
826+
{
827+
Py_XINCREF(tstate->frame);
828+
return tstate->frame;
829+
}
830+
#endif
831+
832+
Code defining ``PyThreadState_EnterTracing()`` and
833+
``PyThreadState_LeaveTracing()`` on Python 3.10 and older::
834+
835+
#if PY_VERSION_HEX < 0x030B00A2
836+
static inline void PyThreadState_EnterTracing(PyThreadState *tstate)
837+
{
838+
tstate->tracing++;
839+
#if PY_VERSION_HEX >= 0x030A00A1
840+
tstate->cframe->use_tracing = 0;
841+
#else
842+
tstate->use_tracing = 0;
843+
#endif
844+
}
845+
846+
static inline void PyThreadState_LeaveTracing(PyThreadState *tstate)
847+
{
848+
int use_tracing = (tstate->c_tracefunc != NULL || tstate->c_profilefunc != NULL);
849+
tstate->tracing--;
850+
#if PY_VERSION_HEX >= 0x030A00A1
851+
tstate->cframe->use_tracing = use_tracing;
852+
#else
853+
tstate->use_tracing = use_tracing;
854+
#endif
855+
}
856+
#endif
857+
858+
Or use `the pythoncapi_compat project
859+
<https://github.com/pythoncapi/pythoncapi_compat>`__ to get these functions
860+
on old Python functions.
861+
758862

759863
Deprecated
760864
----------

0 commit comments

Comments
 (0)