From 23568a2e55b0858e501be11b440cbb0c2a9f17f9 Mon Sep 17 00:00:00 2001 From: Eric Lunderberg Date: Tue, 26 Sep 2023 15:31:27 -0500 Subject: [PATCH] [TVMScript] Preserve traceback across TVMScript parsing Prior to this commit, exceptions raised during the parsing of TVMScript would be caught and replaced with a new exception. While this does allow the TVMScript location of the error to be included in the exception, it also removes the stack trace of the original error. This commit updates the `Parser.report_error` function to provide the original stack trace alongside the updated exception object. --- python/tvm/script/parser/core/parser.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/python/tvm/script/parser/core/parser.py b/python/tvm/script/parser/core/parser.py index 7b7dd066c3fa..ae79eef1265c 100644 --- a/python/tvm/script/parser/core/parser.py +++ b/python/tvm/script/parser/core/parser.py @@ -522,7 +522,18 @@ def report_error( msg = "KeyError: " + str(err) else: msg = str(err) - self.diag.error(node, msg) + + try: + self.diag.error(node, msg) + except Exception as diag_err: + # Calling self.diag.error is guaranteed to throw an + # exception. When shown to a user, this error should + # reference the point of error within the provided + # TVMScript. However, when caught in pdb, the full + # traceback should be available for debugging. + if isinstance(err, Exception): + diag_err = diag_err.with_traceback(err.__traceback__) + raise diag_err def visit(self, node: doc.AST) -> None: """The general visiting method.