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
18 lc_ctype = environ.get(b'LC_CTYPE')
19 if lc_ctype and lc_ctype.lower() != b'iso-8859-1':
20 # Because of argv, options.py, pwd, grp, and any number of other issues
21 print('error: bup currently only works with ISO-8859-1, not LC_CTYPE=%s'
24 print('error: this should already have been arranged, so indicates a bug',
28 from os import fsencode
29 from shlex import quote
35 """Return hex string (not bytes as with hexlify) representation of b."""
39 """Do nothing (already handled by Python 3 infrastructure)."""
42 def add_ex_ctx(ex, context_ex):
43 """Do nothing (already handled by Python 3 infrastructure)."""
50 """Return the original bytes passed to main() for an argv argument."""
53 def bytes_from_uint(i):
56 byte_int = lambda x: x
58 def buffer(object, offset=None, size=None):
60 assert offset is not None
61 return memoryview(object)[offset:offset + size]
63 return memoryview(object)[offset:]
64 return memoryview(object)
66 def join_bytes(*items):
67 """Return the concatenated bytes or memoryview arguments as bytes."""
68 return b''.join(items)
75 from pipes import quote
76 from os import environ
79 int_types = (int, long)
84 """Add a traceback to ex if it doesn't already have one. Return ex.
87 if not getattr(ex, '__traceback__', None):
88 ex.__traceback__ = sys.exc_info()[2]
91 def add_ex_ctx(ex, context_ex):
92 """Make context_ex the __context__ of ex (unless it already has one).
97 if not getattr(ex, '__context__', None):
98 ex.__context__ = context_ex
101 def dump_traceback(ex):
103 next_ex = getattr(ex, '__context__', None)
105 stack.append(next_ex)
106 next_ex = getattr(next_ex, '__context__', None)
107 stack = reversed(stack)
109 tb = getattr(ex, '__traceback__', None)
110 print_exception(type(ex), ex, tb)
112 print('\nDuring handling of the above exception, another exception occurred:\n',
114 tb = getattr(ex, '__traceback__', None)
115 print_exception(type(ex), ex, tb)
121 """Return the original bytes passed to main() for an argv argument."""
124 def bytes_from_uint(i):
131 def join_bytes(x, y):
132 """Return the concatenated bytes or buffer arguments as bytes."""
133 if type(x) == buffer:
134 assert type(y) in (bytes, buffer)
136 assert type(x) == bytes
138 return b''.join((x, y))
139 assert type(y) in (bytes, buffer)
143 """Run main() and raise a SystemExit with the return value if it
144 returns, pass along any SystemExit it raises, convert
145 KeyboardInterrupts into exit(130), and print a Python 3 style
146 contextual backtrace for other exceptions in both Python 2 and
150 except KeyboardInterrupt as ex:
152 except SystemExit as ex:
154 except BaseException as ex:
162 # Excepting wrap_main() in the traceback, these should produce similar output:
163 # python2 lib/bup/compat.py
164 # python3 lib/bup/compat.py
166 # diff -u <(python2 lib/bup/compat.py 2>&1) <(python3 lib/bup/compat.py 2>&1)
168 # Though the python3 output for 'second' will include a stacktrace
169 # starting from wrap_main, rather than from outer().
171 if __name__ == '__main__':
174 raise Exception('first')
179 except Exception as ex:
182 raise Exception('second')
183 except Exception as ex2:
184 raise add_ex_ctx(add_ex_tb(ex2), ex)