-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_excepthook.py
58 lines (52 loc) · 1.67 KB
/
my_excepthook.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
## @package constants
# For better error messages
import sys
import traceback
import os
import re
DARK_BLUE="\033[34m"
RED = '\033[91m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
LIGHT_PURPLE = '\033[94m'
PURPLE = '\033[95m'
CYAN = '\033[96m'
END = '\033[0m'
##
# @brief overwrite the system exception output for a more responsive answer
#
# @param type
# @param value
# @param tb
def my_excepthook(type, value, tb):
lines = traceback.format_list(traceback.extract_tb(tb))
def shorten(match):
return 'File "{}"'.format(os.path.basename(match.group(1)))
lines = [re.sub(r'File "([^"]+)"', shorten, line) for line in lines]
_print_color(lines)
# print(''.join(lines))
print(RED + '{}: {}'.format(type.__name__, value) + END)
print("--------------------------------------------")
sys.excepthook = my_excepthook
def _print_color(lines):
for l in lines:
for i in range(len(l)-1):
if l[i:i+4]=="line":
i +=5
# Find the length of the number
numLen = 0
while l[i+numLen].isdigit():
numLen +=1
# Find the length of the function
funLen = 0
while not l[i+numLen+4 + funLen]=="\n":
funLen+=1
l = ''.join([DARK_BLUE+"->"+END,
l[2:i],
YELLOW+"{}".format(l[i:i+numLen])+END,
l[i+numLen:i+numLen+5],
LIGHT_PURPLE+"{}".format(l[i+numLen+5:i+numLen+5+funLen])+END,
CYAN+"{}".format(l[i+numLen+5+funLen:])+END])
print(l,end="")
break
print("")