@@ -43,12 +43,29 @@ limitations under the License.
43
43
44
44
// Introduced in python 3.10
45
45
#if PY_VERSION_HEX < 0x030a00f0
46
- PyObject * Py_NewRef (PyObject *o) {
46
+ PyObject *Py_NewRef (PyObject *o) {
47
47
Py_INCREF (o);
48
48
return o;
49
49
}
50
50
#endif
51
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
+
52
69
namespace mlir ::python {
53
70
struct TracebackEntry ;
54
71
struct TracebackObject ;
@@ -143,9 +160,15 @@ static void traceback_tp_dealloc(PyObject *self) {
143
160
}
144
161
145
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
146
169
return Traceback::Frame{
147
170
.file_name = nb::borrow<nb::str>(frame.code ->co_filename ),
148
- .function_name = nb::borrow<nb::str>(frame. code -> co_qualname ),
171
+ .function_name = nb::borrow<nb::str>(name ),
149
172
.function_start_line = frame.code ->co_firstlineno ,
150
173
.line_num = PyCode_Addr2Line (frame.code , frame.lasti ),
151
174
};
@@ -423,11 +446,13 @@ void BuildTracebackSubmodule(nb::module_ &m) {
423
446
throw std::runtime_error (" code argument must be a code object" );
424
447
}
425
448
int start_line, start_column, end_line, end_column;
426
- if (!PyCode_Addr2Location (reinterpret_cast <PyCodeObject *>(code.ptr ()),
427
- lasti, &start_line, &start_column, &end_line,
428
- &end_column)) {
429
- throw nb::python_error ();
430
- }
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 ();
431
456
return nb::make_tuple (start_line, start_column, end_line, end_column);
432
457
},
433
458
" Python wrapper around the Python C API function PyCode_Addr2Location" );
0 commit comments