This is an automated email from the ASF dual-hosted git repository.

junrushao pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm.git


The following commit(s) were added to refs/heads/main by this push:
     new c6f2816b58 [FFI][Python] Handle error propagation when line number is 
missing (#15955)
c6f2816b58 is described below

commit c6f2816b589001dd638020c44ffa312d893fade9
Author: Eric Lunderberg <[email protected]>
AuthorDate: Mon Oct 30 13:51:44 2023 -0500

    [FFI][Python] Handle error propagation when line number is missing (#15955)
    
    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 | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/python/tvm/_ffi/base.py b/python/tvm/_ffi/base.py
index f0eddf8b36..b0a63700b7 100644
--- a/python/tvm/_ffi/base.py
+++ b/python/tvm/_ffi/base.py
@@ -24,7 +24,7 @@ import re
 import sys
 import types
 
-from typing import Callable, Sequence
+from typing import Callable, Sequence, Optional
 
 import numpy as np
 
@@ -340,15 +340,16 @@ 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)),
+        f"{header}def dummy_func(): raise NotImplementedError()",
         filepath,
         "exec",
     )
@@ -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)
 

Reply via email to