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 def restore_lc_env():
144 # Once we're up and running with iso-8859-1, undo the bup-python
145 # LC_CTYPE hackery, so we don't affect unrelated subprocesses.
146 bup_lc_all = environ.get(b'BUP_LC_ALL')
148 del environ[b'LC_COLLATE']
149 del environ[b'LC_CTYPE']
150 del environ[b'LC_MONETARY']
151 del environ[b'LC_NUMERIC']
152 del environ[b'LC_TIME']
153 del environ[b'LC_MESSAGES']
154 del environ[b'LC_MESSAGES']
155 environ[b'LC_ALL'] = bup_lc_all
157 bup_lc_ctype = environ.get(b'BUP_LC_CTYPE')
159 environ[b'LC_CTYPE'] = bup_lc_ctype
162 """Run main() and raise a SystemExit with the return value if it
163 returns, pass along any SystemExit it raises, convert
164 KeyboardInterrupts into exit(130), and print a Python 3 style
165 contextual backtrace for other exceptions in both Python 2 and
169 except KeyboardInterrupt as ex:
171 except SystemExit as ex:
173 except BaseException as ex:
181 # Excepting wrap_main() in the traceback, these should produce similar output:
182 # python2 lib/bup/compat.py
183 # python3 lib/bup/compat.py
185 # diff -u <(python2 lib/bup/compat.py 2>&1) <(python3 lib/bup/compat.py 2>&1)
187 # Though the python3 output for 'second' will include a stacktrace
188 # starting from wrap_main, rather than from outer().
190 if __name__ == '__main__':
193 raise Exception('first')
198 except Exception as ex:
201 raise Exception('second')
202 except Exception as ex2:
203 raise add_ex_ctx(add_ex_tb(ex2), ex)