Skip to content

Commit cdbf62f

Browse files
authored
support py3.11 compile, but unittest is disabled. (#54273)
* function * remove print * add some pystate
1 parent d1d43c2 commit cdbf62f

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

paddle/fluid/pybind/jit.cc

+39-12
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ limitations under the License. */
1717
#include <Python.h>
1818
#include <code.h>
1919
#include <frameobject.h>
20+
21+
#if PY_VERSION_HEX >= 0x030b0000
22+
#include <internal/pycore_frame.h>
23+
#endif
24+
2025
#include <object.h>
2126
#include <pystate.h>
2227

@@ -36,6 +41,12 @@ namespace py = pybind11;
3641
namespace paddle {
3742
namespace pybind {
3843

44+
#if PY_VERSION_HEX >= 0x030b0000
45+
typedef _PyInterpreterFrame FrameObject;
46+
#else
47+
typedef PyFrameObject FrameObject;
48+
#endif
49+
3950
#define unlikely(x) __builtin_expect((x), 0)
4051

4152
// Use static variable to save customed eval hook.
@@ -56,7 +67,7 @@ inline static void eval_frame_callback_set(PyObject *obj) {
5667

5768
// call python default eval frame to interpret current frame.
5869
inline static PyObject *eval_frame_default(PyThreadState *tstate,
59-
PyFrameObject *frame,
70+
FrameObject *frame,
6071
int throw_flag) {
6172
#if PY_VERSION_HEX >= 0x03090000
6273
if (tstate == NULL) {
@@ -71,26 +82,34 @@ inline static PyObject *eval_frame_default(PyThreadState *tstate,
7182
// Start a new frame and run code in this frame.
7283
// Execute a piece of code by default frame-hook.
7384
inline static PyObject *eval_custom_code(PyThreadState *tstate,
74-
PyFrameObject *frame,
85+
FrameObject *frame,
7586
PyCodeObject *code,
7687
int throw_flag) {
7788
Py_ssize_t ncells = 0;
7889
Py_ssize_t nfrees = 0;
7990
Py_ssize_t nlocals_new = code->co_nlocals;
8091
Py_ssize_t nlocals_old = frame->f_code->co_nlocals;
8192

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
86100

87101
PyFrameObject *shadow = PyFrame_New(tstate, code, frame->f_globals, NULL);
88102
if (shadow == NULL) {
89103
return NULL;
90104
}
91105

106+
#if PY_VERSION_HEX >= 0x030b0000
107+
PyObject **fastlocals_old = frame->localsplus;
108+
PyObject **fastlocals_new = shadow->f_frame->localsplus;
109+
#else
92110
PyObject **fastlocals_old = frame->f_localsplus;
93111
PyObject **fastlocals_new = shadow->f_localsplus;
112+
#endif
94113

95114
for (Py_ssize_t i = 0; i < nlocals_old; i++) {
96115
Py_XINCREF(fastlocals_old[i]);
@@ -102,18 +121,26 @@ inline static PyObject *eval_custom_code(PyThreadState *tstate,
102121
fastlocals_new[nlocals_new + i] = fastlocals_old[nlocals_old + i];
103122
}
104123

124+
#if PY_VERSION_HEX >= 0x030b0000
125+
PyObject *result = eval_frame_default(tstate, shadow->f_frame, throw_flag);
126+
#else
105127
PyObject *result = eval_frame_default(tstate, shadow, throw_flag);
128+
#endif
106129
Py_DECREF(shadow);
107130
return result;
108131
}
109132

110133
static PyObject *_custom_eval_frame(PyThreadState *tstate,
111-
PyFrameObject *frame,
134+
FrameObject *frame,
112135
int throw_flag,
113136
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
116142
if (PyFrame_FastToLocalsWithError(frame) < 0) {
143+
#endif
117144
return NULL;
118145
}
119146

@@ -167,7 +194,7 @@ static PyObject *_custom_eval_frame(PyThreadState *tstate,
167194
}
168195

169196
static PyObject *_custom_eval_frame_shim(PyThreadState *tstate,
170-
PyFrameObject *frame,
197+
FrameObject *frame,
171198
int throw_flag) {
172199
PyObject *callback = eval_frame_callback_get();
173200

@@ -180,12 +207,12 @@ static PyObject *_custom_eval_frame_shim(PyThreadState *tstate,
180207

181208
#if PY_VERSION_HEX >= 0x03090000
182209
static PyObject *custom_eval_frame_shim(PyThreadState *tstate,
183-
PyFrameObject *frame,
210+
FrameObject *frame,
184211
int throw_flag) {
185212
return _custom_eval_frame_shim(tstate, frame, throw_flag);
186213
}
187214
#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) {
189216
PyThreadState *tstate = PyThreadState_GET();
190217
return _custom_eval_frame_shim(tstate, frame, throw_flag);
191218
}

test/dygraph_to_static/test_eval_frame.py

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import collections
1717
import unittest
18+
from sys import version_info
1819

1920
import paddle
2021

@@ -27,6 +28,12 @@ def tearDown(self):
2728
pass
2829

2930
def test_eval_frame(self):
31+
if version_info.major != 3 or (
32+
version_info.minor <= 8 or version_info.minor >= 11
33+
):
34+
# print("skip test_eval_frame, current only support 3.8 - 3.10")
35+
return
36+
3037
CustomCode = collections.namedtuple(
3138
"CustomCode", ["code", "disable_eval_frame"]
3239
)

0 commit comments

Comments
 (0)