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 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
28 """Return hex string (not bytes as with hexlify) representation of b."""
32 raise ex.with_traceback(sys.exc_info()[2])
35 """Do nothing (already handled by Python 3 infrastructure)."""
38 def add_ex_ctx(ex, context_ex):
39 """Do nothing (already handled by Python 3 infrastructure)."""
43 """Rethrow either the provided ex, or any exception raised by the with
44 statement body. (Supports Python 2 compatibility.)
47 def __init__(self, ex):
51 def __exit__(self, exc_type, exc_value, traceback):
59 """Return the original bytes passed to main() for an argv argument."""
62 def bytes_from_uint(i):
65 def bytes_from_byte(b): # python > 2: b[3] returns ord('x'), not b'x'
68 byte_int = lambda x: x
70 def buffer(object, offset=None, size=None):
72 assert offset is not None
73 return memoryview(object)[offset:offset + size]
75 return memoryview(object)[offset:]
76 return memoryview(object)
79 return fsencode(os.getcwd())
83 ModuleNotFoundError = ImportError
91 from pipes import quote
92 # pylint: disable=unused-import
93 from os import environ, getcwd
95 # pylint: disable=unused-import
96 from bup.py2raise import reraise
98 # on py3 this causes errors, obviously
99 # pylint: disable=undefined-variable
101 # pylint: disable=undefined-variable
103 # pylint: disable=undefined-variable
104 str_type = basestring
105 # pylint: disable=undefined-variable
106 int_types = (int, long)
111 """Add a traceback to ex if it doesn't already have one. Return ex.
114 if not getattr(ex, '__traceback__', None):
115 ex.__traceback__ = sys.exc_info()[2]
118 def add_ex_ctx(ex, context_ex):
119 """Make context_ex the __context__ of ex (unless it already has one).
124 if not getattr(ex, '__context__', None):
125 ex.__context__ = context_ex
129 """Rethrow either the provided ex, or any exception raised by the with
130 statement body, after making ex the __context__ of the newer
131 exception (assuming there's no existing __context__). Ensure
132 the exceptions have __tracebacks__. (Supports Python 2
136 def __init__(self, ex):
140 def __exit__(self, exc_type, exc_value, traceback):
144 add_ex_ctx(exc_value, self.ex)
146 def dump_traceback(ex):
148 next_ex = getattr(ex, '__context__', None)
150 stack.append(next_ex)
151 next_ex = getattr(next_ex, '__context__', None)
152 stack = reversed(stack)
154 tb = getattr(ex, '__traceback__', None)
155 print_exception(type(ex), ex, tb)
157 print('\nDuring handling of the above exception, another exception occurred:\n',
159 tb = getattr(ex, '__traceback__', None)
160 print_exception(type(ex), ex, tb)
166 """Return the original bytes passed to main() for an argv argument."""
169 bytes_from_uint = chr
171 def bytes_from_byte(b):
180 except ModuleNotFoundError:
185 "Return a new list containing the current process argv bytes."
186 return bup_main.argv()
189 "Return a new list containing the current process argv strings."
190 return [x.decode(errors='surrogateescape') for x in bup_main.argv()]
193 return bup_main.argv()
196 raise Exception('get_argvb requires the bup_main module');
198 raise Exception('get_argv requires the bup_main module');
201 """Run main() and raise a SystemExit with the return value if it
202 returns, pass along any SystemExit it raises, convert
203 KeyboardInterrupts into exit(130), and print a Python 3 style
204 contextual backtrace for other exceptions in both Python 2 and
208 except KeyboardInterrupt as ex:
210 except SystemExit as ex:
212 except BaseException as ex:
220 # Excepting wrap_main() in the traceback, these should produce similar output:
221 # python2 lib/bup/compat.py
222 # python3 lib/bup/compat.py
224 # diff -u <(python2 lib/bup/compat.py 2>&1) <(python3 lib/bup/compat.py 2>&1)
226 # Though the python3 output for 'second' will include a stacktrace
227 # starting from wrap_main, rather than from outer().
229 if __name__ == '__main__':
232 raise Exception('first')
237 except Exception as ex:
240 raise Exception('second')
241 except Exception as ex2:
242 raise add_ex_ctx(add_ex_tb(ex2), ex)