error.py
234 lines
| 5.7 KiB
| text/x-python
|
PythonLexer
Brian E Granger
|
r1234 | # encoding: utf-8 | ||
"""Classes and functions for kernel related errors and exceptions.""" | ||||
__docformat__ = "restructuredtext en" | ||||
Fernando Perez
|
r2133 | # Tell nose to skip this module | ||
__test__ = {} | ||||
Brian E Granger
|
r1234 | #------------------------------------------------------------------------------- | ||
# Copyright (C) 2008 The IPython Development Team | ||||
# | ||||
# Distributed under the terms of the BSD License. The full license is in | ||||
# the file COPYING, distributed as part of this software. | ||||
#------------------------------------------------------------------------------- | ||||
#------------------------------------------------------------------------------- | ||||
# Imports | ||||
#------------------------------------------------------------------------------- | ||||
Brian Granger
|
r2507 | |||
Brian Granger
|
r2509 | from twisted.python import failure | ||
Brian E Granger
|
r1234 | from IPython.kernel.core import error | ||
#------------------------------------------------------------------------------- | ||||
# Error classes | ||||
#------------------------------------------------------------------------------- | ||||
class KernelError(error.IPythonError): | ||||
pass | ||||
Brian Granger
|
r2509 | |||
Brian E Granger
|
r1234 | class NotDefined(KernelError): | ||
def __init__(self, name): | ||||
self.name = name | ||||
self.args = (name,) | ||||
def __repr__(self): | ||||
return '<NotDefined: %s>' % self.name | ||||
__str__ = __repr__ | ||||
Brian Granger
|
r2509 | |||
Brian E Granger
|
r1234 | class QueueCleared(KernelError): | ||
pass | ||||
Brian Granger
|
r2509 | |||
Brian E Granger
|
r1234 | class IdInUse(KernelError): | ||
pass | ||||
Brian Granger
|
r2509 | |||
Brian E Granger
|
r1234 | class ProtocolError(KernelError): | ||
pass | ||||
Brian Granger
|
r2509 | |||
Brian E Granger
|
r1234 | class ConnectionError(KernelError): | ||
pass | ||||
Brian Granger
|
r2509 | |||
Brian E Granger
|
r1234 | class InvalidEngineID(KernelError): | ||
pass | ||||
Brian Granger
|
r2509 | |||
Brian E Granger
|
r1234 | class NoEnginesRegistered(KernelError): | ||
pass | ||||
Brian Granger
|
r2509 | |||
Brian E Granger
|
r1234 | class InvalidClientID(KernelError): | ||
pass | ||||
Brian Granger
|
r2509 | |||
Brian E Granger
|
r1234 | class InvalidDeferredID(KernelError): | ||
pass | ||||
Brian Granger
|
r2509 | |||
Brian E Granger
|
r1234 | class SerializationError(KernelError): | ||
pass | ||||
Brian Granger
|
r2509 | |||
Brian E Granger
|
r1234 | class MessageSizeError(KernelError): | ||
pass | ||||
Brian Granger
|
r2509 | |||
Brian E Granger
|
r1234 | class PBMessageSizeError(MessageSizeError): | ||
pass | ||||
Brian Granger
|
r2509 | |||
Brian E Granger
|
r1234 | class ResultNotCompleted(KernelError): | ||
pass | ||||
Brian Granger
|
r2509 | |||
Brian E Granger
|
r1234 | class ResultAlreadyRetrieved(KernelError): | ||
pass | ||||
Brian Granger
|
r2509 | |||
Brian E Granger
|
r1234 | class ClientError(KernelError): | ||
pass | ||||
Brian Granger
|
r2509 | |||
Brian E Granger
|
r1234 | class TaskAborted(KernelError): | ||
pass | ||||
Brian Granger
|
r2509 | |||
Brian E Granger
|
r1234 | class TaskTimeout(KernelError): | ||
pass | ||||
Brian Granger
|
r2509 | |||
Brian E Granger
|
r1234 | class NotAPendingResult(KernelError): | ||
pass | ||||
Brian Granger
|
r2509 | |||
Brian E Granger
|
r1234 | class UnpickleableException(KernelError): | ||
pass | ||||
Brian Granger
|
r2509 | |||
Brian E Granger
|
r1234 | class AbortedPendingDeferredError(KernelError): | ||
pass | ||||
Brian Granger
|
r2509 | |||
Brian E Granger
|
r1234 | class InvalidProperty(KernelError): | ||
pass | ||||
Brian Granger
|
r2509 | |||
Brian E Granger
|
r1234 | class MissingBlockArgument(KernelError): | ||
pass | ||||
Brian Granger
|
r2509 | |||
Brian E Granger
|
r1234 | class StopLocalExecution(KernelError): | ||
pass | ||||
Brian Granger
|
r2509 | |||
Brian E Granger
|
r1234 | class SecurityError(KernelError): | ||
pass | ||||
Brian Granger
|
r2509 | |||
Brian Granger
|
r1944 | class FileTimeoutError(KernelError): | ||
pass | ||||
Brian Granger
|
r2509 | |||
Brian Granger
|
r1952 | class TaskRejectError(KernelError): | ||
"""Exception to raise when a task should be rejected by an engine. | ||||
This exception can be used to allow a task running on an engine to test | ||||
if the engine (or the user's namespace on the engine) has the needed | ||||
task dependencies. If not, the task should raise this exception. For | ||||
the task to be retried on another engine, the task should be created | ||||
with the `retries` argument > 1. | ||||
The advantage of this approach over our older properties system is that | ||||
tasks have full access to the user's namespace on the engines and the | ||||
properties don't have to be managed or tested by the controller. | ||||
""" | ||||
Brian Granger
|
r2509 | |||
Brian E Granger
|
r1234 | class CompositeError(KernelError): | ||
def __init__(self, message, elist): | ||||
Exception.__init__(self, *(message, elist)) | ||||
Brian Granger
|
r2293 | # Don't use pack_exception because it will conflict with the .message | ||
# attribute that is being deprecated in 2.6 and beyond. | ||||
self.msg = message | ||||
Brian E Granger
|
r1234 | self.elist = elist | ||
Brian Granger
|
r2293 | |||
Brian E Granger
|
r1234 | def _get_engine_str(self, ev): | ||
try: | ||||
ei = ev._ipython_engine_info | ||||
except AttributeError: | ||||
return '[Engine Exception]' | ||||
else: | ||||
return '[%i:%s]: ' % (ei['engineid'], ei['method']) | ||||
Brian Granger
|
r2293 | |||
Brian E Granger
|
r1234 | def _get_traceback(self, ev): | ||
try: | ||||
tb = ev._ipython_traceback_text | ||||
except AttributeError: | ||||
return 'No traceback available' | ||||
else: | ||||
return tb | ||||
Brian Granger
|
r2293 | |||
Brian E Granger
|
r1234 | def __str__(self): | ||
Brian Granger
|
r2293 | s = str(self.msg) | ||
Brian E Granger
|
r1234 | for et, ev, etb in self.elist: | ||
engine_str = self._get_engine_str(ev) | ||||
s = s + '\n' + engine_str + str(et.__name__) + ': ' + str(ev) | ||||
return s | ||||
Brian Granger
|
r2293 | |||
Brian E Granger
|
r1234 | def print_tracebacks(self, excid=None): | ||
if excid is None: | ||||
for (et,ev,etb) in self.elist: | ||||
print self._get_engine_str(ev) | ||||
print self._get_traceback(ev) | ||||
else: | ||||
try: | ||||
et,ev,etb = self.elist[excid] | ||||
except: | ||||
raise IndexError("an exception with index %i does not exist"%excid) | ||||
else: | ||||
print self._get_engine_str(ev) | ||||
print self._get_traceback(ev) | ||||
def raise_exception(self, excid=0): | ||||
try: | ||||
et,ev,etb = self.elist[excid] | ||||
except: | ||||
raise IndexError("an exception with index %i does not exist"%excid) | ||||
else: | ||||
raise et, ev, etb | ||||
Fernando Perez
|
r2482 | |||
Brian Granger
|
r2509 | def collect_exceptions(rlist, method): | ||
Brian E Granger
|
r1234 | elist = [] | ||
for r in rlist: | ||||
if isinstance(r, failure.Failure): | ||||
r.cleanFailure() | ||||
et, ev, etb = r.type, r.value, r.tb | ||||
# Sometimes we could have CompositeError in our list. Just take | ||||
# the errors out of them and put them in our new list. This | ||||
# has the effect of flattening lists of CompositeErrors into one | ||||
# CompositeError | ||||
if et==CompositeError: | ||||
for e in ev.elist: | ||||
elist.append(e) | ||||
else: | ||||
elist.append((et, ev, etb)) | ||||
if len(elist)==0: | ||||
return rlist | ||||
else: | ||||
msg = "one or more exceptions from call to method: %s" % (method) | ||||
# This silliness is needed so the debugger has access to the exception | ||||
# instance (e in this case) | ||||
try: | ||||
raise CompositeError(msg, elist) | ||||
except CompositeError, e: | ||||
raise e | ||||