1997-08-29 21:59:26 +00:00
|
|
|
"""Class based built-in exception hierarchy.
|
|
|
|
|
1998-09-25 22:43:21 +00:00
|
|
|
New with Python 1.5, all standard built-in exceptions are now class objects by
|
|
|
|
default. This gives Python's exception handling mechanism a more
|
|
|
|
object-oriented feel. Traditionally they were string objects. Python will
|
|
|
|
fallback to string based exceptions if the interpreter is invoked with the -X
|
|
|
|
option, or if some failure occurs during class exception initialization (in
|
|
|
|
this case a warning will be printed).
|
1997-08-29 21:59:26 +00:00
|
|
|
|
1998-09-25 22:43:21 +00:00
|
|
|
Most existing code should continue to work with class based exceptions. Some
|
|
|
|
tricky uses of IOError may break, but the most common uses should work.
|
1997-08-29 21:59:26 +00:00
|
|
|
|
1998-09-25 22:43:21 +00:00
|
|
|
Here is a rundown of the class hierarchy. You can change this by editing this
|
|
|
|
file, but it isn't recommended. The class names described here are expected
|
1998-12-01 18:36:30 +00:00
|
|
|
to be found by the bltinmodule.c file. If you add classes here, you must
|
|
|
|
modify bltinmodule.c or the exceptions won't be available in the __builtin__
|
|
|
|
module, nor will they be accessible from C.
|
1997-08-29 21:59:26 +00:00
|
|
|
|
1998-12-01 18:36:30 +00:00
|
|
|
The classes with a `*' are new since Python 1.5. They are defined as tuples
|
1998-09-25 22:43:21 +00:00
|
|
|
containing the derived exceptions when string-based exceptions are used. If
|
|
|
|
you define your own class based exceptions, they should be derived from
|
|
|
|
Exception.
|
1997-08-29 21:59:26 +00:00
|
|
|
|
1997-09-16 18:42:04 +00:00
|
|
|
Exception(*)
|
1997-08-29 21:59:26 +00:00
|
|
|
|
|
1997-09-16 18:42:04 +00:00
|
|
|
+-- StandardError(*)
|
|
|
|
|
|
|
|
|
+-- SystemExit
|
|
|
|
+-- KeyboardInterrupt
|
|
|
|
+-- ImportError
|
1998-09-25 22:43:21 +00:00
|
|
|
+-- EnvironmentError(*)
|
|
|
|
| |
|
|
|
|
| +-- IOError
|
|
|
|
| +-- OSError(*)
|
|
|
|
|
|
1997-09-16 18:42:04 +00:00
|
|
|
+-- EOFError
|
|
|
|
+-- RuntimeError
|
1998-12-01 18:36:30 +00:00
|
|
|
| |
|
|
|
|
| +-- NotImplementedError(*)
|
|
|
|
|
|
1997-09-16 18:42:04 +00:00
|
|
|
+-- NameError
|
|
|
|
+-- AttributeError
|
|
|
|
+-- SyntaxError
|
|
|
|
+-- TypeError
|
|
|
|
+-- AssertionError
|
|
|
|
+-- LookupError(*)
|
|
|
|
| |
|
|
|
|
| +-- IndexError
|
|
|
|
| +-- KeyError
|
|
|
|
|
|
1997-09-16 21:50:59 +00:00
|
|
|
+-- ArithmeticError(*)
|
1997-09-16 18:42:04 +00:00
|
|
|
| |
|
|
|
|
| +-- OverflowError
|
|
|
|
| +-- ZeroDivisionError
|
|
|
|
| +-- FloatingPointError
|
|
|
|
|
|
|
|
|
+-- ValueError
|
|
|
|
+-- SystemError
|
|
|
|
+-- MemoryError
|
1997-08-29 21:59:26 +00:00
|
|
|
"""
|
|
|
|
|
1997-09-16 18:42:04 +00:00
|
|
|
class Exception:
|
1997-08-29 21:59:26 +00:00
|
|
|
def __init__(self, *args):
|
1998-03-26 21:13:24 +00:00
|
|
|
self.args = args
|
1997-08-29 21:59:26 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
1997-09-16 18:42:04 +00:00
|
|
|
if not self.args:
|
1997-08-29 21:59:26 +00:00
|
|
|
return ''
|
1998-03-26 21:13:24 +00:00
|
|
|
elif len(self.args) == 1:
|
|
|
|
return str(self.args[0])
|
|
|
|
else:
|
|
|
|
return str(self.args)
|
1997-08-29 21:59:26 +00:00
|
|
|
|
|
|
|
def __getitem__(self, i):
|
1998-03-26 21:13:24 +00:00
|
|
|
return self.args[i]
|
1997-08-29 21:59:26 +00:00
|
|
|
|
1997-10-06 15:48:20 +00:00
|
|
|
class StandardError(Exception):
|
|
|
|
pass
|
|
|
|
|
1997-08-29 21:59:26 +00:00
|
|
|
class SyntaxError(StandardError):
|
1997-09-05 19:00:56 +00:00
|
|
|
filename = lineno = offset = text = None
|
1997-09-16 18:42:04 +00:00
|
|
|
msg = ""
|
|
|
|
def __init__(self, *args):
|
1998-03-26 21:13:24 +00:00
|
|
|
self.args = args
|
|
|
|
if len(self.args) >= 1:
|
|
|
|
self.msg = self.args[0]
|
|
|
|
if len(self.args) == 2:
|
|
|
|
info = self.args[1]
|
|
|
|
try:
|
|
|
|
self.filename, self.lineno, self.offset, self.text = info
|
|
|
|
except:
|
|
|
|
pass
|
1997-08-29 21:59:26 +00:00
|
|
|
def __str__(self):
|
1997-09-05 19:00:56 +00:00
|
|
|
return str(self.msg)
|
1997-08-29 21:59:26 +00:00
|
|
|
|
1998-07-23 16:03:46 +00:00
|
|
|
class EnvironmentError(StandardError):
|
|
|
|
"""Base class for exceptions that occur outside the Python system.
|
|
|
|
Primarily used as a base class for OSError and IOError."""
|
1997-08-29 21:59:26 +00:00
|
|
|
def __init__(self, *args):
|
1998-03-26 21:13:24 +00:00
|
|
|
self.args = args
|
1997-08-29 21:59:26 +00:00
|
|
|
self.errno = None
|
|
|
|
self.strerror = None
|
1998-07-23 16:03:46 +00:00
|
|
|
self.filename = None
|
|
|
|
if len(args) == 3:
|
|
|
|
# open() errors give third argument which is the filename. BUT,
|
|
|
|
# so common in-place unpacking doesn't break, e.g.:
|
|
|
|
#
|
|
|
|
# except IOError, (errno, strerror):
|
|
|
|
#
|
|
|
|
# we hack args so that it only contains two items. This also
|
|
|
|
# means we need our own __str__() which prints out the filename
|
|
|
|
# when it was supplied.
|
|
|
|
self.errno, self.strerror, self.filename = args
|
|
|
|
self.args = args[0:2]
|
1997-09-16 18:42:04 +00:00
|
|
|
if len(args) == 2:
|
1997-08-29 21:59:26 +00:00
|
|
|
# common case: PyErr_SetFromErrno()
|
1998-07-23 16:03:46 +00:00
|
|
|
self.errno, self.strerror = args
|
|
|
|
|
|
|
|
def __str__(self):
|
1998-08-11 18:01:32 +00:00
|
|
|
if self.filename is not None:
|
1998-07-28 17:30:06 +00:00
|
|
|
return '[Errno %s] %s: %s' % (self.errno, self.strerror,
|
1998-08-11 18:01:32 +00:00
|
|
|
repr(self.filename))
|
1998-07-23 16:03:46 +00:00
|
|
|
elif self.errno and self.strerror:
|
1998-07-28 17:30:06 +00:00
|
|
|
return '[Errno %s] %s' % (self.errno, self.strerror)
|
1998-07-23 16:03:46 +00:00
|
|
|
else:
|
|
|
|
return StandardError.__str__(self)
|
|
|
|
|
|
|
|
class IOError(EnvironmentError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class OSError(EnvironmentError):
|
|
|
|
"""Used by the posix module."""
|
|
|
|
pass
|
1997-08-29 21:59:26 +00:00
|
|
|
|
|
|
|
class RuntimeError(StandardError):
|
|
|
|
pass
|
1998-12-01 18:36:30 +00:00
|
|
|
|
|
|
|
class NotImplementedError(RuntimeError):
|
|
|
|
pass
|
1997-08-29 21:59:26 +00:00
|
|
|
|
|
|
|
class SystemError(StandardError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class EOFError(StandardError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class ImportError(StandardError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class TypeError(StandardError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class ValueError(StandardError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class KeyboardInterrupt(StandardError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class AssertionError(StandardError):
|
|
|
|
pass
|
|
|
|
|
1997-09-16 21:50:59 +00:00
|
|
|
class ArithmeticError(StandardError):
|
1997-08-29 21:59:26 +00:00
|
|
|
pass
|
|
|
|
|
1997-09-16 21:50:59 +00:00
|
|
|
class OverflowError(ArithmeticError):
|
1997-08-29 21:59:26 +00:00
|
|
|
pass
|
|
|
|
|
1997-09-16 21:50:59 +00:00
|
|
|
class FloatingPointError(ArithmeticError):
|
1997-08-29 21:59:26 +00:00
|
|
|
pass
|
|
|
|
|
1997-09-16 21:50:59 +00:00
|
|
|
class ZeroDivisionError(ArithmeticError):
|
1997-08-29 21:59:26 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
class LookupError(StandardError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class IndexError(LookupError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class KeyError(LookupError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class AttributeError(StandardError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class NameError(StandardError):
|
|
|
|
pass
|
|
|
|
|
1997-09-16 18:42:04 +00:00
|
|
|
class MemoryError(StandardError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class SystemExit(Exception):
|
1997-08-29 21:59:26 +00:00
|
|
|
def __init__(self, *args):
|
1998-03-26 21:13:24 +00:00
|
|
|
self.args = args
|
1997-08-29 21:59:26 +00:00
|
|
|
if len(args) == 0:
|
1997-09-16 18:42:04 +00:00
|
|
|
self.code = None
|
1997-08-29 21:59:26 +00:00
|
|
|
elif len(args) == 1:
|
1997-09-16 18:42:04 +00:00
|
|
|
self.code = args[0]
|
1997-08-29 21:59:26 +00:00
|
|
|
else:
|
1997-09-16 18:42:04 +00:00
|
|
|
self.code = args
|