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