2 from __future__ import print_function
3 from traceback import print_exception
6 py_maj = sys.version_info[0]
14 """Do nothing (already handled by Python 3 infrastructure)."""
17 def chain_ex(ex, context_ex):
25 """Add a traceback to ex if it doesn't already have one. Return ex.
28 if not getattr(ex, '__traceback__', None):
29 ex.__traceback__ = sys.exc_info()[2]
32 def chain_ex(ex, context_ex):
35 if not getattr(ex, '__context__', None):
36 ex.__context__ = context_ex
39 def dump_traceback(ex):
41 next_ex = getattr(ex, '__context__', None)
44 next_ex = getattr(next_ex, '__context__', None)
45 stack = reversed(stack)
47 tb = getattr(ex, '__traceback__', None)
48 print_exception(type(ex), ex, tb)
50 print('\nDuring handling of the above exception, another exception occurred:\n',
52 tb = getattr(ex, '__traceback__', None)
53 print_exception(type(ex), ex, tb)
56 """Run main() and raise a SystemExit with the return value if it
57 returns, pass along any SystemExit it raises, convert
58 KeyboardInterrupts into exit(130), and print a Python 3 style
59 contextual backtrace for other exceptions in both Python 2 and
63 except KeyboardInterrupt as ex:
65 except SystemExit as ex:
67 except BaseException as ex:
75 # Excepting wrap_main() in the traceback, these should produce the same output:
76 # python2 lib/bup/compat.py
77 # python3 lib/bup/compat.py
79 # diff -u <(python2 lib/bup/compat.py 2>&1) <(python3 lib/bup/compat.py 2>&1)
81 if __name__ == '__main__':
84 raise Exception('first')
89 except Exception as ex:
90 raise chain_ex(Exception('second'), ex)