2 from __future__ import print_function
3 from traceback import print_exception
6 py_maj = sys.version_info[0]
14 def chain_ex(ex, context_ex):
20 if not getattr(ex, '__traceback__', None):
21 ex.__traceback__ = sys.exc_info()[2]
23 def chain_ex(ex, context_ex):
26 if not getattr(ex, '__context__', None):
27 ex.__context__ = context_ex
30 def dump_traceback(ex):
32 next_ex = getattr(ex, '__context__', None)
35 next_ex = getattr(next_ex, '__context__', None)
36 stack = reversed(stack)
38 tb = getattr(ex, '__traceback__', None)
39 print_exception(type(ex), ex, tb)
41 print('\nDuring handling of the above exception, another exception occurred:\n',
43 tb = getattr(ex, '__traceback__', None)
44 print_exception(type(ex), ex, tb)
47 """Run main() and raise a SystemExit with the return value if it
48 returns, pass along any SystemExit it raises, convert
49 KeyboardInterrupts into exit(130), and print a Python 3 style
50 contextual backtrace for other exceptions in both Python 2 and
54 except KeyboardInterrupt as ex:
56 except SystemExit as ex:
58 except BaseException as ex:
66 # Excepting wrap_main() in the traceback, these should produce the same output:
67 # python2 lib/bup/compat.py
68 # python3 lib/bup/compat.py
70 # diff -u <(python2 lib/bup/compat.py 2>&1) <(python3 lib/bup/compat.py 2>&1)
72 if __name__ == '__main__':
75 raise Exception('first')
80 except Exception as ex:
81 raise chain_ex(Exception('second'), ex)