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