]> arthur.barton.de Git - bup.git/blob - lib/bup/compat.py
CODINGSTYLE: add python exception handling guidelines
[bup.git] / lib / bup / compat.py
1
2 from __future__ import print_function
3 from traceback import print_exception
4 import sys
5
6 # Please see CODINGSTYLE for important exception handling guidelines
7 # and the rationale behind add_ex_tb(), chain_ex(), etc.
8
9 py_maj = sys.version_info[0]
10 py3 = py_maj >= 3
11
12 if py3:
13
14     str_type = str
15
16     def add_ex_tb(ex):
17         """Do nothing (already handled by Python 3 infrastructure)."""
18         return ex
19
20     def chain_ex(ex, context_ex):
21         """Do nothing (already handled by Python 3 infrastructure)."""
22         return ex
23
24 else:  # Python 2
25
26     str_type = basestring
27
28     def add_ex_tb(ex):
29         """Add a traceback to ex if it doesn't already have one.  Return ex.
30
31         """
32         if not getattr(ex, '__traceback__', None):
33             ex.__traceback__ = sys.exc_info()[2]
34         return ex
35
36     def chain_ex(ex, context_ex):
37         """Chain context_ex to ex as the __context__ (unless it already has
38         one).  Return ex.
39
40         """
41         if context_ex:
42             if not getattr(ex, '__context__', None):
43                 ex.__context__ = context_ex
44         return ex
45
46     def dump_traceback(ex):
47         stack = [ex]
48         next_ex = getattr(ex, '__context__', None)
49         while next_ex:
50             stack.append(next_ex)
51             next_ex = getattr(next_ex, '__context__', None)
52         stack = reversed(stack)
53         ex = next(stack)
54         tb = getattr(ex, '__traceback__', None)
55         print_exception(type(ex), ex, tb)
56         for ex in stack:
57             print('\nDuring handling of the above exception, another exception occurred:\n',
58                   file=sys.stderr)
59             tb = getattr(ex, '__traceback__', None)
60             print_exception(type(ex), ex, tb)
61
62 def wrap_main(main):
63     """Run main() and raise a SystemExit with the return value if it
64     returns, pass along any SystemExit it raises, convert
65     KeyboardInterrupts into exit(130), and print a Python 3 style
66     contextual backtrace for other exceptions in both Python 2 and
67     3)."""
68     try:
69         sys.exit(main())
70     except KeyboardInterrupt as ex:
71         sys.exit(130)
72     except SystemExit as ex:
73         raise
74     except BaseException as ex:
75         if py3:
76             raise
77         add_ex_tb(ex)
78         dump_traceback(ex)
79         sys.exit(1)
80
81
82 # Excepting wrap_main() in the traceback, these should produce similar output:
83 #   python2 lib/bup/compat.py
84 #   python3 lib/bup/compat.py
85 # i.e.:
86 #   diff -u <(python2 lib/bup/compat.py 2>&1) <(python3 lib/bup/compat.py 2>&1)
87 #
88 # Though the python3 output for 'second' will include a stacktrace
89 # starting from wrap_main, rather than from outer().
90
91 if __name__ == '__main__':
92
93     def inner():
94         raise Exception('first')
95
96     def outer():
97         try:
98             inner()
99         except Exception as ex:
100             add_ex_tb(ex)
101             try:
102                 raise Exception('second')
103             except Exception as ex2:
104                 raise chain_ex(add_ex_tb(ex2), ex)
105
106     wrap_main(outer)