]> arthur.barton.de Git - bup.git/blob - lib/bup/compat.py
f5f9dc0ffa3801632d74dbca79e3bfa67104a4a9
[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 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 add_ex_tb(ex):
39         """Do nothing (already handled by Python 3 infrastructure)."""
40         return ex
41
42     def add_ex_ctx(ex, context_ex):
43         """Do nothing (already handled by Python 3 infrastructure)."""
44         return ex
45
46     def items(x):
47         return x.items()
48
49     def argv_bytes(x):
50         """Return the original bytes passed to main() for an argv argument."""
51         return fsencode(x)
52
53     def bytes_from_uint(i):
54         return bytes((i,))
55
56     byte_int = lambda x: x
57
58     def buffer(object, offset=None, size=None):
59         if size:
60             assert offset is not None
61             return memoryview(object)[offset:offset + size]
62         if offset:
63             return memoryview(object)[offset:]
64         return memoryview(object)
65
66     def join_bytes(*items):
67         """Return the concatenated bytes or memoryview arguments as bytes."""
68         return b''.join(items)
69
70 else:  # Python 2
71
72     def fsencode(x):
73         return x
74
75     from pipes import quote
76     from os import environ
77     range = xrange
78     str_type = basestring
79     int_types = (int, long)
80
81     hexstr = hexlify
82
83     def add_ex_tb(ex):
84         """Add a traceback to ex if it doesn't already have one.  Return ex.
85
86         """
87         if not getattr(ex, '__traceback__', None):
88             ex.__traceback__ = sys.exc_info()[2]
89         return ex
90
91     def add_ex_ctx(ex, context_ex):
92         """Make context_ex the __context__ of ex (unless it already has one).
93         Return ex.
94
95         """
96         if context_ex:
97             if not getattr(ex, '__context__', None):
98                 ex.__context__ = context_ex
99         return ex
100
101     def dump_traceback(ex):
102         stack = [ex]
103         next_ex = getattr(ex, '__context__', None)
104         while next_ex:
105             stack.append(next_ex)
106             next_ex = getattr(next_ex, '__context__', None)
107         stack = reversed(stack)
108         ex = next(stack)
109         tb = getattr(ex, '__traceback__', None)
110         print_exception(type(ex), ex, tb)
111         for ex in stack:
112             print('\nDuring handling of the above exception, another exception occurred:\n',
113                   file=sys.stderr)
114             tb = getattr(ex, '__traceback__', None)
115             print_exception(type(ex), ex, tb)
116
117     def items(x):
118         return x.iteritems()
119
120     def argv_bytes(x):
121         """Return the original bytes passed to main() for an argv argument."""
122         return x
123
124     def bytes_from_uint(i):
125         return chr(i)
126
127     byte_int = ord
128
129     buffer = buffer
130
131     def join_bytes(x, y):
132         """Return the concatenated bytes or buffer arguments as bytes."""
133         if type(x) == buffer:
134             assert type(y) in (bytes, buffer)
135             return x + y
136         assert type(x) == bytes
137         if type(y) == bytes:
138             return b''.join((x, y))
139         assert type(y) in (bytes, buffer)
140         return buffer(x) + y
141
142
143 def restore_lc_env():
144     # Once we're up and running with iso-8859-1, undo the bup-python
145     # LC_CTYPE hackery, so we don't affect unrelated subprocesses.
146     bup_lc_all = environ.get(b'BUP_LC_ALL')
147     if bup_lc_all:
148         del environ[b'LC_COLLATE']
149         del environ[b'LC_CTYPE']
150         del environ[b'LC_MONETARY']
151         del environ[b'LC_NUMERIC']
152         del environ[b'LC_TIME']
153         del environ[b'LC_MESSAGES']
154         del environ[b'LC_MESSAGES']
155         environ[b'LC_ALL'] = bup_lc_all
156         return
157     bup_lc_ctype = environ.get(b'BUP_LC_CTYPE')
158     if bup_lc_ctype:
159         environ[b'LC_CTYPE'] = bup_lc_ctype
160
161 def wrap_main(main):
162     """Run main() and raise a SystemExit with the return value if it
163     returns, pass along any SystemExit it raises, convert
164     KeyboardInterrupts into exit(130), and print a Python 3 style
165     contextual backtrace for other exceptions in both Python 2 and
166     3)."""
167     try:
168         sys.exit(main())
169     except KeyboardInterrupt as ex:
170         sys.exit(130)
171     except SystemExit as ex:
172         raise
173     except BaseException as ex:
174         if py3:
175             raise
176         add_ex_tb(ex)
177         dump_traceback(ex)
178         sys.exit(1)
179
180
181 # Excepting wrap_main() in the traceback, these should produce similar output:
182 #   python2 lib/bup/compat.py
183 #   python3 lib/bup/compat.py
184 # i.e.:
185 #   diff -u <(python2 lib/bup/compat.py 2>&1) <(python3 lib/bup/compat.py 2>&1)
186 #
187 # Though the python3 output for 'second' will include a stacktrace
188 # starting from wrap_main, rather than from outer().
189
190 if __name__ == '__main__':
191
192     def inner():
193         raise Exception('first')
194
195     def outer():
196         try:
197             inner()
198         except Exception as ex:
199             add_ex_tb(ex)
200             try:
201                 raise Exception('second')
202             except Exception as ex2:
203                 raise add_ex_ctx(add_ex_tb(ex2), ex)
204
205     wrap_main(outer)