Skip to content

Commit 46adb54

Browse files
committed
compat patches
1 parent ebf792d commit 46adb54

File tree

1 file changed

+39
-6
lines changed

1 file changed

+39
-6
lines changed

mlir/lib/Bindings/Python/Traceback.cpp

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
4469
namespace mlir::python {
4570
struct TracebackEntry;
4671
struct TracebackObject;
@@ -135,9 +160,15 @@ static void traceback_tp_dealloc(PyObject *self) {
135160
}
136161

137162
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
138169
return Traceback::Frame{
139170
.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),
141172
.function_start_line = frame.code->co_firstlineno,
142173
.line_num = PyCode_Addr2Line(frame.code, frame.lasti),
143174
};
@@ -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

Comments
 (0)