]> arthur.barton.de Git - bup.git/blob - lib/bup/compat.py
compat.pending_raise: allow/ignore None ex; make rethrow optional
[bup.git] / lib / bup / compat.py
1
2 from __future__ import absolute_import, print_function
3 from binascii import hexlify
4 from traceback import print_exception
5 import os, sys
6
7 # Please see CODINGSTYLE for important exception handling guidelines
8 # and the rationale behind add_ex_tb(), add_ex_ctx(), etc.
9
10 py_maj = sys.version_info[0]
11 py3 = py_maj >= 3
12
13 if py3:
14
15     # pylint: disable=unused-import
16     from os import environb as environ
17     from os import fsdecode, fsencode
18     from shlex import quote
19     # pylint: disable=undefined-variable
20     # (for python2 looking here)
21     ModuleNotFoundError = ModuleNotFoundError
22     input = input
23     range = range
24     str_type = str
25     int_types = (int,)
26
27     def hexstr(b):
28         """Return hex string (not bytes as with hexlify) representation of b."""
29         return b.hex()
30
31     def reraise(ex):
32         raise ex.with_traceback(sys.exc_info()[2])
33
34     def add_ex_tb(ex):
35         """Do nothing (already handled by Python 3 infrastructure)."""
36         return ex
37
38     def add_ex_ctx(ex, context_ex):
39         """Do nothing (already handled by Python 3 infrastructure)."""
40         return ex
41
42     class pending_raise:
43         """If rethrow is true, rethrow ex (if any), unless the body throws.
44
45         (Supports Python 2 compatibility.)
46
47         """
48         def __init__(self, ex, rethrow=True):
49             self.ex = ex
50             self.rethrow = rethrow
51         def __enter__(self):
52             return None
53         def __exit__(self, exc_type, exc_value, traceback):
54             if not exc_type and self.ex and self.rethrow:
55                 raise self.ex
56
57     def items(x):
58         return x.items()
59
60     def argv_bytes(x):
61         """Return the original bytes passed to main() for an argv argument."""
62         return fsencode(x)
63
64     def bytes_from_uint(i):
65         return bytes((i,))
66
67     def bytes_from_byte(b):  # python > 2: b[3] returns ord('x'), not b'x'
68         return bytes((b,))
69
70     byte_int = lambda x: x
71
72     def buffer(object, offset=None, size=None):
73         if size:
74             assert offset is not None
75             return memoryview(object)[offset:offset + size]
76         if offset:
77             return memoryview(object)[offset:]
78         return memoryview(object)
79
80     def getcwd():
81         return fsencode(os.getcwd())
82
83 else:  # Python 2
84
85     ModuleNotFoundError = ImportError
86
87     def fsdecode(x):
88         return x
89
90     def fsencode(x):
91         return x
92
93     from pipes import quote
94     # pylint: disable=unused-import
95     from os import environ, getcwd
96
97     # pylint: disable=unused-import
98     from bup.py2raise import reraise
99
100     # on py3 this causes errors, obviously
101     # pylint: disable=undefined-variable
102     input = raw_input
103     # pylint: disable=undefined-variable
104     range = xrange
105     # pylint: disable=undefined-variable
106     str_type = basestring
107     # pylint: disable=undefined-variable
108     int_types = (int, long)
109
110     hexstr = hexlify
111
112     def add_ex_tb(ex):
113         """Add a traceback to ex if it doesn't already have one.  Return ex.
114
115         """
116         if not getattr(ex, '__traceback__', None):
117             ex.__traceback__ = sys.exc_info()[2]
118         return ex
119
120     def add_ex_ctx(ex, context_ex):
121         """Make context_ex the __context__ of ex (unless it already has one).
122         Return ex.
123
124         """
125         if context_ex:
126             if not getattr(ex, '__context__', None):
127                 ex.__context__ = context_ex
128         return ex
129
130     class pending_raise:
131         """If rethrow is true, rethrow ex (if any), unless the body throws.
132
133         If the body does throw, make any provided ex the __context__
134         of the newer exception (assuming there's no existing
135         __context__).  Ensure the exceptions have __tracebacks__.
136         (Supports Python 2 compatibility.)
137
138         """
139         def __init__(self, ex, rethrow=True):
140             self.ex = ex
141             self.rethrow = rethrow
142         def __enter__(self):
143             if self.ex:
144                 add_ex_tb(self.ex)
145         def __exit__(self, exc_type, exc_value, traceback):
146             if exc_value:
147                 if self.ex:
148                     add_ex_tb(exc_value)
149                     add_ex_ctx(exc_value, self.ex)
150                 return
151             if self.rethrow and self.ex:
152                 raise self.ex
153
154     def dump_traceback(ex):
155         stack = [ex]
156         next_ex = getattr(ex, '__context__', None)
157         while next_ex:
158             stack.append(next_ex)
159             next_ex = getattr(next_ex, '__context__', None)
160         stack = reversed(stack)
161         ex = next(stack)
162         tb = getattr(ex, '__traceback__', None)
163         print_exception(type(ex), ex, tb)
164         for ex in stack:
165             print('\nDuring handling of the above exception, another exception occurred:\n',
166                   file=sys.stderr)
167             tb = getattr(ex, '__traceback__', None)
168             print_exception(type(ex), ex, tb)
169
170     def items(x):
171         return x.iteritems()
172
173     def argv_bytes(x):
174         """Return the original bytes passed to main() for an argv argument."""
175         return x
176
177     bytes_from_uint = chr
178
179     def bytes_from_byte(b):
180         return b
181
182     byte_int = ord
183
184     buffer = buffer
185
186 try:
187     import bup_main
188 except ModuleNotFoundError:
189     bup_main = None
190
191 if bup_main:
192     def get_argvb():
193         "Return a new list containing the current process argv bytes."
194         return bup_main.argv()
195     if py3:
196         def get_argv():
197             "Return a new list containing the current process argv strings."
198             return [x.decode(errors='surrogateescape') for x in bup_main.argv()]
199     else:
200         def get_argv():
201             return bup_main.argv()
202 else:
203     def get_argvb():
204         raise Exception('get_argvb requires the bup_main module');
205     def get_argv():
206         raise Exception('get_argv requires the bup_main module');
207
208 def wrap_main(main):
209     """Run main() and raise a SystemExit with the return value if it
210     returns, pass along any SystemExit it raises, convert
211     KeyboardInterrupts into exit(130), and print a Python 3 style
212     contextual backtrace for other exceptions in both Python 2 and
213     3)."""
214     try:
215         sys.exit(main())
216     except KeyboardInterrupt as ex:
217         sys.exit(130)
218     except SystemExit as ex:
219         raise
220     except BaseException as ex:
221         if py3:
222             raise
223         add_ex_tb(ex)
224         dump_traceback(ex)
225         sys.exit(1)
226
227
228 # Excepting wrap_main() in the traceback, these should produce similar output:
229 #   python2 lib/bup/compat.py
230 #   python3 lib/bup/compat.py
231 # i.e.:
232 #   diff -u <(python2 lib/bup/compat.py 2>&1) <(python3 lib/bup/compat.py 2>&1)
233 #
234 # Though the python3 output for 'second' will include a stacktrace
235 # starting from wrap_main, rather than from outer().
236
237 if __name__ == '__main__':
238
239     def inner():
240         raise Exception('first')
241
242     def outer():
243         try:
244             inner()
245         except Exception as ex:
246             add_ex_tb(ex)
247             try:
248                 raise Exception('second')
249             except Exception as ex2:
250                 raise add_ex_ctx(add_ex_tb(ex2), ex)
251
252     wrap_main(outer)