2 from __future__ import absolute_import, print_function
3 from traceback import print_exception
6 # Please see CODINGSTYLE for important exception handling guidelines
7 # and the rationale behind add_ex_tb(), chain_ex(), etc.
9 py_maj = sys.version_info[0]
18 """Do nothing (already handled by Python 3 infrastructure)."""
21 def chain_ex(ex, context_ex):
22 """Do nothing (already handled by Python 3 infrastructure)."""
34 """Add a traceback to ex if it doesn't already have one. Return ex.
37 if not getattr(ex, '__traceback__', None):
38 ex.__traceback__ = sys.exc_info()[2]
41 def chain_ex(ex, context_ex):
42 """Chain context_ex to ex as the __context__ (unless it already has
47 if not getattr(ex, '__context__', None):
48 ex.__context__ = context_ex
51 def dump_traceback(ex):
53 next_ex = getattr(ex, '__context__', None)
56 next_ex = getattr(next_ex, '__context__', None)
57 stack = reversed(stack)
59 tb = getattr(ex, '__traceback__', None)
60 print_exception(type(ex), ex, tb)
62 print('\nDuring handling of the above exception, another exception occurred:\n',
64 tb = getattr(ex, '__traceback__', None)
65 print_exception(type(ex), ex, tb)
72 """Run main() and raise a SystemExit with the return value if it
73 returns, pass along any SystemExit it raises, convert
74 KeyboardInterrupts into exit(130), and print a Python 3 style
75 contextual backtrace for other exceptions in both Python 2 and
79 except KeyboardInterrupt as ex:
81 except SystemExit as ex:
83 except BaseException as ex:
91 # Excepting wrap_main() in the traceback, these should produce similar output:
92 # python2 lib/bup/compat.py
93 # python3 lib/bup/compat.py
95 # diff -u <(python2 lib/bup/compat.py 2>&1) <(python3 lib/bup/compat.py 2>&1)
97 # Though the python3 output for 'second' will include a stacktrace
98 # starting from wrap_main, rather than from outer().
100 if __name__ == '__main__':
103 raise Exception('first')
108 except Exception as ex:
111 raise Exception('second')
112 except Exception as ex2:
113 raise chain_ex(add_ex_tb(ex2), ex)