2 # pylint: disable=unused-import
3 from contextlib import ExitStack, nullcontext
4 from os import environb as environ
5 from os import fsdecode, fsencode
6 from shlex import quote
10 """Return hex string (not bytes as with hexlify) representation of b."""
14 raise ex.with_traceback(sys.exc_info()[2])
16 # These three functions (add_ex_tb, add_ex_ctx, and pending_raise) are
17 # vestigial, and code that uses them can probably be rewritten more
18 # simply now that we require Python versions that automatically
19 # populate the tracebacks and automatically chain pending exceptions.
22 """Do nothing (already handled by Python 3 infrastructure)."""
25 def add_ex_ctx(ex, context_ex):
26 """Do nothing (already handled by Python 3 infrastructure)."""
30 """If rethrow is true, rethrow ex (if any), unless the body throws.
32 (Supports Python 2 compatibility.)
35 # This is completely vestigial, and should be removed
36 def __init__(self, ex, rethrow=True):
39 self.rethrow = rethrow
42 def __exit__(self, exc_type, exc_value, traceback):
44 if not exc_type and self.ex and self.rethrow:
50 """Return the original bytes passed to main() for an argv argument."""
53 def bytes_from_uint(i):
56 def bytes_from_byte(b): # python > 2: b[3] returns ord('x'), not b'x'
59 byte_int = lambda x: x
61 def buffer(object, offset=None, size=None):
63 assert offset is not None
64 return memoryview(object)[offset:offset + size]
66 return memoryview(object)[offset:]
67 return memoryview(object)
70 return fsencode(os.getcwd())
75 except ModuleNotFoundError:
80 "Return a new list containing the current process argv bytes."
81 return bup_main.argv()
83 "Return a new list containing the current process argv strings."
84 return [x.decode(errors='surrogateescape') for x in bup_main.argv()]
87 raise Exception('get_argvb requires the bup_main module');
89 raise Exception('get_argv requires the bup_main module');
92 """Run main() and raise a SystemExit with the return value if it
93 returns, pass along any SystemExit it raises, convert
94 KeyboardInterrupts into exit(130), and print a Python 3 style
95 contextual backtrace for other exceptions in both Python 2 and
99 except KeyboardInterrupt as ex: