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