2 from __future__ import absolute_import, print_function
3 from array import array
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 from os import environb as environ
17 lc_ctype = environ.get(b'LC_CTYPE')
18 if lc_ctype and lc_ctype.lower() != b'iso-8859-1':
19 # Because of argv, options.py, pwd, grp, and any number of other issues
20 print('error: bup currently only works with ISO-8859-1, not LC_CTYPE=%s'
23 print('error: this should already have been arranged, so indicates a bug',
27 from os import fsencode
28 from shlex import quote
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 byte_int = lambda x: x
52 def buffer(object, offset=None, size=None):
54 assert offset is not None
55 return memoryview(object)[offset:offset + size]
57 return memoryview(object)[offset:]
58 return memoryview(object)
60 def join_bytes(*items):
61 """Return the concatenated bytes or memoryview arguments as bytes."""
62 return b''.join(items)
66 from pipes import quote
67 from os import environ
72 """Add a traceback to ex if it doesn't already have one. Return ex.
75 if not getattr(ex, '__traceback__', None):
76 ex.__traceback__ = sys.exc_info()[2]
79 def add_ex_ctx(ex, context_ex):
80 """Make context_ex the __context__ of ex (unless it already has one).
85 if not getattr(ex, '__context__', None):
86 ex.__context__ = context_ex
89 def dump_traceback(ex):
91 next_ex = getattr(ex, '__context__', None)
94 next_ex = getattr(next_ex, '__context__', None)
95 stack = reversed(stack)
97 tb = getattr(ex, '__traceback__', None)
98 print_exception(type(ex), ex, tb)
100 print('\nDuring handling of the above exception, another exception occurred:\n',
102 tb = getattr(ex, '__traceback__', None)
103 print_exception(type(ex), ex, tb)
109 """Return the original bytes passed to main() for an argv argument."""
112 def bytes_from_uint(i):
119 def join_bytes(x, y):
120 """Return the concatenated bytes or buffer arguments as bytes."""
121 if type(x) == buffer:
122 assert type(y) in (bytes, buffer)
124 assert type(x) == bytes
126 return b''.join((x, y))
127 assert type(y) in (bytes, buffer)
131 """Run main() and raise a SystemExit with the return value if it
132 returns, pass along any SystemExit it raises, convert
133 KeyboardInterrupts into exit(130), and print a Python 3 style
134 contextual backtrace for other exceptions in both Python 2 and
138 except KeyboardInterrupt as ex:
140 except SystemExit as ex:
142 except BaseException as ex:
150 # Excepting wrap_main() in the traceback, these should produce similar output:
151 # python2 lib/bup/compat.py
152 # python3 lib/bup/compat.py
154 # diff -u <(python2 lib/bup/compat.py 2>&1) <(python3 lib/bup/compat.py 2>&1)
156 # Though the python3 output for 'second' will include a stacktrace
157 # starting from wrap_main, rather than from outer().
159 if __name__ == '__main__':
162 raise Exception('first')
167 except Exception as ex:
170 raise Exception('second')
171 except Exception as ex2:
172 raise add_ex_ctx(add_ex_tb(ex2), ex)