1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 """Controls handling of exceptions and stderr through the setting of handlers.
19
20 An C{exception_handler} is a function with these arguments:
21 - C{exception}: The exception being handled. In case of a remote exception, this exception object is a client-side reconstruction of the server-side exception.
22 - C{op}: A command of type C{Op}, or, in case of a remote exception, a command description, obtained by applying C{str()}.
23 - C{input}: Input to the command that raised the exception.
24 - C{thread}: The thread on which the exception occurred.
25
26 An C{error_handler} is a function with these arguments:
27 - C{line}: A line written to stderr.
28 - C{op}: A command of type C{Op}, or, in case of remote stderr output, a command description, obtained by applying C{str()}.
29 - C{input}: Input to the command that generated the stderr output.
30 - C{thread}: The thread on which the stderr output occurred,
31 """
32
33 import sys
34 import new
35
36 import util
37
73
74
75
76
77
78
79
80
82
84 Exception.__init__(self)
85 self.cause = cause
86
88 return str(self.cause)
89
90
91
92
93
94
95
96
97
98 exception_handler = None
99 stderr_handler = None
100
110
112 buffer = []
113 if thread:
114 buffer.append('on ')
115 buffer.append(str(thread))
116 buffer.append(': ')
117 buffer.append(str(op))
118 _format_input_for_reporting(input, buffer)
119 buffer.append(' ')
120 buffer.append(str(exception.__class__))
121 buffer.append(': ')
122 buffer.append(str(exception))
123 print >>sys.stderr, ''.join(buffer)
124
126 """Use C{handler} as the exception handler.
127 """
128 global exception_handler
129 def wrap_provided_exception_handler(exception, op, input, thread = None):
130 try:
131 handler(exception, op, input, thread)
132 except Exception, e:
133 raise OshKiller(e)
134 exception_handler = wrap_provided_exception_handler
135
136 exception_handler = _default_exception_handler
137
139 buffer = []
140 if thread:
141 buffer.append('on ')
142 buffer.append(str(thread))
143 buffer.append(': ')
144 buffer.append(str(op))
145 _format_input_for_reporting(input, buffer)
146 buffer.append(': ')
147 buffer.append(line.rstrip())
148 print >>sys.stderr, ''.join(buffer)
149
151 """Use C{handler} as the stderr handler.
152 """
153 def wrap_provided_stderr_handler(line, op, input, thread = None):
154 try:
155 handler(line, op, input, thread)
156 except Exception, e:
157 raise OshKiller(e)
158 global stderr_handler
159 stderr_handler = wrap_provided_stderr_handler
160
161 stderr_handler = _default_stderr_handler
162