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 raise ex.with_traceback(sys.exc_info()[2])
42 """Do nothing (already handled by Python 3 infrastructure)."""
45 def add_ex_ctx(ex, context_ex):
46 """Do nothing (already handled by Python 3 infrastructure)."""
53 """Return the original bytes passed to main() for an argv argument."""
56 def bytes_from_uint(i):
59 def bytes_from_byte(b): # python > 2: b[3] returns ord('x'), not b'x'
62 byte_int = lambda x: x
64 def buffer(object, offset=None, size=None):
66 assert offset is not None
67 return memoryview(object)[offset:offset + size]
69 return memoryview(object)[offset:]
70 return memoryview(object)
72 def join_bytes(*items):
73 """Return the concatenated bytes or memoryview arguments as bytes."""
74 return b''.join(items)
81 from pipes import quote
82 from os import environ
84 from bup.py2raise import reraise
88 int_types = (int, long)
93 """Add a traceback to ex if it doesn't already have one. Return ex.
96 if not getattr(ex, '__traceback__', None):
97 ex.__traceback__ = sys.exc_info()[2]
100 def add_ex_ctx(ex, context_ex):
101 """Make context_ex the __context__ of ex (unless it already has one).
106 if not getattr(ex, '__context__', None):
107 ex.__context__ = context_ex
110 def dump_traceback(ex):
112 next_ex = getattr(ex, '__context__', None)
114 stack.append(next_ex)
115 next_ex = getattr(next_ex, '__context__', None)
116 stack = reversed(stack)
118 tb = getattr(ex, '__traceback__', None)
119 print_exception(type(ex), ex, tb)
121 print('\nDuring handling of the above exception, another exception occurred:\n',
123 tb = getattr(ex, '__traceback__', None)
124 print_exception(type(ex), ex, tb)
130 """Return the original bytes passed to main() for an argv argument."""
133 def bytes_from_uint(i):
136 def bytes_from_byte(b):
143 def join_bytes(x, y):
144 """Return the concatenated bytes or buffer arguments as bytes."""
145 if type(x) == buffer:
146 assert type(y) in (bytes, buffer)
148 assert type(x) == bytes
150 return b''.join((x, y))
151 assert type(y) in (bytes, buffer)
155 def restore_lc_env():
156 # Once we're up and running with iso-8859-1, undo the bup-python
157 # LC_CTYPE hackery, so we don't affect unrelated subprocesses.
158 bup_lc_all = environ.get(b'BUP_LC_ALL')
160 del environ[b'LC_COLLATE']
161 del environ[b'LC_CTYPE']
162 del environ[b'LC_MONETARY']
163 del environ[b'LC_NUMERIC']
164 del environ[b'LC_TIME']
165 del environ[b'LC_MESSAGES']
166 del environ[b'LC_MESSAGES']
167 environ[b'LC_ALL'] = bup_lc_all
169 bup_lc_ctype = environ.get(b'BUP_LC_CTYPE')
171 environ[b'LC_CTYPE'] = bup_lc_ctype
174 """Run main() and raise a SystemExit with the return value if it
175 returns, pass along any SystemExit it raises, convert
176 KeyboardInterrupts into exit(130), and print a Python 3 style
177 contextual backtrace for other exceptions in both Python 2 and
181 except KeyboardInterrupt as ex:
183 except SystemExit as ex:
185 except BaseException as ex:
193 # Excepting wrap_main() in the traceback, these should produce similar output:
194 # python2 lib/bup/compat.py
195 # python3 lib/bup/compat.py
197 # diff -u <(python2 lib/bup/compat.py 2>&1) <(python3 lib/bup/compat.py 2>&1)
199 # Though the python3 output for 'second' will include a stacktrace
200 # starting from wrap_main, rather than from outer().
202 if __name__ == '__main__':
205 raise Exception('first')
210 except Exception as ex:
213 raise Exception('second')
214 except Exception as ex2:
215 raise add_ex_ctx(add_ex_tb(ex2), ex)