77
88// 0x030B0000 is 3.11.
99#define PY_311 0x030B0000
10+ // 0x030D0000 is 3.13.
11+ #define PY_313 0x030D0000
12+ // 0x030E0000 is 3.14.
13+ #define PY_314 0x030E0000
14+
1015#if PY_VERSION_HEX >= PY_311
1116
1217/**
2227 */
2328
2429#define Py_BUILD_CORE
30+ #if PY_VERSION_HEX >= PY_314
31+ // Python 3.14 moved frame internals to pycore_interpframe.h
32+ #include "internal/pycore_interpframe.h"
33+ #else
2534#include "internal/pycore_frame.h"
35+ #endif
2636#undef Py_BUILD_CORE
2737
2838// Modified from
2939// https://github.com/python/cpython/blob/v3.11.4/Python/pystate.c#L1278-L1285
3040_PyInterpreterFrame * unsafe_PyThreadState_GetInterpreterFrame (
3141 PyThreadState * tstate ) {
3242 assert (tstate != NULL );
43+ #if PY_VERSION_HEX >= PY_313
44+ // In Python 3.13+, cframe was removed and current_frame is directly on tstate
45+ _PyInterpreterFrame * f = tstate -> current_frame ;
46+ #else
47+ // Python 3.11 and 3.12 use cframe->current_frame
3348 _PyInterpreterFrame * f = tstate -> cframe -> current_frame ;
49+ #endif
3450 while (f && _PyFrame_IsIncomplete (f )) {
3551 f = f -> previous ;
3652 }
@@ -47,7 +63,13 @@ PyCodeObject *unsafe_PyInterpreterFrame_GetCode(
4763 _PyInterpreterFrame * frame ) {
4864 assert (frame != NULL );
4965 assert (!_PyFrame_IsIncomplete (frame ));
66+ #if PY_VERSION_HEX >= PY_313
67+ // In Python 3.13+, use the _PyFrame_GetCode inline function
68+ // f_code was renamed to f_executable
69+ PyCodeObject * code = _PyFrame_GetCode (frame );
70+ #else
5071 PyCodeObject * code = frame -> f_code ;
72+ #endif
5173 assert (code != NULL );
5274 return code ;
5375}
@@ -71,6 +93,10 @@ _PyInterpreterFrame *unsafe_PyInterpreterFrame_GetBack(
7193// this function is not available in libpython
7294int _PyInterpreterFrame_GetLine (_PyInterpreterFrame * frame ) {
7395 int addr = _PyInterpreterFrame_LASTI (frame ) * sizeof (_Py_CODEUNIT );
96+ #if PY_VERSION_HEX >= PY_313
97+ return PyCode_Addr2Line (_PyFrame_GetCode (frame ), addr );
98+ #else
7499 return PyCode_Addr2Line (frame -> f_code , addr );
100+ #endif
75101}
76- #endif // PY_VERSION_HEX >= PY_311
102+ #endif // PY_VERSION_HEX >= PY_311
0 commit comments