3 # Any copyright is dedicated to the Public Domain.
4 # https://creativecommons.org/publicdomain/zero/1.0/
6 # Written by Francois Fleuret <francois@fleuret.org>
8 from torch import is_tensor
12 def exception_hook(exc_type, exc_value, tb):
17 # x=tb.tb_frame #.f_code
18 # for field in dir(x):
19 # print(f'@@@ {field} {getattr(x, field)}')
20 print('--------------------------------------------------')
21 filename = tb.tb_frame.f_code.co_filename
22 name = tb.tb_frame.f_code.co_name
23 line_no = tb.tb_lineno
24 print(f' File "{filename}", line {line_no}, in {name}')
25 print(open(filename, 'r').readlines()[line_no-1], end='')
27 local_vars = tb.tb_frame.f_locals
29 for n,v in local_vars.items():
31 print(f' {n} -> {tuple(v.size())}:{v.dtype}:{v.device}')
37 print(f'{exc_type.__name__}: {exc_value}')
39 sys.excepthook = exception_hook
41 ######################################################################
43 if __name__ == '__main__':