2 from __future__ import absolute_import, print_function
3 from binascii import hexlify
4 from traceback import print_exception
7 # Please see CODINGSTYLE for important exception handling guidelines
8 # and the rationale behind add_ex_tb(), add_ex_ctx(), etc.
10 py_maj = sys.version_info[0]
15 # pylint: disable=unused-import
16 from contextlib import ExitStack, nullcontext
17 from os import environb as environ
18 from os import fsdecode, fsencode
19 from shlex import quote
20 # pylint: disable=undefined-variable
21 # (for python2 looking here)
22 ModuleNotFoundError = ModuleNotFoundError
29 """Return hex string (not bytes as with hexlify) representation of b."""
33 raise ex.with_traceback(sys.exc_info()[2])
36 """Do nothing (already handled by Python 3 infrastructure)."""
39 def add_ex_ctx(ex, context_ex):
40 """Do nothing (already handled by Python 3 infrastructure)."""
44 """If rethrow is true, rethrow ex (if any), unless the body throws.
46 (Supports Python 2 compatibility.)
49 def __init__(self, ex, rethrow=True):
52 self.rethrow = rethrow
55 def __exit__(self, exc_type, exc_value, traceback):
57 if not exc_type and self.ex and self.rethrow:
66 """Return the original bytes passed to main() for an argv argument."""
69 def bytes_from_uint(i):
72 def bytes_from_byte(b): # python > 2: b[3] returns ord('x'), not b'x'
75 byte_int = lambda x: x
77 def buffer(object, offset=None, size=None):
79 assert offset is not None
80 return memoryview(object)[offset:offset + size]
82 return memoryview(object)[offset:]
83 return memoryview(object)
86 return fsencode(os.getcwd())
90 from contextlib import contextmanager
92 ModuleNotFoundError = ImportError
100 from pipes import quote
101 # pylint: disable=unused-import
102 from os import environ, getcwd
104 # pylint: disable=unused-import
105 from bup.py2raise import reraise
108 def nullcontext(enter_result=None):
111 # on py3 this causes errors, obviously
112 # pylint: disable=undefined-variable
114 # pylint: disable=undefined-variable
116 # pylint: disable=undefined-variable
117 str_type = basestring
118 # pylint: disable=undefined-variable
119 int_types = (int, long)
124 """Add a traceback to ex if it doesn't already have one. Return ex.
127 if not getattr(ex, '__traceback__', None):
128 ex.__traceback__ = sys.exc_info()[2]
131 def add_ex_ctx(ex, context_ex):
132 """Make context_ex the __context__ of ex (unless it already has one).
137 if not getattr(ex, '__context__', None):
138 ex.__context__ = context_ex
142 """If rethrow is true, rethrow ex (if any), unless the body throws.
144 If the body does throw, make any provided ex the __context__
145 of the newer exception (assuming there's no existing
146 __context__). Ensure the exceptions have __tracebacks__.
147 (Supports Python 2 compatibility.)
150 def __init__(self, ex, rethrow=True):
153 self.rethrow = rethrow
157 def __exit__(self, exc_type, exc_value, traceback):
162 add_ex_ctx(exc_value, self.ex)
164 if self.rethrow and self.ex:
169 def dump_traceback(ex):
171 next_ex = getattr(ex, '__context__', None)
173 stack.append(next_ex)
174 next_ex = getattr(next_ex, '__context__', None)
175 stack = reversed(stack)
177 tb = getattr(ex, '__traceback__', None)
178 print_exception(type(ex), ex, tb)
180 print('\nDuring handling of the above exception, another exception occurred:\n',
182 tb = getattr(ex, '__traceback__', None)
183 print_exception(type(ex), ex, tb)
192 def __exit__(self, value_type, value, traceback):
194 for ctx in reversed(self.contexts):
196 ctx.__exit__(value_type, value, traceback)
197 except BaseException as ex:
200 add_ex_ctx(ex, value)
201 value_type = type(ex)
203 traceback = ex.__traceback__
204 if value is not init_value:
207 def enter_context(self, x):
208 self.contexts.append(x)
214 """Return the original bytes passed to main() for an argv argument."""
217 bytes_from_uint = chr
219 def bytes_from_byte(b):
228 except ModuleNotFoundError:
233 "Return a new list containing the current process argv bytes."
234 return bup_main.argv()
237 "Return a new list containing the current process argv strings."
238 return [x.decode(errors='surrogateescape') for x in bup_main.argv()]
241 return bup_main.argv()
244 raise Exception('get_argvb requires the bup_main module');
246 raise Exception('get_argv requires the bup_main module');
249 """Run main() and raise a SystemExit with the return value if it
250 returns, pass along any SystemExit it raises, convert
251 KeyboardInterrupts into exit(130), and print a Python 3 style
252 contextual backtrace for other exceptions in both Python 2 and
256 except KeyboardInterrupt as ex:
258 except SystemExit as ex:
260 except BaseException as ex:
268 # Excepting wrap_main() in the traceback, these should produce similar output:
269 # python2 lib/bup/compat.py
270 # python3 lib/bup/compat.py
272 # diff -u <(python2 lib/bup/compat.py 2>&1) <(python3 lib/bup/compat.py 2>&1)
274 # Though the python3 output for 'second' will include a stacktrace
275 # starting from wrap_main, rather than from outer().
277 if __name__ == '__main__':
280 raise Exception('first')
285 except Exception as ex:
288 raise Exception('second')
289 except Exception as ex2:
290 raise add_ex_ctx(add_ex_tb(ex2), ex)