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{host}: The host on which the exception occurred, or C{None} if local.
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{host}: The host on which the stderr output occurred, or C{None} if local.
31 """
32
33 import sys
34 import new
35
71
72
73
74
75
76
77
78
80
82 Exception.__init__(self)
83 self.cause = cause
84
86 return str(self.cause)
87
88
89
90
91
92
93
94
95
96 exception_handler = None
97 stderr_handler = None
98
108
110 buffer = []
111 if host:
112 buffer.append('From ')
113 buffer.append(host)
114 buffer.append(': ')
115 buffer.append(str(op))
116 _format_input_for_reporting(input, buffer)
117 buffer.append(' ')
118 buffer.append(str(exception.__class__))
119 buffer.append(': ')
120 buffer.append(str(exception))
121 print >>sys.stderr, ''.join(buffer)
122
124 """Use C{handler} as the exception handler.
125 """
126 global exception_handler
127 def wrap_provided_exception_handler(exception, op, input, host = None):
128 try:
129 handler(exception, op, input, host)
130 except Exception, e:
131 raise ExceptionHandlerException(e)
132 exception_handler = wrap_provided_exception_handler
133
134 exception_handler = _default_exception_handler
135
137 buffer = []
138 if host:
139 buffer.append('From ')
140 buffer.append(host)
141 buffer.append(': ')
142 buffer.append(str(op))
143 _format_input_for_reporting(input, buffer)
144 buffer.append(': ')
145 buffer.append(line.rstrip())
146 print >>sys.stderr, ''.join(buffer)
147
149 """Use C{handler} as the stderr handler.
150 """
151 def wrap_provided_stderr_handler(line, op, input, host = None):
152 try:
153 handler(line, op, input, host)
154 except Exception, e:
155 raise ExceptionHandlerException(e)
156 global stderr_handler
157 stderr_handler = wrap_provided_stderr_handler
158
159 stderr_handler = _default_stderr_handler
160