2 from __future__ import absolute_import, print_function
3 from array import array
4 from binascii import hexlify
5 from traceback import print_exception
8 # Please see CODINGSTYLE for important exception handling guidelines
9 # and the rationale behind add_ex_tb(), add_ex_ctx(), etc.
11 py_maj = sys.version_info[0]
16 from os import environb as environ
17 from os import fsdecode, fsencode
18 from shlex import quote
19 ModuleNotFoundError = ModuleNotFoundError
26 """Return hex string (not bytes as with hexlify) representation of b."""
30 raise ex.with_traceback(sys.exc_info()[2])
33 """Do nothing (already handled by Python 3 infrastructure)."""
36 def add_ex_ctx(ex, context_ex):
37 """Do nothing (already handled by Python 3 infrastructure)."""
44 """Return the original bytes passed to main() for an argv argument."""
47 def bytes_from_uint(i):
50 def bytes_from_byte(b): # python > 2: b[3] returns ord('x'), not b'x'
53 byte_int = lambda x: x
55 def buffer(object, offset=None, size=None):
57 assert offset is not None
58 return memoryview(object)[offset:offset + size]
60 return memoryview(object)[offset:]
61 return memoryview(object)
64 return fsencode(os.getcwd())
68 ModuleNotFoundError = ImportError
76 from pipes import quote
77 from os import environ, getcwd
79 from bup.py2raise import reraise
84 int_types = (int, long)
89 """Add a traceback to ex if it doesn't already have one. Return ex.
92 if not getattr(ex, '__traceback__', None):
93 ex.__traceback__ = sys.exc_info()[2]
96 def add_ex_ctx(ex, context_ex):
97 """Make context_ex the __context__ of ex (unless it already has one).
102 if not getattr(ex, '__context__', None):
103 ex.__context__ = context_ex
106 def dump_traceback(ex):
108 next_ex = getattr(ex, '__context__', None)
110 stack.append(next_ex)
111 next_ex = getattr(next_ex, '__context__', None)
112 stack = reversed(stack)
114 tb = getattr(ex, '__traceback__', None)
115 print_exception(type(ex), ex, tb)
117 print('\nDuring handling of the above exception, another exception occurred:\n',
119 tb = getattr(ex, '__traceback__', None)
120 print_exception(type(ex), ex, tb)
126 """Return the original bytes passed to main() for an argv argument."""
129 bytes_from_uint = chr
131 def bytes_from_byte(b):
142 def _configure_argv():
146 if len(sys.argv) > 1:
147 if environ.get(b'BUP_ARGV_0'):
148 print('error: BUP_ARGV* set and sys.argv not empty', file=sys.stderr)
151 argvb = [argv_bytes(x) for x in argv]
155 arg = environ.get(b'BUP_ARGV_%d' % i)
156 while arg is not None:
159 arg = environ.get(b'BUP_ARGV_%d' % i)
162 del environ[b'BUP_ARGV_%d' % i]
167 argv = [x.decode(errors='surrogateescape') for x in args]
175 """Run main() and raise a SystemExit with the return value if it
176 returns, pass along any SystemExit it raises, convert
177 KeyboardInterrupts into exit(130), and print a Python 3 style
178 contextual backtrace for other exceptions in both Python 2 and
182 except KeyboardInterrupt as ex:
184 except SystemExit as ex:
186 except BaseException as ex:
194 # Excepting wrap_main() in the traceback, these should produce similar output:
195 # python2 lib/bup/compat.py
196 # python3 lib/bup/compat.py
198 # diff -u <(python2 lib/bup/compat.py 2>&1) <(python3 lib/bup/compat.py 2>&1)
200 # Though the python3 output for 'second' will include a stacktrace
201 # starting from wrap_main, rather than from outer().
203 if __name__ == '__main__':
206 raise Exception('first')
211 except Exception as ex:
214 raise Exception('second')
215 except Exception as ex2:
216 raise add_ex_ctx(add_ex_tb(ex2), ex)