MCPcopy Create free account
hub / github.com/apache/tvm-ffi / TracebackManager

Class TracebackManager

python/tvm_ffi/error.py:60–135  ·  view source on GitHub ↗

Helper to manage traceback generation.

Source from the content-addressed store, hash-verified

58
59
60class TracebackManager:
61 """Helper to manage traceback generation."""
62
63 def __init__(self) -> None:
64 """Initialize the traceback manager and its cache."""
65 self._code_cache: dict[tuple[str, int, str], types.CodeType] = {}
66
67 def _get_cached_code_object(self, filename: str, lineno: int, func: str) -> types.CodeType:
68 # Hack to create a code object that points to the correct
69 # line number and function name
70 key = (filename, lineno, func)
71 # cache the code object to avoid re-creating it
72 if key in self._code_cache:
73 return self._code_cache[key]
74 # Parse to AST and zero out column info
75 # since column info are not accurate in original trace
76 tree = ast.parse("_getframe()", filename=filename, mode="eval")
77 for node in ast.walk(tree):
78 if hasattr(node, "col_offset"):
79 node.col_offset = 0 # ty: ignore[invalid-assignment]
80 if hasattr(node, "end_col_offset"):
81 node.end_col_offset = 0 # ty: ignore[invalid-assignment]
82 # call into get frame, bt changes the context
83 code_object = compile(tree, filename, "eval")
84 # replace the function name and line number
85 code_object = code_object.replace(co_name=func, co_firstlineno=lineno)
86 self._code_cache[key] = code_object
87 return code_object
88
89 def _create_frame(self, filename: str, lineno: int, func: str) -> types.FrameType:
90 """Create a frame object from the filename, lineno, and func."""
91 code_object = self._get_cached_code_object(filename, lineno, func)
92 # call into get frame, but changes the context so the code
93 # points to the correct frame
94 context = {"_getframe": sys._getframe}
95 # pylint: disable=eval-used
96 return eval(code_object, context, context)
97
98 def append_traceback(
99 self,
100 tb: types.TracebackType | None,
101 filename: str,
102 lineno: int,
103 func: str,
104 ) -> types.TracebackType:
105 """Append a traceback to the given traceback.
106
107 Parameters
108 ----------
109 tb
110 The traceback to append to.
111 filename
112 The filename of the traceback
113 lineno
114 The line number of the traceback
115 func
116 The function name of the traceback
117

Callers 1

error.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected