@@ -18,132 +18,25 @@ limitations under the License. */
18
18
19
19
#include "paddle/fluid/pybind/sot/cpython_internals.h"
20
20
#include "paddle/fluid/pybind/sot/eval_frame_tools.h"
21
+ #include "paddle/fluid/pybind/sot/frame_proxy.h"
21
22
22
23
#include <Python.h>
23
- #include <frameobject.h>
24
24
25
- #if PY_3_8_PLUS && PY_VERSION_HEX < PY_3_9_0_HEX
25
+ #if PY_3_8_PLUS && ! PY_3_9_PLUS
26
26
#define Py_BUILD_CORE // internal/pycore_pymem.h need this macro
27
27
#include <internal/pycore_pystate.h>
28
28
#undef Py_BUILD_CORE
29
29
#endif
30
- #if PY_VERSION_HEX < PY_3_11_0_HEX
30
+ #if ! PY_3_11_PLUS
31
31
#include <code.h>
32
32
#endif
33
33
34
34
#include <object.h>
35
35
#include <pystate.h>
36
36
37
37
#if PY_3_11_PLUS
38
- // To avoid the error: undefined symbol: _PyFrame_GetFrameObject, all we need is
39
- // to redefine this function based source code in python3.11. The advantage is
40
- // that we don't need any modification in eval_frame functions.
41
- typedef _PyInterpreterFrame FrameObject ;
42
38
#define CALL_STAT_INC (name ) ((void)0)
43
39
44
- // clang-format off
45
- // Define a proxy PyObject to access _PyInterpreterFrame's properties.
46
- // It will be passed as an argument to the eval frame's callback.
47
- typedef struct PyInterpreterFrameProxy {
48
- PyObject_HEAD
49
- _PyInterpreterFrame * frame ;
50
- #if PY_3_13_PLUS
51
- PyObject * locals ;
52
- #endif
53
- } PyInterpreterFrameProxy ;
54
- // clang-format on
55
-
56
- #define DECLARE_PROXY_PROPERTY (name ) \
57
- static PyObject *PyInterpreterFrameProxy_property_##name( \
58
- PyInterpreterFrameProxy *self, void *closure) { \
59
- Py_XINCREF(self->frame->name); \
60
- return (PyObject *)self->frame->name; \
61
- }
62
-
63
- // clang-format off
64
- #define REGISTER_PROXY_PROPERTY (property_name , func_name ) \
65
- { #property_name, (getter)PyInterpreterFrameProxy_property_##func_name, NULL, NULL, NULL }
66
- // clang-format on
67
-
68
- #if PY_3_13_PLUS
69
- DECLARE_PROXY_PROPERTY (f_executable )
70
- #else
71
- DECLARE_PROXY_PROPERTY (f_code )
72
- #endif
73
- #if PY_3_13_PLUS
74
- static PyObject * PyInterpreterFrameProxy_property_f_locals (
75
- PyInterpreterFrameProxy * self , void * closure ) {
76
- Py_XINCREF (self -> locals );
77
- return self -> locals ;
78
- }
79
- #else
80
- DECLARE_PROXY_PROPERTY (f_locals )
81
- #endif
82
- DECLARE_PROXY_PROPERTY (f_globals )
83
- DECLARE_PROXY_PROPERTY (f_builtins )
84
-
85
- // Refer to
86
- // https://github.com/python/cpython/blob/9414ddf91898892f3f6a672ae946931ee4b3ceb7/Objects/frameobject.c#L953-L961
87
- static PyObject * PyInterpreterFrameProxy_method_repr (
88
- PyInterpreterFrameProxy * self ) {
89
- #if PY_3_13_PLUS
90
- int lineno = Internal_PyUnstable_InterpreterFrame_GetLine (self -> frame );
91
- #else
92
- int lineno = Internal_PyInterpreterFrame_GetLine (self -> frame );
93
- #endif
94
- PyCodeObject * code = PyFrame_GET_CODE (self -> frame );
95
- return PyUnicode_FromFormat (
96
- "<PyInterpreterFrameProxy at %p, file %R, line %d, code %S>" ,
97
- self ,
98
- code -> co_filename ,
99
- lineno ,
100
- code -> co_name );
101
- }
102
-
103
- static PyGetSetDef PyInterpreterFrameProxy_properties [] = {
104
- #if PY_3_13_PLUS
105
- REGISTER_PROXY_PROPERTY (f_code , f_executable ),
106
- #else
107
- REGISTER_PROXY_PROPERTY (f_code , f_code ),
108
- #endif
109
- REGISTER_PROXY_PROPERTY (f_locals , f_locals ),
110
- REGISTER_PROXY_PROPERTY (f_globals , f_globals ),
111
- REGISTER_PROXY_PROPERTY (f_builtins , f_builtins ),
112
- {NULL } /* Sentinel */
113
- };
114
-
115
- // clang-format off
116
- static PyTypeObject PyInterpreterFrameProxyType = {
117
- PyVarObject_HEAD_INIT (NULL , 0 )
118
- .tp_name = "paddle.framework.core.PyInterpreterFrameProxy" ,
119
- .tp_doc = PyDoc_STR ("A proxy object for _PyInterpreterFrame, "
120
- "it's only define all properties we need." ),
121
- .tp_repr = (reprfunc )PyInterpreterFrameProxy_method_repr ,
122
- .tp_basicsize = sizeof (PyInterpreterFrameProxy ),
123
- .tp_itemsize = 0 ,
124
- .tp_flags = Py_TPFLAGS_DEFAULT ,
125
- .tp_getset = PyInterpreterFrameProxy_properties ,
126
- };
127
- // clang-format on
128
-
129
- PyInterpreterFrameProxy * PyInterpreterFrameProxy_New (
130
- _PyInterpreterFrame * frame ) {
131
- PyTypeObject * type = & PyInterpreterFrameProxyType ;
132
- PyInterpreterFrameProxy * self =
133
- (PyInterpreterFrameProxy * )type -> tp_alloc (type , 0 );
134
- if (!self ) {
135
- // VLOG(7) << "Failed to allocate PyInterpreterFrameProxy";
136
- return NULL ;
137
- }
138
- self -> frame = frame ;
139
- #if PY_3_13_PLUS
140
- self -> locals = NULL ;
141
- #endif
142
- return self ;
143
- }
144
-
145
- #else
146
- typedef PyFrameObject FrameObject ;
147
40
#endif
148
41
149
42
#ifdef _WIN32
@@ -556,13 +449,6 @@ PyMODINIT_FUNC PyInit__eval_frame() {
556
449
Py_INCREF (Py_None );
557
450
eval_frame_callback_set (Py_None );
558
451
559
- #if PY_3_11_PLUS
560
- if (PyType_Ready (& PyInterpreterFrameProxyType ) < 0 ) {
561
- // VLOG(7) << "PyInterpreterFrameProxyType has not been ready!";
562
- }
563
- Py_INCREF (& PyInterpreterFrameProxyType );
564
- #endif
565
-
566
452
return NULL ;
567
453
}
568
454
0 commit comments