]> arthur.barton.de Git - bup.git/blob - lib/bup/compat.py
692bd9c1d48518b66b7750f1955a0ea05b5dbe9a
[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 fsencode
29     from shlex import quote
30     range = range
31     str_type = str
32     int_types = (int,)
33
34     def hexstr(b):
35         """Return hex string (not bytes as with hexlify) representation of b."""
36         return b.hex()
37
38     def reraise(ex):
39         raise ex.with_traceback(sys.exc_info()[2])
40
41     def add_ex_tb(ex):
42         """Do nothing (already handled by Python 3 infrastructure)."""
43         return ex
44
45     def add_ex_ctx(ex, context_ex):
46         """Do nothing (already handled by Python 3 infrastructure)."""
47         return ex
48
49     def items(x):
50         return x.items()
51
52     def argv_bytes(x):
53         """Return the original bytes passed to main() for an argv argument."""
54         return fsencode(x)
55
56     def bytes_from_uint(i):
57         return bytes((i,))
58
59     def bytes_from_byte(b):  # python > 2: b[3] returns ord('x'), not b'x'
60         return bytes((b,))
61
62     byte_int = lambda x: x
63
64     def buffer(object, offset=None, size=None):
65         if size:
66             assert offset is not None
67             return memoryview(object)[offset:offset + size]
68         if offset:
69             return memoryview(object)[offset:]
70         return memoryview(object)
71
72     def join_bytes(*items):
73         """Return the concatenated bytes or memoryview arguments as bytes."""
74         return b''.join(items)
75
76 else:  # Python 2
77
78     def fsencode(x):
79         return x
80
81     from pipes import quote
82     from os import environ
83
84     from bup.py2raise import reraise
85
86     range = xrange
87     str_type = basestring
88     int_types = (int, long)
89
90     hexstr = hexlify
91
92     def add_ex_tb(ex):
93         """Add a traceback to ex if it doesn't already have one.  Return ex.
94
95         """
96         if not getattr(ex, '__traceback__', None):
97             ex.__traceback__ = sys.exc_info()[2]
98         return ex
99
100     def add_ex_ctx(ex, context_ex):
101         """Make context_ex the __context__ of ex (unless it already has one).
102         Return ex.
103
104         """
105         if context_ex:
106             if not getattr(ex, '__context__', None):
107                 ex.__context__ = context_ex
108         return ex
109
110     def dump_traceback(ex):
111         stack = [ex]
112         next_ex = getattr(ex, '__context__', None)
113         while next_ex:
114             stack.append(next_ex)
115             next_ex = getattr(next_ex, '__context__', None)
116         stack = reversed(stack)
117         ex = next(stack)
118         tb = getattr(ex, '__traceback__', None)
119         print_exception(type(ex), ex, tb)
120         for ex in stack:
121             print('\nDuring handling of the above exception, another exception occurred:\n',
122                   file=sys.stderr)
123             tb = getattr(ex, '__traceback__', None)
124             print_exception(type(ex), ex, tb)
125
126     def items(x):
127         return x.iteritems()
128
129     def argv_bytes(x):
130         """Return the original bytes passed to main() for an argv argument."""
131         return x
132
133     def bytes_from_uint(i):
134         return chr(i)
135
136     def bytes_from_byte(b):
137         return b
138
139     byte_int = ord
140
141     buffer = buffer
142
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)
147             return x + y
148         assert type(x) == bytes
149         if type(y) == bytes:
150             return b''.join((x, y))
151         assert type(y) in (bytes, buffer)
152         return buffer(x) + y
153
154
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')
159     if 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
168         return
169     bup_lc_ctype = environ.get(b'BUP_LC_CTYPE')
170     if bup_lc_ctype:
171         environ[b'LC_CTYPE'] = bup_lc_ctype
172
173 def wrap_main(main):
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
178     3)."""
179     try:
180         sys.exit(main())
181     except KeyboardInterrupt as ex:
182         sys.exit(130)
183     except SystemExit as ex:
184         raise
185     except BaseException as ex:
186         if py3:
187             raise
188         add_ex_tb(ex)
189         dump_traceback(ex)
190         sys.exit(1)
191
192
193 # Excepting wrap_main() in the traceback, these should produce similar output:
194 #   python2 lib/bup/compat.py
195 #   python3 lib/bup/compat.py
196 # i.e.:
197 #   diff -u <(python2 lib/bup/compat.py 2>&1) <(python3 lib/bup/compat.py 2>&1)
198 #
199 # Though the python3 output for 'second' will include a stacktrace
200 # starting from wrap_main, rather than from outer().
201
202 if __name__ == '__main__':
203
204     def inner():
205         raise Exception('first')
206
207     def outer():
208         try:
209             inner()
210         except Exception as ex:
211             add_ex_tb(ex)
212             try:
213                 raise Exception('second')
214             except Exception as ex2:
215                 raise add_ex_ctx(add_ex_tb(ex2), ex)
216
217     wrap_main(outer)