]> arthur.barton.de Git - bup.git/blob - lib/bup/compat.py
fb45ead5d6e100fdcc0d37910d42e69f6e72793c
[bup.git] / lib / bup / compat.py
1
2 from __future__ import absolute_import, print_function
3 from binascii import hexlify
4 from traceback import print_exception
5 import os, sys
6
7 # Please see CODINGSTYLE for important exception handling guidelines
8 # and the rationale behind add_ex_tb(), add_ex_ctx(), etc.
9
10 py_maj = sys.version_info[0]
11 py3 = py_maj >= 3
12
13 if py3:
14
15     # pylint: disable=unused-import
16     from os import environb as environ
17     from os import fsdecode, fsencode
18     from shlex import quote
19     ModuleNotFoundError = ModuleNotFoundError
20     input = input
21     range = range
22     str_type = str
23     int_types = (int,)
24
25     def hexstr(b):
26         """Return hex string (not bytes as with hexlify) representation of b."""
27         return b.hex()
28
29     def reraise(ex):
30         raise ex.with_traceback(sys.exc_info()[2])
31
32     def add_ex_tb(ex):
33         """Do nothing (already handled by Python 3 infrastructure)."""
34         return ex
35
36     def add_ex_ctx(ex, context_ex):
37         """Do nothing (already handled by Python 3 infrastructure)."""
38         return ex
39
40     class pending_raise:
41         """Rethrow either the provided ex, or any exception raised by the with
42         statement body.  (Supports Python 2 compatibility.)
43
44         """
45         def __init__(self, ex):
46             self.ex = ex
47         def __enter__(self):
48             return None
49         def __exit__(self, exc_type, exc_value, traceback):
50             if not exc_type:
51                 raise self.ex
52             return None
53
54     def items(x):
55         return x.items()
56
57     def argv_bytes(x):
58         """Return the original bytes passed to main() for an argv argument."""
59         return fsencode(x)
60
61     def bytes_from_uint(i):
62         return bytes((i,))
63
64     def bytes_from_byte(b):  # python > 2: b[3] returns ord('x'), not b'x'
65         return bytes((b,))
66
67     byte_int = lambda x: x
68
69     def buffer(object, offset=None, size=None):
70         if size:
71             assert offset is not None
72             return memoryview(object)[offset:offset + size]
73         if offset:
74             return memoryview(object)[offset:]
75         return memoryview(object)
76
77     def getcwd():
78         return fsencode(os.getcwd())
79
80 else:  # Python 2
81
82     ModuleNotFoundError = ImportError
83
84     def fsdecode(x):
85         return x
86
87     def fsencode(x):
88         return x
89
90     from pipes import quote
91     # pylint: disable=unused-import
92     from os import environ, getcwd
93
94     # pylint: disable=unused-import
95     from bup.py2raise import reraise
96
97     input = raw_input
98     range = xrange
99     str_type = basestring
100     int_types = (int, long)
101
102     hexstr = hexlify
103
104     def add_ex_tb(ex):
105         """Add a traceback to ex if it doesn't already have one.  Return ex.
106
107         """
108         if not getattr(ex, '__traceback__', None):
109             ex.__traceback__ = sys.exc_info()[2]
110         return ex
111
112     def add_ex_ctx(ex, context_ex):
113         """Make context_ex the __context__ of ex (unless it already has one).
114         Return ex.
115
116         """
117         if context_ex:
118             if not getattr(ex, '__context__', None):
119                 ex.__context__ = context_ex
120         return ex
121
122     class pending_raise:
123         """Rethrow either the provided ex, or any exception raised by the with
124         statement body, after making ex the __context__ of the newer
125         exception (assuming there's no existing __context__).  Ensure
126         the exceptions have __tracebacks__.  (Supports Python 2
127         compatibility.)
128
129         """
130         def __init__(self, ex):
131             self.ex = ex
132         def __enter__(self):
133             add_ex_tb(self.ex)
134             return None
135         def __exit__(self, exc_type, exc_value, traceback):
136             if not exc_type:
137                 raise self.ex
138             add_ex_tb(exc_value)
139             add_ex_ctx(exc_value, self.ex)
140             return None
141
142     def dump_traceback(ex):
143         stack = [ex]
144         next_ex = getattr(ex, '__context__', None)
145         while next_ex:
146             stack.append(next_ex)
147             next_ex = getattr(next_ex, '__context__', None)
148         stack = reversed(stack)
149         ex = next(stack)
150         tb = getattr(ex, '__traceback__', None)
151         print_exception(type(ex), ex, tb)
152         for ex in stack:
153             print('\nDuring handling of the above exception, another exception occurred:\n',
154                   file=sys.stderr)
155             tb = getattr(ex, '__traceback__', None)
156             print_exception(type(ex), ex, tb)
157
158     def items(x):
159         return x.iteritems()
160
161     def argv_bytes(x):
162         """Return the original bytes passed to main() for an argv argument."""
163         return x
164
165     bytes_from_uint = chr
166
167     def bytes_from_byte(b):
168         return b
169
170     byte_int = ord
171
172     buffer = buffer
173
174 try:
175     import bup_main
176 except ModuleNotFoundError:
177     bup_main = None
178
179 if bup_main:
180     def get_argvb():
181         "Return a new list containing the current process argv bytes."
182         return bup_main.argv()
183     if py3:
184         def get_argv():
185             "Return a new list containing the current process argv strings."
186             return [x.decode(errors='surrogateescape') for x in bup_main.argv()]
187     else:
188         def get_argv():
189             return bup_main.argv()
190 else:
191     def get_argvb():
192         raise Exception('get_argvb requires the bup_main module');
193     def get_argv():
194         raise Exception('get_argv requires the bup_main module');
195
196 def wrap_main(main):
197     """Run main() and raise a SystemExit with the return value if it
198     returns, pass along any SystemExit it raises, convert
199     KeyboardInterrupts into exit(130), and print a Python 3 style
200     contextual backtrace for other exceptions in both Python 2 and
201     3)."""
202     try:
203         sys.exit(main())
204     except KeyboardInterrupt as ex:
205         sys.exit(130)
206     except SystemExit as ex:
207         raise
208     except BaseException as ex:
209         if py3:
210             raise
211         add_ex_tb(ex)
212         dump_traceback(ex)
213         sys.exit(1)
214
215
216 # Excepting wrap_main() in the traceback, these should produce similar output:
217 #   python2 lib/bup/compat.py
218 #   python3 lib/bup/compat.py
219 # i.e.:
220 #   diff -u <(python2 lib/bup/compat.py 2>&1) <(python3 lib/bup/compat.py 2>&1)
221 #
222 # Though the python3 output for 'second' will include a stacktrace
223 # starting from wrap_main, rather than from outer().
224
225 if __name__ == '__main__':
226
227     def inner():
228         raise Exception('first')
229
230     def outer():
231         try:
232             inner()
233         except Exception as ex:
234             add_ex_tb(ex)
235             try:
236                 raise Exception('second')
237             except Exception as ex2:
238                 raise add_ex_ctx(add_ex_tb(ex2), ex)
239
240     wrap_main(outer)