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 nullcontext
18 from os import environb as environ
19 from os import fsdecode, fsencode
20 from shlex import quote
21 # pylint: disable=undefined-variable
22 # (for python2 looking here)
23 ModuleNotFoundError = ModuleNotFoundError
30 """Return hex string (not bytes as with hexlify) representation of b."""
34 raise ex.with_traceback(sys.exc_info()[2])
37 """Do nothing (already handled by Python 3 infrastructure)."""
40 def add_ex_ctx(ex, context_ex):
41 """Do nothing (already handled by Python 3 infrastructure)."""
45 """If rethrow is true, rethrow ex (if any), unless the body throws.
47 (Supports Python 2 compatibility.)
50 def __init__(self, ex, rethrow=True):
52 self.rethrow = rethrow
55 def __exit__(self, exc_type, exc_value, traceback):
56 if not exc_type and self.ex and self.rethrow:
63 """Return the original bytes passed to main() for an argv argument."""
66 def bytes_from_uint(i):
69 def bytes_from_byte(b): # python > 2: b[3] returns ord('x'), not b'x'
72 byte_int = lambda x: x
74 def buffer(object, offset=None, size=None):
76 assert offset is not None
77 return memoryview(object)[offset:offset + size]
79 return memoryview(object)[offset:]
80 return memoryview(object)
83 return fsencode(os.getcwd())
87 from contextlib import contextmanager
88 import mmap as py_mmap
90 ModuleNotFoundError = ImportError
98 from pipes import quote
99 # pylint: disable=unused-import
100 from os import environ, getcwd
102 # pylint: disable=unused-import
103 from bup.py2raise import reraise
106 def nullcontext(enter_result=None):
109 # on py3 this causes errors, obviously
110 # pylint: disable=undefined-variable
112 # pylint: disable=undefined-variable
114 # pylint: disable=undefined-variable
115 str_type = basestring
116 # pylint: disable=undefined-variable
117 int_types = (int, long)
122 """Add a traceback to ex if it doesn't already have one. Return ex.
125 if not getattr(ex, '__traceback__', None):
126 ex.__traceback__ = sys.exc_info()[2]
129 def add_ex_ctx(ex, context_ex):
130 """Make context_ex the __context__ of ex (unless it already has one).
135 if not getattr(ex, '__context__', None):
136 ex.__context__ = context_ex
140 """If rethrow is true, rethrow ex (if any), unless the body throws.
142 If the body does throw, make any provided ex the __context__
143 of the newer exception (assuming there's no existing
144 __context__). Ensure the exceptions have __tracebacks__.
145 (Supports Python 2 compatibility.)
148 def __init__(self, ex, rethrow=True):
150 self.rethrow = rethrow
154 def __exit__(self, exc_type, exc_value, traceback):
158 add_ex_ctx(exc_value, self.ex)
160 if self.rethrow and self.ex:
163 def dump_traceback(ex):
165 next_ex = getattr(ex, '__context__', None)
167 stack.append(next_ex)
168 next_ex = getattr(next_ex, '__context__', None)
169 stack = reversed(stack)
171 tb = getattr(ex, '__traceback__', None)
172 print_exception(type(ex), ex, tb)
174 print('\nDuring handling of the above exception, another exception occurred:\n',
176 tb = getattr(ex, '__traceback__', None)
177 print_exception(type(ex), ex, tb)
183 """Return the original bytes passed to main() for an argv argument."""
186 bytes_from_uint = chr
188 def bytes_from_byte(b):
195 assert not hasattr(py_mmap.mmap, '__enter__')
196 assert not hasattr(py_mmap.mmap, '__exit__')
198 class mmap(py_mmap.mmap):
201 def __exit__(self, type, value, traceback):
202 with pending_raise(value, rethrow=False):
207 except ModuleNotFoundError:
212 "Return a new list containing the current process argv bytes."
213 return bup_main.argv()
216 "Return a new list containing the current process argv strings."
217 return [x.decode(errors='surrogateescape') for x in bup_main.argv()]
220 return bup_main.argv()
223 raise Exception('get_argvb requires the bup_main module');
225 raise Exception('get_argv requires the bup_main module');
228 """Run main() and raise a SystemExit with the return value if it
229 returns, pass along any SystemExit it raises, convert
230 KeyboardInterrupts into exit(130), and print a Python 3 style
231 contextual backtrace for other exceptions in both Python 2 and
235 except KeyboardInterrupt as ex:
237 except SystemExit as ex:
239 except BaseException as ex:
247 # Excepting wrap_main() in the traceback, these should produce similar output:
248 # python2 lib/bup/compat.py
249 # python3 lib/bup/compat.py
251 # diff -u <(python2 lib/bup/compat.py 2>&1) <(python3 lib/bup/compat.py 2>&1)
253 # Though the python3 output for 'second' will include a stacktrace
254 # starting from wrap_main, rather than from outer().
256 if __name__ == '__main__':
259 raise Exception('first')
264 except Exception as ex:
267 raise Exception('second')
268 except Exception as ex2:
269 raise add_ex_ctx(add_ex_tb(ex2), ex)