]> arthur.barton.de Git - bup.git/blob - lib/bup/compat.py
compat.hexstr: add and use
[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 def wrap_main(main):
143     """Run main() and raise a SystemExit with the return value if it
144     returns, pass along any SystemExit it raises, convert
145     KeyboardInterrupts into exit(130), and print a Python 3 style
146     contextual backtrace for other exceptions in both Python 2 and
147     3)."""
148     try:
149         sys.exit(main())
150     except KeyboardInterrupt as ex:
151         sys.exit(130)
152     except SystemExit as ex:
153         raise
154     except BaseException as ex:
155         if py3:
156             raise
157         add_ex_tb(ex)
158         dump_traceback(ex)
159         sys.exit(1)
160
161
162 # Excepting wrap_main() in the traceback, these should produce similar output:
163 #   python2 lib/bup/compat.py
164 #   python3 lib/bup/compat.py
165 # i.e.:
166 #   diff -u <(python2 lib/bup/compat.py 2>&1) <(python3 lib/bup/compat.py 2>&1)
167 #
168 # Though the python3 output for 'second' will include a stacktrace
169 # starting from wrap_main, rather than from outer().
170
171 if __name__ == '__main__':
172
173     def inner():
174         raise Exception('first')
175
176     def outer():
177         try:
178             inner()
179         except Exception as ex:
180             add_ex_tb(ex)
181             try:
182                 raise Exception('second')
183             except Exception as ex2:
184                 raise add_ex_ctx(add_ex_tb(ex2), ex)
185
186     wrap_main(outer)