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