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 """If rethrow is true, rethrow ex (if any), unless the body throws.
45 (Supports Python 2 compatibility.)
48 def __init__(self, ex, rethrow=True):
50 self.rethrow = rethrow
53 def __exit__(self, exc_type, exc_value, traceback):
54 if not exc_type and self.ex and self.rethrow:
61 """Return the original bytes passed to main() for an argv argument."""
64 def bytes_from_uint(i):
67 def bytes_from_byte(b): # python > 2: b[3] returns ord('x'), not b'x'
70 byte_int = lambda x: x
72 def buffer(object, offset=None, size=None):
74 assert offset is not None
75 return memoryview(object)[offset:offset + size]
77 return memoryview(object)[offset:]
78 return memoryview(object)
81 return fsencode(os.getcwd())
85 ModuleNotFoundError = ImportError
93 from pipes import quote
94 # pylint: disable=unused-import
95 from os import environ, getcwd
97 # pylint: disable=unused-import
98 from bup.py2raise import reraise
100 # on py3 this causes errors, obviously
101 # pylint: disable=undefined-variable
103 # pylint: disable=undefined-variable
105 # pylint: disable=undefined-variable
106 str_type = basestring
107 # pylint: disable=undefined-variable
108 int_types = (int, long)
113 """Add a traceback to ex if it doesn't already have one. Return ex.
116 if not getattr(ex, '__traceback__', None):
117 ex.__traceback__ = sys.exc_info()[2]
120 def add_ex_ctx(ex, context_ex):
121 """Make context_ex the __context__ of ex (unless it already has one).
126 if not getattr(ex, '__context__', None):
127 ex.__context__ = context_ex
131 """If rethrow is true, rethrow ex (if any), unless the body throws.
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.)
139 def __init__(self, ex, rethrow=True):
141 self.rethrow = rethrow
145 def __exit__(self, exc_type, exc_value, traceback):
149 add_ex_ctx(exc_value, self.ex)
151 if self.rethrow and self.ex:
154 def dump_traceback(ex):
156 next_ex = getattr(ex, '__context__', None)
158 stack.append(next_ex)
159 next_ex = getattr(next_ex, '__context__', None)
160 stack = reversed(stack)
162 tb = getattr(ex, '__traceback__', None)
163 print_exception(type(ex), ex, tb)
165 print('\nDuring handling of the above exception, another exception occurred:\n',
167 tb = getattr(ex, '__traceback__', None)
168 print_exception(type(ex), ex, tb)
174 """Return the original bytes passed to main() for an argv argument."""
177 bytes_from_uint = chr
179 def bytes_from_byte(b):
188 except ModuleNotFoundError:
193 "Return a new list containing the current process argv bytes."
194 return bup_main.argv()
197 "Return a new list containing the current process argv strings."
198 return [x.decode(errors='surrogateescape') for x in bup_main.argv()]
201 return bup_main.argv()
204 raise Exception('get_argvb requires the bup_main module');
206 raise Exception('get_argv requires the bup_main module');
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
216 except KeyboardInterrupt as ex:
218 except SystemExit as ex:
220 except BaseException as ex:
228 # Excepting wrap_main() in the traceback, these should produce similar output:
229 # python2 lib/bup/compat.py
230 # python3 lib/bup/compat.py
232 # diff -u <(python2 lib/bup/compat.py 2>&1) <(python3 lib/bup/compat.py 2>&1)
234 # Though the python3 output for 'second' will include a stacktrace
235 # starting from wrap_main, rather than from outer().
237 if __name__ == '__main__':
240 raise Exception('first')
245 except Exception as ex:
248 raise Exception('second')
249 except Exception as ex2:
250 raise add_ex_ctx(add_ex_tb(ex2), ex)