From: Siu Kwan Lam <1929845+sklam@users.noreply.github.com>
Origin: https://github.com/numba/numba/pull/8545
Date: Wed, 2 Nov 2022 15:31:10 -0500
Subject: Fix try except in py<3.11

---
 numba/_helperlib.c        | 13 ++++++++++++-
 numba/core/interpreter.py |  4 +++-
 2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/numba/_helperlib.c b/numba/_helperlib.c
index 705404d..2e078ba 100644
--- a/numba/_helperlib.c
+++ b/numba/_helperlib.c
@@ -819,8 +819,12 @@ static void traceback_add(const char *funcname, const char *filename, int lineno
     Py_DECREF(code);
     if (!frame)
         goto error;
-    //frame->f_lineno = lineno;
 
+#if (PY_MAJOR_VERSION >= 3) && (PY_MINOR_VERSION >= 11)
+    // empty
+#else
+    frame->f_lineno = lineno;
+#endif
     PyErr_Restore(exc, val, tb);
     PyTraceBack_Here(frame);
     Py_DECREF(frame);
@@ -872,9 +876,16 @@ int reraise_exc_is_none(void) {
 #else
     PyThreadState *tstate_exc = tstate;
 #endif
+
+#if (PY_MAJOR_VERSION >= 3) && (PY_MINOR_VERSION >= 11)
     type = tstate->curexc_type;
     value = tstate->curexc_value;
     tb = tstate->curexc_traceback;
+#else
+    type = tstate_exc->exc_type;
+    value = tstate_exc->exc_value;
+    tb = tstate_exc->exc_traceback;
+#endif
     if (type == Py_None) {
         PyErr_SetString(PyExc_RuntimeError,
                         "No active exception to reraise");
diff --git a/numba/core/interpreter.py b/numba/core/interpreter.py
index ba670bd..2e354d0 100644
--- a/numba/core/interpreter.py
+++ b/numba/core/interpreter.py
@@ -1379,7 +1379,8 @@ class Interpreter(object):
         # Interpret loop
         for inst, kws in self._iter_inst():
             self._dispatch(inst, kws)
-        self._end_try_blocks()
+        if PYVERSION == (3, 11):
+            self._end_try_blocks()
         self._legalize_exception_vars()
         # Prepare FunctionIR
         func_ir = ir.FunctionIR(self.blocks, self.is_generator, self.func_id,
@@ -1413,6 +1414,7 @@ class Interpreter(object):
         return func_ir
 
     def _end_try_blocks(self):
+        assert PYVERSION == (3, 11)
         graph = self.cfa.graph
         for offset, block in self.blocks.items():
             cur_bs = inc_bs = self.dfa.infos[offset].blockstack
