]> arthur.barton.de Git - bup.git/blob - lib/bup/compat.py
prune-older: paper over python 3's integer type changes
[bup.git] / lib / bup / compat.py
1
2 from __future__ import absolute_import, print_function
3 from array import array
4 from traceback import print_exception
5 import 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     from os import environb as environ
16
17     lc_ctype = environ.get(b'LC_CTYPE')
18     if lc_ctype and lc_ctype.lower() != b'iso-8859-1':
19         # Because of argv, options.py, pwd, grp, and any number of other issues
20         print('error: bup currently only works with ISO-8859-1, not LC_CTYPE=%s'
21               % lc_ctype.decode(),
22               file=sys.stderr)
23         print('error: this should already have been arranged, so indicates a bug',
24               file=sys.stderr)
25         sys.exit(2)
26
27     from os import fsencode
28     from shlex import quote
29     range = range
30     str_type = str
31     int_types = (int,)
32
33     def add_ex_tb(ex):
34         """Do nothing (already handled by Python 3 infrastructure)."""
35         return ex
36
37     def add_ex_ctx(ex, context_ex):
38         """Do nothing (already handled by Python 3 infrastructure)."""
39         return ex
40
41     def items(x):
42         return x.items()
43
44     def argv_bytes(x):
45         """Return the original bytes passed to main() for an argv argument."""
46         return fsencode(x)
47
48     def bytes_from_uint(i):
49         return bytes((i,))
50
51     byte_int = lambda x: x
52
53     def buffer(object, offset=None, size=None):
54         if size:
55             assert offset is not None
56             return memoryview(object)[offset:offset + size]
57         if offset:
58             return memoryview(object)[offset:]
59         return memoryview(object)
60
61     def join_bytes(*items):
62         """Return the concatenated bytes or memoryview arguments as bytes."""
63         return b''.join(items)
64
65 else:  # Python 2
66
67     def fsencode(x):
68         return x
69
70     from pipes import quote
71     from os import environ
72     range = xrange
73     str_type = basestring
74     int_types = (int, long)
75
76     def add_ex_tb(ex):
77         """Add a traceback to ex if it doesn't already have one.  Return ex.
78
79         """
80         if not getattr(ex, '__traceback__', None):
81             ex.__traceback__ = sys.exc_info()[2]
82         return ex
83
84     def add_ex_ctx(ex, context_ex):
85         """Make context_ex the __context__ of ex (unless it already has one).
86         Return ex.
87
88         """
89         if context_ex:
90             if not getattr(ex, '__context__', None):
91                 ex.__context__ = context_ex
92         return ex
93
94     def dump_traceback(ex):
95         stack = [ex]
96         next_ex = getattr(ex, '__context__', None)
97         while next_ex:
98             stack.append(next_ex)
99             next_ex = getattr(next_ex, '__context__', None)
100         stack = reversed(stack)
101         ex = next(stack)
102         tb = getattr(ex, '__traceback__', None)
103         print_exception(type(ex), ex, tb)
104         for ex in stack:
105             print('\nDuring handling of the above exception, another exception occurred:\n',
106                   file=sys.stderr)
107             tb = getattr(ex, '__traceback__', None)
108             print_exception(type(ex), ex, tb)
109
110     def items(x):
111         return x.iteritems()
112
113     def argv_bytes(x):
114         """Return the original bytes passed to main() for an argv argument."""
115         return x
116
117     def bytes_from_uint(i):
118         return chr(i)
119
120     byte_int = ord
121
122     buffer = buffer
123
124     def join_bytes(x, y):
125         """Return the concatenated bytes or buffer arguments as bytes."""
126         if type(x) == buffer:
127             assert type(y) in (bytes, buffer)
128             return x + y
129         assert type(x) == bytes
130         if type(y) == bytes:
131             return b''.join((x, y))
132         assert type(y) in (bytes, buffer)
133         return buffer(x) + y
134
135 def wrap_main(main):
136     """Run main() and raise a SystemExit with the return value if it
137     returns, pass along any SystemExit it raises, convert
138     KeyboardInterrupts into exit(130), and print a Python 3 style
139     contextual backtrace for other exceptions in both Python 2 and
140     3)."""
141     try:
142         sys.exit(main())
143     except KeyboardInterrupt as ex:
144         sys.exit(130)
145     except SystemExit as ex:
146         raise
147     except BaseException as ex:
148         if py3:
149             raise
150         add_ex_tb(ex)
151         dump_traceback(ex)
152         sys.exit(1)
153
154
155 # Excepting wrap_main() in the traceback, these should produce similar output:
156 #   python2 lib/bup/compat.py
157 #   python3 lib/bup/compat.py
158 # i.e.:
159 #   diff -u <(python2 lib/bup/compat.py 2>&1) <(python3 lib/bup/compat.py 2>&1)
160 #
161 # Though the python3 output for 'second' will include a stacktrace
162 # starting from wrap_main, rather than from outer().
163
164 if __name__ == '__main__':
165
166     def inner():
167         raise Exception('first')
168
169     def outer():
170         try:
171             inner()
172         except Exception as ex:
173             add_ex_tb(ex)
174             try:
175                 raise Exception('second')
176             except Exception as ex2:
177                 raise add_ex_ctx(add_ex_tb(ex2), ex)
178
179     wrap_main(outer)