@@ -2786,13 +2786,71 @@ class PyOpAttributeMap {
27862786 PyOperationRef operation;
27872787};
27882788
2789- // bpo-40429 added PyThreadState_GetFrame() to Python 3.9.0b1
2789+ // see
2790+ // https://raw.githubusercontent.com/python/pythoncapi_compat/master/pythoncapi_compat.h
2791+
2792+ #ifndef _Py_CAST
2793+ #define _Py_CAST (type, expr ) ((type)(expr))
2794+ #endif
2795+
2796+ // Static inline functions should use _Py_NULL rather than using directly NULL
2797+ // to prevent C++ compiler warnings. On C23 and newer and on C++11 and newer,
2798+ // _Py_NULL is defined as nullptr.
2799+ #ifndef _Py_NULL
2800+ #if (defined(__STDC_VERSION__) && __STDC_VERSION__ > 201710L) || \
2801+ (defined (__cplusplus) && __cplusplus >= 201103 )
2802+ #define _Py_NULL nullptr
2803+ #else
2804+ #define _Py_NULL NULL
2805+ #endif
2806+ #endif
2807+
2808+ // Python 3.10.0a3
2809+ #if PY_VERSION_HEX < 0x030A00A3
2810+
2811+ // bpo-42262 added Py_XNewRef()
2812+ #if !defined(Py_XNewRef)
2813+ PyObject *_Py_XNewRef (PyObject *obj) {
2814+ Py_XINCREF (obj);
2815+ return obj;
2816+ }
2817+ #define Py_XNewRef (obj ) _Py_XNewRef(_PyObject_CAST(obj))
2818+ #endif
2819+
2820+ // bpo-42262 added Py_NewRef()
2821+ #if !defined(Py_NewRef)
2822+ PyObject *_Py_NewRef (PyObject *obj) {
2823+ Py_INCREF (obj);
2824+ return obj;
2825+ }
2826+ #define Py_NewRef (obj ) _Py_NewRef(_PyObject_CAST(obj))
2827+ #endif
2828+
2829+ #endif // Python 3.10.0a3
2830+
2831+ // Python 3.9.0b1
27902832#if PY_VERSION_HEX < 0x030900B1 && !defined(PYPY_VERSION)
2791- static inline PyFrameObject *PyThreadState_GetFrame (PyThreadState *tstate) {
2792- assert (tstate != _Py_NULL);
2833+
2834+ // bpo-40429 added PyThreadState_GetFrame()
2835+ PyFrameObject *PyThreadState_GetFrame (PyThreadState *tstate) {
2836+ assert (tstate != _Py_NULL && " expected tstate != _Py_NULL" );
27932837 return _Py_CAST (PyFrameObject *, Py_XNewRef (tstate->frame ));
27942838}
2795- #endif
2839+
2840+ // bpo-40421 added PyFrame_GetBack()
2841+ PyFrameObject *PyFrame_GetBack (PyFrameObject *frame) {
2842+ assert (frame != _Py_NULL && " expected frame != _Py_NULL" );
2843+ return _Py_CAST (PyFrameObject *, Py_XNewRef (frame->f_back ));
2844+ }
2845+
2846+ // bpo-40421 added PyFrame_GetCode()
2847+ PyCodeObject *PyFrame_GetCode (PyFrameObject *frame) {
2848+ assert (frame != _Py_NULL && " expected frame != _Py_NULL" );
2849+ assert (frame->f_code != _Py_NULL && " expected frame->f_code != _Py_NULL" );
2850+ return _Py_CAST (PyCodeObject *, Py_NewRef (frame->f_code ));
2851+ }
2852+
2853+ #endif // Python 3.9.0b1
27962854
27972855MlirLocation tracebackToLocation (MlirContext ctx) {
27982856 size_t framesLimit =
@@ -2820,15 +2878,15 @@ MlirLocation tracebackToLocation(MlirContext ctx) {
28202878 if (!PyGlobals::get ().getTracebackLoc ().isUserTracebackFilename (fileName))
28212879 continue ;
28222880
2823- #if PY_VERSION_HEX < 0x030b00f0
2881+ // co_qualname and PyCode_Addr2Location added in py3.11
2882+ #if PY_VERSION_HEX < 0x030B00F0
28242883 std::string name =
28252884 nb::cast<std::string>(nb::borrow<nb::str>(code->co_name ));
28262885 llvm::StringRef funcName (name);
28272886 int startLine = PyFrame_GetLineNumber (pyFrame);
28282887 MlirLocation loc =
28292888 mlirLocationFileLineColGet (ctx, wrap (fileName), startLine, 0 );
28302889#else
2831- // co_qualname and PyCode_Addr2Location added in py3.11
28322890 std::string name =
28332891 nb::cast<std::string>(nb::borrow<nb::str>(code->co_qualname ));
28342892 llvm::StringRef funcName (name);
0 commit comments