]> arthur.barton.de Git - bup.git/blob - lib/bup/compat.py
vint: fix bytes for Python 3
[bup.git] / lib / bup / compat.py
1
2 from __future__ import absolute_import, print_function
3 from traceback import print_exception
4 import sys
5
6 # Please see CODINGSTYLE for important exception handling guidelines
7 # and the rationale behind add_ex_tb(), add_ex_ctx(), etc.
8
9 py_maj = sys.version_info[0]
10 py3 = py_maj >= 3
11
12 if py3:
13
14     from shlex import quote
15     range = range
16     str_type = str
17
18     def add_ex_tb(ex):
19         """Do nothing (already handled by Python 3 infrastructure)."""
20         return ex
21
22     def add_ex_ctx(ex, context_ex):
23         """Do nothing (already handled by Python 3 infrastructure)."""
24         return ex
25
26     def items(x):
27         return x.items()
28
29     def bytes_from_uint(i):
30         return bytes((i,))
31
32 else:  # Python 2
33
34     from pipes import quote
35     range = xrange
36     str_type = basestring
37
38     def add_ex_tb(ex):
39         """Add a traceback to ex if it doesn't already have one.  Return ex.
40
41         """
42         if not getattr(ex, '__traceback__', None):
43             ex.__traceback__ = sys.exc_info()[2]
44         return ex
45
46     def add_ex_ctx(ex, context_ex):
47         """Make context_ex the __context__ of ex (unless it already has one).
48         Return ex.
49
50         """
51         if context_ex:
52             if not getattr(ex, '__context__', None):
53                 ex.__context__ = context_ex
54         return ex
55
56     def dump_traceback(ex):
57         stack = [ex]
58         next_ex = getattr(ex, '__context__', None)
59         while next_ex:
60             stack.append(next_ex)
61             next_ex = getattr(next_ex, '__context__', None)
62         stack = reversed(stack)
63         ex = next(stack)
64         tb = getattr(ex, '__traceback__', None)
65         print_exception(type(ex), ex, tb)
66         for ex in stack:
67             print('\nDuring handling of the above exception, another exception occurred:\n',
68                   file=sys.stderr)
69             tb = getattr(ex, '__traceback__', None)
70             print_exception(type(ex), ex, tb)
71
72     def items(x):
73         return x.iteritems()
74
75     def bytes_from_uint(i):
76         return chr(i)
77
78
79 def wrap_main(main):
80     """Run main() and raise a SystemExit with the return value if it
81     returns, pass along any SystemExit it raises, convert
82     KeyboardInterrupts into exit(130), and print a Python 3 style
83     contextual backtrace for other exceptions in both Python 2 and
84     3)."""
85     try:
86         sys.exit(main())
87     except KeyboardInterrupt as ex:
88         sys.exit(130)
89     except SystemExit as ex:
90         raise
91     except BaseException as ex:
92         if py3:
93             raise
94         add_ex_tb(ex)
95         dump_traceback(ex)
96         sys.exit(1)
97
98
99 # Excepting wrap_main() in the traceback, these should produce similar output:
100 #   python2 lib/bup/compat.py
101 #   python3 lib/bup/compat.py
102 # i.e.:
103 #   diff -u <(python2 lib/bup/compat.py 2>&1) <(python3 lib/bup/compat.py 2>&1)
104 #
105 # Though the python3 output for 'second' will include a stacktrace
106 # starting from wrap_main, rather than from outer().
107
108 if __name__ == '__main__':
109
110     def inner():
111         raise Exception('first')
112
113     def outer():
114         try:
115             inner()
116         except Exception as ex:
117             add_ex_tb(ex)
118             try:
119                 raise Exception('second')
120             except Exception as ex2:
121                 raise add_ex_ctx(add_ex_tb(ex2), ex)
122
123     wrap_main(outer)