Show More
@@ -1,25 +1,25 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """The IPython1 kernel. |
|
2 | """The IPython1 kernel. | |
3 |
|
3 | |||
4 | The IPython kernel actually refers to three things: |
|
4 | The IPython kernel actually refers to three things: | |
5 |
|
5 | |||
6 | * The IPython Engine |
|
6 | * The IPython Engine | |
7 | * The IPython Controller |
|
7 | * The IPython Controller | |
8 | * Clients to the IPython Controller |
|
8 | * Clients to the IPython Controller | |
9 |
|
9 | |||
10 | The kernel module implements the engine, controller and client and all the |
|
10 | The kernel module implements the engine, controller and client and all the | |
11 | network protocols needed for the various entities to talk to each other. |
|
11 | network protocols needed for the various entities to talk to each other. | |
12 |
|
12 | |||
13 | An end user should probably begin by looking at the `client.py` module |
|
13 | An end user should probably begin by looking at the `client.py` module | |
14 | if they need blocking clients or in `asyncclient.py` if they want asynchronous, |
|
14 | if they need blocking clients or in `asyncclient.py` if they want asynchronous, | |
15 | deferred/Twisted using clients. |
|
15 | deferred/Twisted using clients. | |
16 | """ |
|
16 | """ | |
17 | __docformat__ = "restructuredtext en" |
|
17 | __docformat__ = "restructuredtext en" | |
18 | #----------------------------------------------------------------------------- |
|
18 | #----------------------------------------------------------------------------- | |
19 | # Copyright (C) 2008 The IPython Development Team |
|
19 | # Copyright (C) 2008 The IPython Development Team | |
20 | # |
|
20 | # | |
21 | # Distributed under the terms of the BSD License. The full license is in |
|
21 | # Distributed under the terms of the BSD License. The full license is in | |
22 | # the file COPYING, distributed as part of this software. |
|
22 | # the file COPYING, distributed as part of this software. | |
23 | #----------------------------------------------------------------------------- |
|
23 | #----------------------------------------------------------------------------- | |
24 |
|
24 | |||
25 | from IPython.kernel.error import TaskRejectError No newline at end of file |
|
25 | from IPython.kernel.error import TaskRejectError |
@@ -1,207 +1,207 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 |
|
2 | |||
3 | """Classes and functions for kernel related errors and exceptions.""" |
|
3 | """Classes and functions for kernel related errors and exceptions.""" | |
4 |
|
4 | |||
5 | __docformat__ = "restructuredtext en" |
|
5 | __docformat__ = "restructuredtext en" | |
6 |
|
6 | |||
7 | # Tell nose to skip this module |
|
7 | # Tell nose to skip this module | |
8 | __test__ = {} |
|
8 | __test__ = {} | |
9 |
|
9 | |||
10 | #------------------------------------------------------------------------------- |
|
10 | #------------------------------------------------------------------------------- | |
11 | # Copyright (C) 2008 The IPython Development Team |
|
11 | # Copyright (C) 2008 The IPython Development Team | |
12 | # |
|
12 | # | |
13 | # Distributed under the terms of the BSD License. The full license is in |
|
13 | # Distributed under the terms of the BSD License. The full license is in | |
14 | # the file COPYING, distributed as part of this software. |
|
14 | # the file COPYING, distributed as part of this software. | |
15 | #------------------------------------------------------------------------------- |
|
15 | #------------------------------------------------------------------------------- | |
16 |
|
16 | |||
17 | #------------------------------------------------------------------------------- |
|
17 | #------------------------------------------------------------------------------- | |
18 | # Imports |
|
18 | # Imports | |
19 | #------------------------------------------------------------------------------- |
|
19 | #------------------------------------------------------------------------------- | |
20 | from twisted.python import failure |
|
|||
21 |
|
||||
22 | from IPython.kernel.core import error |
|
20 | from IPython.kernel.core import error | |
23 |
|
21 | |||
24 | #------------------------------------------------------------------------------- |
|
22 | #------------------------------------------------------------------------------- | |
25 | # Error classes |
|
23 | # Error classes | |
26 | #------------------------------------------------------------------------------- |
|
24 | #------------------------------------------------------------------------------- | |
27 |
|
25 | |||
28 | class KernelError(error.IPythonError): |
|
26 | class KernelError(error.IPythonError): | |
29 | pass |
|
27 | pass | |
30 |
|
28 | |||
31 | class NotDefined(KernelError): |
|
29 | class NotDefined(KernelError): | |
32 | def __init__(self, name): |
|
30 | def __init__(self, name): | |
33 | self.name = name |
|
31 | self.name = name | |
34 | self.args = (name,) |
|
32 | self.args = (name,) | |
35 |
|
33 | |||
36 | def __repr__(self): |
|
34 | def __repr__(self): | |
37 | return '<NotDefined: %s>' % self.name |
|
35 | return '<NotDefined: %s>' % self.name | |
38 |
|
36 | |||
39 | __str__ = __repr__ |
|
37 | __str__ = __repr__ | |
40 |
|
38 | |||
41 | class QueueCleared(KernelError): |
|
39 | class QueueCleared(KernelError): | |
42 | pass |
|
40 | pass | |
43 |
|
41 | |||
44 | class IdInUse(KernelError): |
|
42 | class IdInUse(KernelError): | |
45 | pass |
|
43 | pass | |
46 |
|
44 | |||
47 | class ProtocolError(KernelError): |
|
45 | class ProtocolError(KernelError): | |
48 | pass |
|
46 | pass | |
49 |
|
47 | |||
50 | class ConnectionError(KernelError): |
|
48 | class ConnectionError(KernelError): | |
51 | pass |
|
49 | pass | |
52 |
|
50 | |||
53 | class InvalidEngineID(KernelError): |
|
51 | class InvalidEngineID(KernelError): | |
54 | pass |
|
52 | pass | |
55 |
|
53 | |||
56 | class NoEnginesRegistered(KernelError): |
|
54 | class NoEnginesRegistered(KernelError): | |
57 | pass |
|
55 | pass | |
58 |
|
56 | |||
59 | class InvalidClientID(KernelError): |
|
57 | class InvalidClientID(KernelError): | |
60 | pass |
|
58 | pass | |
61 |
|
59 | |||
62 | class InvalidDeferredID(KernelError): |
|
60 | class InvalidDeferredID(KernelError): | |
63 | pass |
|
61 | pass | |
64 |
|
62 | |||
65 | class SerializationError(KernelError): |
|
63 | class SerializationError(KernelError): | |
66 | pass |
|
64 | pass | |
67 |
|
65 | |||
68 | class MessageSizeError(KernelError): |
|
66 | class MessageSizeError(KernelError): | |
69 | pass |
|
67 | pass | |
70 |
|
68 | |||
71 | class PBMessageSizeError(MessageSizeError): |
|
69 | class PBMessageSizeError(MessageSizeError): | |
72 | pass |
|
70 | pass | |
73 |
|
71 | |||
74 | class ResultNotCompleted(KernelError): |
|
72 | class ResultNotCompleted(KernelError): | |
75 | pass |
|
73 | pass | |
76 |
|
74 | |||
77 | class ResultAlreadyRetrieved(KernelError): |
|
75 | class ResultAlreadyRetrieved(KernelError): | |
78 | pass |
|
76 | pass | |
79 |
|
77 | |||
80 | class ClientError(KernelError): |
|
78 | class ClientError(KernelError): | |
81 | pass |
|
79 | pass | |
82 |
|
80 | |||
83 | class TaskAborted(KernelError): |
|
81 | class TaskAborted(KernelError): | |
84 | pass |
|
82 | pass | |
85 |
|
83 | |||
86 | class TaskTimeout(KernelError): |
|
84 | class TaskTimeout(KernelError): | |
87 | pass |
|
85 | pass | |
88 |
|
86 | |||
89 | class NotAPendingResult(KernelError): |
|
87 | class NotAPendingResult(KernelError): | |
90 | pass |
|
88 | pass | |
91 |
|
89 | |||
92 | class UnpickleableException(KernelError): |
|
90 | class UnpickleableException(KernelError): | |
93 | pass |
|
91 | pass | |
94 |
|
92 | |||
95 | class AbortedPendingDeferredError(KernelError): |
|
93 | class AbortedPendingDeferredError(KernelError): | |
96 | pass |
|
94 | pass | |
97 |
|
95 | |||
98 | class InvalidProperty(KernelError): |
|
96 | class InvalidProperty(KernelError): | |
99 | pass |
|
97 | pass | |
100 |
|
98 | |||
101 | class MissingBlockArgument(KernelError): |
|
99 | class MissingBlockArgument(KernelError): | |
102 | pass |
|
100 | pass | |
103 |
|
101 | |||
104 | class StopLocalExecution(KernelError): |
|
102 | class StopLocalExecution(KernelError): | |
105 | pass |
|
103 | pass | |
106 |
|
104 | |||
107 | class SecurityError(KernelError): |
|
105 | class SecurityError(KernelError): | |
108 | pass |
|
106 | pass | |
109 |
|
107 | |||
110 | class FileTimeoutError(KernelError): |
|
108 | class FileTimeoutError(KernelError): | |
111 | pass |
|
109 | pass | |
112 |
|
110 | |||
113 | class TaskRejectError(KernelError): |
|
111 | class TaskRejectError(KernelError): | |
114 | """Exception to raise when a task should be rejected by an engine. |
|
112 | """Exception to raise when a task should be rejected by an engine. | |
115 |
|
113 | |||
116 | This exception can be used to allow a task running on an engine to test |
|
114 | This exception can be used to allow a task running on an engine to test | |
117 | if the engine (or the user's namespace on the engine) has the needed |
|
115 | if the engine (or the user's namespace on the engine) has the needed | |
118 | task dependencies. If not, the task should raise this exception. For |
|
116 | task dependencies. If not, the task should raise this exception. For | |
119 | the task to be retried on another engine, the task should be created |
|
117 | the task to be retried on another engine, the task should be created | |
120 | with the `retries` argument > 1. |
|
118 | with the `retries` argument > 1. | |
121 |
|
119 | |||
122 | The advantage of this approach over our older properties system is that |
|
120 | The advantage of this approach over our older properties system is that | |
123 | tasks have full access to the user's namespace on the engines and the |
|
121 | tasks have full access to the user's namespace on the engines and the | |
124 | properties don't have to be managed or tested by the controller. |
|
122 | properties don't have to be managed or tested by the controller. | |
125 | """ |
|
123 | """ | |
126 |
|
124 | |||
127 | class CompositeError(KernelError): |
|
125 | class CompositeError(KernelError): | |
128 | def __init__(self, message, elist): |
|
126 | def __init__(self, message, elist): | |
129 | Exception.__init__(self, *(message, elist)) |
|
127 | Exception.__init__(self, *(message, elist)) | |
130 | # Don't use pack_exception because it will conflict with the .message |
|
128 | # Don't use pack_exception because it will conflict with the .message | |
131 | # attribute that is being deprecated in 2.6 and beyond. |
|
129 | # attribute that is being deprecated in 2.6 and beyond. | |
132 | self.msg = message |
|
130 | self.msg = message | |
133 | self.elist = elist |
|
131 | self.elist = elist | |
134 |
|
132 | |||
135 | def _get_engine_str(self, ev): |
|
133 | def _get_engine_str(self, ev): | |
136 | try: |
|
134 | try: | |
137 | ei = ev._ipython_engine_info |
|
135 | ei = ev._ipython_engine_info | |
138 | except AttributeError: |
|
136 | except AttributeError: | |
139 | return '[Engine Exception]' |
|
137 | return '[Engine Exception]' | |
140 | else: |
|
138 | else: | |
141 | return '[%i:%s]: ' % (ei['engineid'], ei['method']) |
|
139 | return '[%i:%s]: ' % (ei['engineid'], ei['method']) | |
142 |
|
140 | |||
143 | def _get_traceback(self, ev): |
|
141 | def _get_traceback(self, ev): | |
144 | try: |
|
142 | try: | |
145 | tb = ev._ipython_traceback_text |
|
143 | tb = ev._ipython_traceback_text | |
146 | except AttributeError: |
|
144 | except AttributeError: | |
147 | return 'No traceback available' |
|
145 | return 'No traceback available' | |
148 | else: |
|
146 | else: | |
149 | return tb |
|
147 | return tb | |
150 |
|
148 | |||
151 | def __str__(self): |
|
149 | def __str__(self): | |
152 | s = str(self.msg) |
|
150 | s = str(self.msg) | |
153 | for et, ev, etb in self.elist: |
|
151 | for et, ev, etb in self.elist: | |
154 | engine_str = self._get_engine_str(ev) |
|
152 | engine_str = self._get_engine_str(ev) | |
155 | s = s + '\n' + engine_str + str(et.__name__) + ': ' + str(ev) |
|
153 | s = s + '\n' + engine_str + str(et.__name__) + ': ' + str(ev) | |
156 | return s |
|
154 | return s | |
157 |
|
155 | |||
158 | def print_tracebacks(self, excid=None): |
|
156 | def print_tracebacks(self, excid=None): | |
159 | if excid is None: |
|
157 | if excid is None: | |
160 | for (et,ev,etb) in self.elist: |
|
158 | for (et,ev,etb) in self.elist: | |
161 | print self._get_engine_str(ev) |
|
159 | print self._get_engine_str(ev) | |
162 | print self._get_traceback(ev) |
|
160 | print self._get_traceback(ev) | |
163 |
|
161 | |||
164 | else: |
|
162 | else: | |
165 | try: |
|
163 | try: | |
166 | et,ev,etb = self.elist[excid] |
|
164 | et,ev,etb = self.elist[excid] | |
167 | except: |
|
165 | except: | |
168 | raise IndexError("an exception with index %i does not exist"%excid) |
|
166 | raise IndexError("an exception with index %i does not exist"%excid) | |
169 | else: |
|
167 | else: | |
170 | print self._get_engine_str(ev) |
|
168 | print self._get_engine_str(ev) | |
171 | print self._get_traceback(ev) |
|
169 | print self._get_traceback(ev) | |
172 |
|
170 | |||
173 | def raise_exception(self, excid=0): |
|
171 | def raise_exception(self, excid=0): | |
174 | try: |
|
172 | try: | |
175 | et,ev,etb = self.elist[excid] |
|
173 | et,ev,etb = self.elist[excid] | |
176 | except: |
|
174 | except: | |
177 | raise IndexError("an exception with index %i does not exist"%excid) |
|
175 | raise IndexError("an exception with index %i does not exist"%excid) | |
178 | else: |
|
176 | else: | |
179 | raise et, ev, etb |
|
177 | raise et, ev, etb | |
180 |
|
178 | |||
181 | def collect_exceptions(rlist, method): |
|
179 | def collect_exceptions(rlist, method): | |
|
180 | from twisted.python import failure | |||
|
181 | ||||
182 | elist = [] |
|
182 | elist = [] | |
183 | for r in rlist: |
|
183 | for r in rlist: | |
184 | if isinstance(r, failure.Failure): |
|
184 | if isinstance(r, failure.Failure): | |
185 | r.cleanFailure() |
|
185 | r.cleanFailure() | |
186 | et, ev, etb = r.type, r.value, r.tb |
|
186 | et, ev, etb = r.type, r.value, r.tb | |
187 | # Sometimes we could have CompositeError in our list. Just take |
|
187 | # Sometimes we could have CompositeError in our list. Just take | |
188 | # the errors out of them and put them in our new list. This |
|
188 | # the errors out of them and put them in our new list. This | |
189 | # has the effect of flattening lists of CompositeErrors into one |
|
189 | # has the effect of flattening lists of CompositeErrors into one | |
190 | # CompositeError |
|
190 | # CompositeError | |
191 | if et==CompositeError: |
|
191 | if et==CompositeError: | |
192 | for e in ev.elist: |
|
192 | for e in ev.elist: | |
193 | elist.append(e) |
|
193 | elist.append(e) | |
194 | else: |
|
194 | else: | |
195 | elist.append((et, ev, etb)) |
|
195 | elist.append((et, ev, etb)) | |
196 | if len(elist)==0: |
|
196 | if len(elist)==0: | |
197 | return rlist |
|
197 | return rlist | |
198 | else: |
|
198 | else: | |
199 | msg = "one or more exceptions from call to method: %s" % (method) |
|
199 | msg = "one or more exceptions from call to method: %s" % (method) | |
200 | # This silliness is needed so the debugger has access to the exception |
|
200 | # This silliness is needed so the debugger has access to the exception | |
201 | # instance (e in this case) |
|
201 | # instance (e in this case) | |
202 | try: |
|
202 | try: | |
203 | raise CompositeError(msg, elist) |
|
203 | raise CompositeError(msg, elist) | |
204 | except CompositeError, e: |
|
204 | except CompositeError, e: | |
205 | raise e |
|
205 | raise e | |
206 |
|
206 | |||
207 |
|
207 |
General Comments 0
You need to be logged in to leave comments.
Login now