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 fsdecode, fsencode
29 from shlex import quote
36 """Return hex string (not bytes as with hexlify) representation of b."""
40 raise ex.with_traceback(sys.exc_info()[2])
43 """Do nothing (already handled by Python 3 infrastructure)."""
46 def add_ex_ctx(ex, context_ex):
47 """Do nothing (already handled by Python 3 infrastructure)."""
54 """Return the original bytes passed to main() for an argv argument."""
57 def bytes_from_uint(i):
60 def bytes_from_byte(b): # python > 2: b[3] returns ord('x'), not b'x'
63 byte_int = lambda x: x
65 def buffer(object, offset=None, size=None):
67 assert offset is not None
68 return memoryview(object)[offset:offset + size]
70 return memoryview(object)[offset:]
71 return memoryview(object)
74 return fsencode(os.getcwd())
84 from pipes import quote
85 from os import environ, getcwd
87 from bup.py2raise import reraise
92 int_types = (int, long)
97 """Add a traceback to ex if it doesn't already have one. Return ex.
100 if not getattr(ex, '__traceback__', None):
101 ex.__traceback__ = sys.exc_info()[2]
104 def add_ex_ctx(ex, context_ex):
105 """Make context_ex the __context__ of ex (unless it already has one).
110 if not getattr(ex, '__context__', None):
111 ex.__context__ = context_ex
114 def dump_traceback(ex):
116 next_ex = getattr(ex, '__context__', None)
118 stack.append(next_ex)
119 next_ex = getattr(next_ex, '__context__', None)
120 stack = reversed(stack)
122 tb = getattr(ex, '__traceback__', None)
123 print_exception(type(ex), ex, tb)
125 print('\nDuring handling of the above exception, another exception occurred:\n',
127 tb = getattr(ex, '__traceback__', None)
128 print_exception(type(ex), ex, tb)
134 """Return the original bytes passed to main() for an argv argument."""
137 bytes_from_uint = chr
139 def bytes_from_byte(b):
150 def _configure_argv():
154 if len(sys.argv) > 1:
155 if environ.get(b'BUP_ARGV_0'):
156 print('error: BUP_ARGV* set and sys.argv not empty', file=sys.stderr)
159 argvb = [argv_bytes(x) for x in argv]
163 arg = environ.get(b'BUP_ARGV_%d' % i)
164 while arg is not None:
167 arg = environ.get(b'BUP_ARGV_%d' % i)
170 del environ[b'BUP_ARGV_%d' % i]
175 argv = [x.decode(errors='surrogateescape') for x in args]
182 def restore_lc_env():
183 # Once we're up and running with iso-8859-1, undo the bup-python
184 # LC_CTYPE hackery, so we don't affect unrelated subprocesses.
185 bup_lc_all = environ.get(b'BUP_LC_ALL')
187 del environ[b'LC_COLLATE']
188 del environ[b'LC_CTYPE']
189 del environ[b'LC_MONETARY']
190 del environ[b'LC_NUMERIC']
191 del environ[b'LC_TIME']
192 del environ[b'LC_MESSAGES']
193 del environ[b'LC_MESSAGES']
194 environ[b'LC_ALL'] = bup_lc_all
196 bup_lc_ctype = environ.get(b'BUP_LC_CTYPE')
198 environ[b'LC_CTYPE'] = bup_lc_ctype
201 """Run main() and raise a SystemExit with the return value if it
202 returns, pass along any SystemExit it raises, convert
203 KeyboardInterrupts into exit(130), and print a Python 3 style
204 contextual backtrace for other exceptions in both Python 2 and
208 except KeyboardInterrupt as ex:
210 except SystemExit as ex:
212 except BaseException as ex:
220 # Excepting wrap_main() in the traceback, these should produce similar output:
221 # python2 lib/bup/compat.py
222 # python3 lib/bup/compat.py
224 # diff -u <(python2 lib/bup/compat.py 2>&1) <(python3 lib/bup/compat.py 2>&1)
226 # Though the python3 output for 'second' will include a stacktrace
227 # starting from wrap_main, rather than from outer().
229 if __name__ == '__main__':
232 raise Exception('first')
237 except Exception as ex:
240 raise Exception('second')
241 except Exception as ex2:
242 raise add_ex_ctx(add_ex_tb(ex2), ex)