-
Notifications
You must be signed in to change notification settings - Fork 22
/
tracer.py
executable file
·30 lines (26 loc) · 1015 Bytes
/
tracer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/python
import traceback
import sys
def format_error_info(exc_type, exc_value, exc_traceback):
"""
Format error information in GNU format (file:line:column).
"""
tb_list = traceback.extract_tb(exc_traceback)
formatted_lines = []
for filename, lineno, funcname, line in reversed(tb_list):
formatted_lines.append(f"{filename}:{lineno}:{line.strip()}")
error_message = f"{exc_type.__name__}: {exc_value}\n" + "\n".join(formatted_lines)
return error_message
def custom_error_handler(exc_type, exc_value, exc_traceback):
"""
Custom error handler to print errors in GNU format.
"""
error_message = format_error_info(exc_type, exc_value, exc_traceback)
sys.stderr.write(error_message + "\n")
# Set the custom error handler
sys.excepthook = custom_error_handler
# Example usage to trigger an error
try:
raise ValueError("This is a sample error") from None
except Exception as e:
pass # The error will be handled by our custom error handler