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