@@ -17,6 +17,11 @@ limitations under the License. */
17
17
#include < Python.h>
18
18
#include < code.h>
19
19
#include < frameobject.h>
20
+
21
+ #if PY_VERSION_HEX >= 0x030b0000
22
+ #include < internal/pycore_frame.h>
23
+ #endif
24
+
20
25
#include < object.h>
21
26
#include < pystate.h>
22
27
@@ -36,6 +41,12 @@ namespace py = pybind11;
36
41
namespace paddle {
37
42
namespace pybind {
38
43
44
+ #if PY_VERSION_HEX >= 0x030b0000
45
+ typedef _PyInterpreterFrame FrameObject;
46
+ #else
47
+ typedef PyFrameObject FrameObject;
48
+ #endif
49
+
39
50
#define unlikely (x ) __builtin_expect((x), 0 )
40
51
41
52
// Use static variable to save customed eval hook.
@@ -56,7 +67,7 @@ inline static void eval_frame_callback_set(PyObject *obj) {
56
67
57
68
// call python default eval frame to interpret current frame.
58
69
inline static PyObject *eval_frame_default (PyThreadState *tstate,
59
- PyFrameObject *frame,
70
+ FrameObject *frame,
60
71
int throw_flag) {
61
72
#if PY_VERSION_HEX >= 0x03090000
62
73
if (tstate == NULL ) {
@@ -71,26 +82,34 @@ inline static PyObject *eval_frame_default(PyThreadState *tstate,
71
82
// Start a new frame and run code in this frame.
72
83
// Execute a piece of code by default frame-hook.
73
84
inline static PyObject *eval_custom_code (PyThreadState *tstate,
74
- PyFrameObject *frame,
85
+ FrameObject *frame,
75
86
PyCodeObject *code,
76
87
int throw_flag) {
77
88
Py_ssize_t ncells = 0 ;
78
89
Py_ssize_t nfrees = 0 ;
79
90
Py_ssize_t nlocals_new = code->co_nlocals ;
80
91
Py_ssize_t nlocals_old = frame->f_code ->co_nlocals ;
81
92
82
- if ((code->co_flags & CO_NOFREE) == 0 ) {
83
- ncells = PyTuple_GET_SIZE (code->co_cellvars );
84
- nfrees = PyTuple_GET_SIZE (code->co_freevars );
85
- }
93
+ #if PY_VERSION_HEX >= 0x030b0000
94
+ ncells = code->co_ncellvars ;
95
+ nfrees = code->co_nfreevars ;
96
+ #else
97
+ ncells = PyTuple_GET_SIZE (code->co_cellvars );
98
+ nfrees = PyTuple_GET_SIZE (code->co_freevars );
99
+ #endif
86
100
87
101
PyFrameObject *shadow = PyFrame_New (tstate, code, frame->f_globals , NULL );
88
102
if (shadow == NULL ) {
89
103
return NULL ;
90
104
}
91
105
106
+ #if PY_VERSION_HEX >= 0x030b0000
107
+ PyObject **fastlocals_old = frame->localsplus ;
108
+ PyObject **fastlocals_new = shadow ->f_frame ->localsplus ;
109
+ #else
92
110
PyObject **fastlocals_old = frame->f_localsplus ;
93
111
PyObject **fastlocals_new = shadow ->f_localsplus ;
112
+ #endif
94
113
95
114
for (Py_ssize_t i = 0 ; i < nlocals_old; i++) {
96
115
Py_XINCREF (fastlocals_old[i]);
@@ -102,18 +121,26 @@ inline static PyObject *eval_custom_code(PyThreadState *tstate,
102
121
fastlocals_new[nlocals_new + i] = fastlocals_old[nlocals_old + i];
103
122
}
104
123
124
+ #if PY_VERSION_HEX >= 0x030b0000
125
+ PyObject *result = eval_frame_default (tstate, shadow ->f_frame , throw_flag);
126
+ #else
105
127
PyObject *result = eval_frame_default (tstate, shadow , throw_flag);
128
+ #endif
106
129
Py_DECREF (shadow );
107
130
return result;
108
131
}
109
132
110
133
static PyObject *_custom_eval_frame (PyThreadState *tstate,
111
- PyFrameObject *frame,
134
+ FrameObject *frame,
112
135
int throw_flag,
113
136
PyObject *callback) {
114
- // https://peps.python.org/pep-0558/#fast-locals-proxy-implementation-details
115
- // https://devguide.python.org/internals/interpreter/#all-sorts-of-variables
137
+ // https://peps.python.org/pep-0558/#fast-locals-proxy-implementation-details
138
+ // https://devguide.python.org/internals/interpreter/#all-sorts-of-variables
139
+ #if PY_VERSION_HEX >= 0x030b0000
140
+ if (PyFrame_FastToLocalsWithError (frame->frame_obj ) < 0 ) {
141
+ #else
116
142
if (PyFrame_FastToLocalsWithError (frame) < 0 ) {
143
+ #endif
117
144
return NULL ;
118
145
}
119
146
@@ -167,7 +194,7 @@ static PyObject *_custom_eval_frame(PyThreadState *tstate,
167
194
}
168
195
169
196
static PyObject *_custom_eval_frame_shim (PyThreadState *tstate,
170
- PyFrameObject *frame,
197
+ FrameObject *frame,
171
198
int throw_flag) {
172
199
PyObject *callback = eval_frame_callback_get ();
173
200
@@ -180,12 +207,12 @@ static PyObject *_custom_eval_frame_shim(PyThreadState *tstate,
180
207
181
208
#if PY_VERSION_HEX >= 0x03090000
182
209
static PyObject *custom_eval_frame_shim (PyThreadState *tstate,
183
- PyFrameObject *frame,
210
+ FrameObject *frame,
184
211
int throw_flag) {
185
212
return _custom_eval_frame_shim (tstate, frame, throw_flag);
186
213
}
187
214
#else
188
- static PyObject *custom_eval_frame_shim (PyFrameObject *frame, int throw_flag) {
215
+ static PyObject *custom_eval_frame_shim (FrameObject *frame, int throw_flag) {
189
216
PyThreadState *tstate = PyThreadState_GET ();
190
217
return _custom_eval_frame_shim (tstate, frame, throw_flag);
191
218
}
0 commit comments