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
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):
53 self.rethrow = rethrow
56 def __exit__(self, exc_type, exc_value, traceback):
58 if not exc_type and self.ex and self.rethrow:
67 """Return the original bytes passed to main() for an argv argument."""
70 def bytes_from_uint(i):
73 def bytes_from_byte(b): # python > 2: b[3] returns ord('x'), not b'x'
76 byte_int = lambda x: x
78 def buffer(object, offset=None, size=None):
80 assert offset is not None
81 return memoryview(object)[offset:offset + size]
83 return memoryview(object)[offset:]
84 return memoryview(object)
87 return fsencode(os.getcwd())
91 from contextlib import contextmanager
92 import mmap as py_mmap
94 ModuleNotFoundError = ImportError
102 from pipes import quote
103 # pylint: disable=unused-import
104 from os import environ, getcwd
106 # pylint: disable=unused-import
107 from bup.py2raise import reraise
110 def nullcontext(enter_result=None):
113 # on py3 this causes errors, obviously
114 # pylint: disable=undefined-variable
116 # pylint: disable=undefined-variable
118 # pylint: disable=undefined-variable
119 str_type = basestring
120 # pylint: disable=undefined-variable
121 int_types = (int, long)
126 """Add a traceback to ex if it doesn't already have one. Return ex.
129 if not getattr(ex, '__traceback__', None):
130 ex.__traceback__ = sys.exc_info()[2]
133 def add_ex_ctx(ex, context_ex):
134 """Make context_ex the __context__ of ex (unless it already has one).
139 if not getattr(ex, '__context__', None):
140 ex.__context__ = context_ex
144 """If rethrow is true, rethrow ex (if any), unless the body throws.
146 If the body does throw, make any provided ex the __context__
147 of the newer exception (assuming there's no existing
148 __context__). Ensure the exceptions have __tracebacks__.
149 (Supports Python 2 compatibility.)
152 def __init__(self, ex, rethrow=True):
155 self.rethrow = rethrow
159 def __exit__(self, exc_type, exc_value, traceback):
164 add_ex_ctx(exc_value, self.ex)
166 if self.rethrow and self.ex:
171 def dump_traceback(ex):
173 next_ex = getattr(ex, '__context__', None)
175 stack.append(next_ex)
176 next_ex = getattr(next_ex, '__context__', None)
177 stack = reversed(stack)
179 tb = getattr(ex, '__traceback__', None)
180 print_exception(type(ex), ex, tb)
182 print('\nDuring handling of the above exception, another exception occurred:\n',
184 tb = getattr(ex, '__traceback__', None)
185 print_exception(type(ex), ex, tb)
194 def __exit__(self, value_type, value, traceback):
196 for ctx in reversed(self.contexts):
198 ctx.__exit__(value_type, value, traceback)
199 except BaseException as ex:
202 add_ex_ctx(ex, value)
203 value_type = type(ex)
205 traceback = ex.__traceback__
206 if value is not init_value:
209 def enter_context(self, x):
210 self.contexts.append(x)
216 """Return the original bytes passed to main() for an argv argument."""
219 bytes_from_uint = chr
221 def bytes_from_byte(b):
228 assert not hasattr(py_mmap.mmap, '__del__')
229 assert not hasattr(py_mmap.mmap, '__enter__')
230 assert not hasattr(py_mmap.mmap, '__exit__')
232 class mmap(py_mmap.mmap):
233 def __init__(self, *args, **kwargs):
234 self._bup_closed = True
235 # Silence deprecation warnings. mmap's current parent is
236 # object, which accepts no params and as of at least 2.7
238 if py_mmap.mmap.__init__ is not object.__init__:
239 super(mmap, self).__init__(self, *args, **kwargs)
240 self._bup_closed = False
242 self._bup_closed = True
243 super(mmap, self).close()
246 def __exit__(self, type, value, traceback):
247 with pending_raise(value, rethrow=False):
250 assert self._bup_closed
254 except ModuleNotFoundError:
259 "Return a new list containing the current process argv bytes."
260 return bup_main.argv()
263 "Return a new list containing the current process argv strings."
264 return [x.decode(errors='surrogateescape') for x in bup_main.argv()]
267 return bup_main.argv()
270 raise Exception('get_argvb requires the bup_main module');
272 raise Exception('get_argv requires the bup_main module');
275 """Run main() and raise a SystemExit with the return value if it
276 returns, pass along any SystemExit it raises, convert
277 KeyboardInterrupts into exit(130), and print a Python 3 style
278 contextual backtrace for other exceptions in both Python 2 and
282 except KeyboardInterrupt as ex:
284 except SystemExit as ex:
286 except BaseException as ex:
294 # Excepting wrap_main() in the traceback, these should produce similar output:
295 # python2 lib/bup/compat.py
296 # python3 lib/bup/compat.py
298 # diff -u <(python2 lib/bup/compat.py 2>&1) <(python3 lib/bup/compat.py 2>&1)
300 # Though the python3 output for 'second' will include a stacktrace
301 # starting from wrap_main, rather than from outer().
303 if __name__ == '__main__':
306 raise Exception('first')
311 except Exception as ex:
314 raise Exception('second')
315 except Exception as ex2:
316 raise add_ex_ctx(add_ex_tb(ex2), ex)