4 py_maj = sys.version_info.major
7 # pylint: disable=unused-import
8 from contextlib import ExitStack, nullcontext
9 from os import environb as environ
10 from os import fsdecode, fsencode
11 from shlex import quote
14 """Return hex string (not bytes as with hexlify) representation of b."""
18 raise ex.with_traceback(sys.exc_info()[2])
20 # These three functions (add_ex_tb, add_ex_ctx, and pending_raise) are
21 # vestigial, and code that uses them can probably be rewritten more
22 # simply now that we require Python versions that automatically
23 # populate the tracebacks and automatically chain pending exceptions.
26 """Do nothing (already handled by Python 3 infrastructure)."""
29 def add_ex_ctx(ex, context_ex):
30 """Do nothing (already handled by Python 3 infrastructure)."""
34 """If rethrow is true, rethrow ex (if any), unless the body throws.
36 (Supports Python 2 compatibility.)
39 # This is completely vestigial, and should be removed
40 def __init__(self, ex, rethrow=True):
43 self.rethrow = rethrow
46 def __exit__(self, exc_type, exc_value, traceback):
48 if not exc_type and self.ex and self.rethrow:
57 """Return the original bytes passed to main() for an argv argument."""
60 def bytes_from_uint(i):
63 def bytes_from_byte(b): # python > 2: b[3] returns ord('x'), not b'x'
66 byte_int = lambda x: x
68 def buffer(object, offset=None, size=None):
70 assert offset is not None
71 return memoryview(object)[offset:offset + size]
73 return memoryview(object)[offset:]
74 return memoryview(object)
77 return fsencode(os.getcwd())
82 except ModuleNotFoundError:
87 "Return a new list containing the current process argv bytes."
88 return bup_main.argv()
90 "Return a new list containing the current process argv strings."
91 return [x.decode(errors='surrogateescape') for x in bup_main.argv()]
94 raise Exception('get_argvb requires the bup_main module');
96 raise Exception('get_argv requires the bup_main module');
99 """Run main() and raise a SystemExit with the return value if it
100 returns, pass along any SystemExit it raises, convert
101 KeyboardInterrupts into exit(130), and print a Python 3 style
102 contextual backtrace for other exceptions in both Python 2 and
106 except KeyboardInterrupt as ex: