2 from __future__ import print_function
3 from traceback import print_exception
6 py_maj = sys.version_info[0]
16 def chain_ex(ex, context_ex):
24 if not getattr(ex, '__traceback__', None):
25 ex.__traceback__ = sys.exc_info()[2]
27 def chain_ex(ex, context_ex):
30 if not getattr(ex, '__context__', None):
31 ex.__context__ = context_ex
34 def dump_traceback(ex):
36 next_ex = getattr(ex, '__context__', None)
39 next_ex = getattr(next_ex, '__context__', None)
40 stack = reversed(stack)
42 tb = getattr(ex, '__traceback__', None)
43 print_exception(type(ex), ex, tb)
45 print('\nDuring handling of the above exception, another exception occurred:\n',
47 tb = getattr(ex, '__traceback__', None)
48 print_exception(type(ex), ex, tb)
51 """Run main() and raise a SystemExit with the return value if it
52 returns, pass along any SystemExit it raises, convert
53 KeyboardInterrupts into exit(130), and print a Python 3 style
54 contextual backtrace for other exceptions in both Python 2 and
58 except KeyboardInterrupt as ex:
60 except SystemExit as ex:
62 except BaseException as ex:
70 # Excepting wrap_main() in the traceback, these should produce the same output:
71 # python2 lib/bup/compat.py
72 # python3 lib/bup/compat.py
74 # diff -u <(python2 lib/bup/compat.py 2>&1) <(python3 lib/bup/compat.py 2>&1)
76 if __name__ == '__main__':
79 raise Exception('first')
84 except Exception as ex:
85 raise chain_ex(Exception('second'), ex)