@@ -41,6 +41,31 @@ limitations under the License.
4141#undef Py_BUILD_CORE
4242#endif // PLATFORM_GOOGLE
4343
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+
4469namespace mlir ::python {
4570struct TracebackEntry ;
4671struct TracebackObject ;
@@ -135,11 +160,17 @@ static void traceback_tp_dealloc(PyObject *self) {
135160}
136161
137162Traceback::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
138169 return Traceback::Frame{
139- . file_name = nb::borrow<nb::str>(frame.code ->co_filename ),
140- . function_name = nb::borrow<nb::str>(frame. code -> co_qualname ),
141- . function_start_line = frame.code ->co_firstlineno ,
142- . line_num = PyCode_Addr2Line (frame.code , frame.lasti ),
170+ /* file_name= */ nb::borrow<nb::str>(frame.code ->co_filename ),
171+ /* function_name= */ nb::borrow<nb::str>(name ),
172+ /* function_start_line= */ frame.code ->co_firstlineno ,
173+ /* line_num= */ PyCode_Addr2Line (frame.code , frame.lasti ),
143174 };
144175}
145176
@@ -415,11 +446,13 @@ void BuildTracebackSubmodule(nb::module_ &m) {
415446 throw std::runtime_error (" code argument must be a code object" );
416447 }
417448 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 ();
423456 return nb::make_tuple (start_line, start_column, end_line, end_column);
424457 },
425458 " Python wrapper around the Python C API function PyCode_Addr2Location" );
0 commit comments