]> arthur.barton.de Git - bup.git/blob - lib/bup/compat.py
Bypass Python 3 glibc argv problems by routing args through env
[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
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'
22               % lc_ctype.decode(),
23               file=sys.stderr)
24         print('error: this should already have been arranged, so indicates a bug',
25               file=sys.stderr)
26         sys.exit(2)
27
28     from os import fsdecode, fsencode
29     from shlex import quote
30     input = input
31     range = range
32     str_type = str
33     int_types = (int,)
34
35     def hexstr(b):
36         """Return hex string (not bytes as with hexlify) representation of b."""
37         return b.hex()
38
39     def reraise(ex):
40         raise ex.with_traceback(sys.exc_info()[2])
41
42     def add_ex_tb(ex):
43         """Do nothing (already handled by Python 3 infrastructure)."""
44         return ex
45
46     def add_ex_ctx(ex, context_ex):
47         """Do nothing (already handled by Python 3 infrastructure)."""
48         return ex
49
50     def items(x):
51         return x.items()
52
53     def argv_bytes(x):
54         """Return the original bytes passed to main() for an argv argument."""
55         return fsencode(x)
56
57     def bytes_from_uint(i):
58         return bytes((i,))
59
60     def bytes_from_byte(b):  # python > 2: b[3] returns ord('x'), not b'x'
61         return bytes((b,))
62
63     byte_int = lambda x: x
64
65     def buffer(object, offset=None, size=None):
66         if size:
67             assert offset is not None
68             return memoryview(object)[offset:offset + size]
69         if offset:
70             return memoryview(object)[offset:]
71         return memoryview(object)
72
73     def getcwd():
74         return fsencode(os.getcwd())
75
76 else:  # Python 2
77
78     def fsdecode(x):
79         return x
80
81     def fsencode(x):
82         return x
83
84     from pipes import quote
85     from os import environ, getcwd
86
87     from bup.py2raise import reraise
88
89     input = raw_input
90     range = xrange
91     str_type = basestring
92     int_types = (int, long)
93
94     hexstr = hexlify
95
96     def add_ex_tb(ex):
97         """Add a traceback to ex if it doesn't already have one.  Return ex.
98
99         """
100         if not getattr(ex, '__traceback__', None):
101             ex.__traceback__ = sys.exc_info()[2]
102         return ex
103
104     def add_ex_ctx(ex, context_ex):
105         """Make context_ex the __context__ of ex (unless it already has one).
106         Return ex.
107
108         """
109         if context_ex:
110             if not getattr(ex, '__context__', None):
111                 ex.__context__ = context_ex
112         return ex
113
114     def dump_traceback(ex):
115         stack = [ex]
116         next_ex = getattr(ex, '__context__', None)
117         while next_ex:
118             stack.append(next_ex)
119             next_ex = getattr(next_ex, '__context__', None)
120         stack = reversed(stack)
121         ex = next(stack)
122         tb = getattr(ex, '__traceback__', None)
123         print_exception(type(ex), ex, tb)
124         for ex in stack:
125             print('\nDuring handling of the above exception, another exception occurred:\n',
126                   file=sys.stderr)
127             tb = getattr(ex, '__traceback__', None)
128             print_exception(type(ex), ex, tb)
129
130     def items(x):
131         return x.iteritems()
132
133     def argv_bytes(x):
134         """Return the original bytes passed to main() for an argv argument."""
135         return x
136
137     bytes_from_uint = chr
138
139     def bytes_from_byte(b):
140         return b
141
142     byte_int = ord
143
144     buffer = buffer
145
146
147 argv = None
148 argvb = None
149
150 def _configure_argv():
151     global argv, argvb
152     assert not argv
153     assert not argvb
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)
157             sys.exit(2)
158         argv = sys.argv
159         argvb = [argv_bytes(x) for x in argv]
160         return
161     args = []
162     i = 0
163     arg = environ.get(b'BUP_ARGV_%d' % i)
164     while arg is not None:
165         args.append(arg)
166         i += 1
167         arg = environ.get(b'BUP_ARGV_%d' % i)
168     i -= 1
169     while i >= 0:
170         del environ[b'BUP_ARGV_%d' % i]
171         i -= 1
172     argvb = args
173     # System encoding?
174     if py3:
175         argv = [x.decode(errors='surrogateescape') for x in args]
176     else:
177         argv = argvb
178
179 _configure_argv()
180
181
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')
186     if 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
195         return
196     bup_lc_ctype = environ.get(b'BUP_LC_CTYPE')
197     if bup_lc_ctype:
198         environ[b'LC_CTYPE'] = bup_lc_ctype
199
200 def wrap_main(main):
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
205     3)."""
206     try:
207         sys.exit(main())
208     except KeyboardInterrupt as ex:
209         sys.exit(130)
210     except SystemExit as ex:
211         raise
212     except BaseException as ex:
213         if py3:
214             raise
215         add_ex_tb(ex)
216         dump_traceback(ex)
217         sys.exit(1)
218
219
220 # Excepting wrap_main() in the traceback, these should produce similar output:
221 #   python2 lib/bup/compat.py
222 #   python3 lib/bup/compat.py
223 # i.e.:
224 #   diff -u <(python2 lib/bup/compat.py 2>&1) <(python3 lib/bup/compat.py 2>&1)
225 #
226 # Though the python3 output for 'second' will include a stacktrace
227 # starting from wrap_main, rather than from outer().
228
229 if __name__ == '__main__':
230
231     def inner():
232         raise Exception('first')
233
234     def outer():
235         try:
236             inner()
237         except Exception as ex:
238             add_ex_tb(ex)
239             try:
240                 raise Exception('second')
241             except Exception as ex2:
242                 raise add_ex_ctx(add_ex_tb(ex2), ex)
243
244     wrap_main(outer)