| Home | Trees | Indices | Help |
|
|---|
|
|
1 # osh
2 # Copyright (C) 2005 Jack Orenstein <jao@geophile.com>
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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
37
38 _command_description = None
39 _input = None
40 _exception_args = None
41 _exception_type_name = None
42 _exception_message = None
43
45 self._command_description = command_description
46 self._input = input
47 self._exception_args = exception.args
48 self._exception_type_name = str(exception.__class__)
49 self._exception_message = str(exception)
50
52 return ('Encountered %s during execution of %s on input %s: %s' %
53 (self._exception_type_name,
54 self._command_description,
55 self._input,
56 self._exception_message))
57
59 last_dot = self._exception_type_name.rfind('.')
60 assert last_dot > 0, self._exception_type_name
61 module_name = self._exception_type_name[:last_dot]
62 exec 'import %s' % module_name
63 klass = eval(self._exception_type_name)
64 return new.instance(klass, {'args': str(self)})
65
67 return self._command_description
68
70 return self._input
71
72 #----------------------------------------------------------------------
73
74 # Exception and error handling
75
76 # Exception thrown by exception handler needs to terminate osh.
77 # Use ExceptionHandlerException to tunnel through except blocks.
78
87
88
89 # exception_handler is a function with these arguments:
90 # - exception: The exception being handled. In case of a remote exception, this exception
91 # object is a client-side reconstruction of the server-side exception.
92 # - op: A command of type Op, or, in case of a remote exception, a command description,
93 # obtained by applying str().
94 # - host: The host on which the exception occurred, or None if it occurred locally.
95
96 exception_handler = None
97 stderr_handler = None
98
100 if isinstance(input, list):
101 buffer.append(str(tuple(input)))
102 elif isinstance(input, tuple):
103 buffer.append(str(input))
104 else:
105 buffer.append('(')
106 buffer.append(str(input))
107 buffer.append(')')
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
| Home | Trees | Indices | Help |
|
|---|
| Generated by Epydoc 3.0beta1 on Thu Mar 27 22:39:15 2008 | http://epydoc.sourceforge.net |