From ba3945e5fcded1fef3179425a7028432aced67de Mon Sep 17 00:00:00 2001 From: Eric Lunderberg Date: Fri, 20 Oct 2023 09:00:26 -0500 Subject: [PATCH 1/2] [FFI][Python] Handle error propagation when line number is missing Prior to this commit, the FFI error propagation in Python assumed that any stack frame with a known file name also had a known line number. This commit updates the error propagation to instead check if a known line number is present. Closes https://github.com/apache/tvm/issues/15880. --- python/tvm/_ffi/base.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/python/tvm/_ffi/base.py b/python/tvm/_ffi/base.py index f0eddf8b3636..7f46caaf41bc 100644 --- a/python/tvm/_ffi/base.py +++ b/python/tvm/_ffi/base.py @@ -24,7 +24,7 @@ import sys import types -from typing import Callable, Sequence +from typing import Callable, Sequence, Optional import numpy as np @@ -340,13 +340,14 @@ def get_last_ffi_error(): return ERROR_TYPE.get(err_type, TVMError)(py_err_msg) -def _append_traceback_frame(tb, func_name, filepath, lineno): +def _append_traceback_frame(tb, func_name, filepath, lineno: Optional[int]): """Append a dummy frame to appear in the Python traceback""" # Compile a dummy function to Python bytecode, so that with the # filepath that we want to appear in the traceback. Any external # debugger (e.g. pdb) that catches the exception will use the # filepath to show code snippets from that FFI file. + header = "" if lineno is None else "\n" * (lineno - 1) code = compile( "{}def dummy_func(): raise NotImplementedError()".format("\n" * (lineno - 1)), filepath, @@ -446,10 +447,14 @@ def raise_last_ffi_error(): for frame in frames: if " at " in frame: func_name, frame = frame.split(" at ", 1) - filename, lineno = frame.rsplit(":", 1) + if ":" in frame: + filename, lineno = frame.rsplit(":", 1) + lineno = int(lineno.strip()) + else: + filename = frame + lineno = None func_name = func_name.strip() filename = filename.strip() - lineno = int(lineno.strip()) tb = _append_traceback_frame(tb, func_name, filename, lineno) From 68603cb940cf8490efe4e88195450f9fb0602dbf Mon Sep 17 00:00:00 2001 From: Eric Lunderberg Date: Mon, 23 Oct 2023 15:38:10 -0500 Subject: [PATCH 2/2] lint fix --- python/tvm/_ffi/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/tvm/_ffi/base.py b/python/tvm/_ffi/base.py index 7f46caaf41bc..b0a63700b79b 100644 --- a/python/tvm/_ffi/base.py +++ b/python/tvm/_ffi/base.py @@ -349,7 +349,7 @@ def _append_traceback_frame(tb, func_name, filepath, lineno: Optional[int]): # filepath to show code snippets from that FFI file. header = "" if lineno is None else "\n" * (lineno - 1) code = compile( - "{}def dummy_func(): raise NotImplementedError()".format("\n" * (lineno - 1)), + f"{header}def dummy_func(): raise NotImplementedError()", filepath, "exec", )