@@ -41,6 +41,31 @@ limitations under the License.
41
41
#undef Py_BUILD_CORE
42
42
#endif // PLATFORM_GOOGLE
43
43
44
+ // Introduced in python 3.10
45
+ #if PY_VERSION_HEX < 0x030a00f0
46
+ PyObject *Py_NewRef (PyObject *o) {
47
+ Py_INCREF (o);
48
+ return o;
49
+ }
50
+ #endif
51
+
52
+ // bpo-40421 added PyFrame_GetLasti() to Python 3.11.0b1
53
+ #if PY_VERSION_HEX < 0x030B00B1 && !defined(PYPY_VERSION)
54
+ static inline int PyFrame_GetLasti (PyFrameObject *frame) {
55
+ #if PY_VERSION_HEX >= 0x030A00A7
56
+ // bpo-27129: Since Python 3.10.0a7, f_lasti is an instruction offset,
57
+ // not a bytes offset anymore. Python uses 16-bit "wordcode" (2 bytes)
58
+ // instructions.
59
+ if (frame->f_lasti < 0 ) {
60
+ return -1 ;
61
+ }
62
+ return frame->f_lasti * 2 ;
63
+ #else
64
+ return frame->f_lasti ;
65
+ #endif
66
+ }
67
+ #endif
68
+
44
69
namespace mlir ::python {
45
70
struct TracebackEntry ;
46
71
struct TracebackObject ;
@@ -135,9 +160,15 @@ static void traceback_tp_dealloc(PyObject *self) {
135
160
}
136
161
137
162
Traceback::Frame DecodeFrame (const TracebackEntry &frame) {
163
+ // python 3.11
164
+ #if PY_VERSION_HEX < 0x030b00f0
165
+ PyObject *name = frame.code ->co_name ;
166
+ #else
167
+ PyObject *name = frame.code ->co_qualname ;
168
+ #endif
138
169
return Traceback::Frame{
139
170
.file_name = nb::borrow<nb::str>(frame.code ->co_filename ),
140
- .function_name = nb::borrow<nb::str>(frame. code -> co_qualname ),
171
+ .function_name = nb::borrow<nb::str>(name ),
141
172
.function_start_line = frame.code ->co_firstlineno ,
142
173
.line_num = PyCode_Addr2Line (frame.code , frame.lasti ),
143
174
};
@@ -415,11 +446,13 @@ void BuildTracebackSubmodule(nb::module_ &m) {
415
446
throw std::runtime_error (" code argument must be a code object" );
416
447
}
417
448
int start_line, start_column, end_line, end_column;
418
- if (!PyCode_Addr2Location (reinterpret_cast <PyCodeObject *>(code.ptr ()),
419
- lasti, &start_line, &start_column, &end_line,
420
- &end_column)) {
421
- throw nb::python_error ();
422
- }
449
+ // if (!PyCode_Addr2Location(reinterpret_cast<PyCodeObject
450
+ // *>(code.ptr()),
451
+ // lasti, &start_line, &start_column,
452
+ // &end_line, &end_column)) {
453
+ // throw nb::python_error();
454
+ // }
455
+ throw nb::python_error ();
423
456
return nb::make_tuple (start_line, start_column, end_line, end_column);
424
457
},
425
458
" Python wrapper around the Python C API function PyCode_Addr2Location" );
0 commit comments