Two subtle changes:

(1) Introduce Exception as the conceptual root class for all exceptions.

(2) Do less work in __init__(), and more in __str__ (store args
unchanged).
This commit is contained in:
Guido van Rossum 1997-09-16 18:42:04 +00:00
parent 3d26cc9542
commit c56ba38350
1 changed files with 58 additions and 71 deletions

View File

@ -15,88 +15,78 @@ editing this file, but it isn't recommended. The classes with a `*'
are new with this feature. They are defined as tuples containing the are new with this feature. They are defined as tuples containing the
derived exceptions when string-based exceptions are used. derived exceptions when string-based exceptions are used.
StandardError(*) Exception(*)
| |
+-- SystemExit +-- StandardError(*)
+-- KeyboardInterrupt |
+-- ImportError +-- SystemExit
+-- IOError +-- KeyboardInterrupt
+-- EOFError +-- ImportError
+-- RuntimeError +-- IOError
+-- NameError +-- EOFError
+-- AttributeError +-- RuntimeError
+-- SyntaxError +-- NameError
+-- TypeError +-- AttributeError
+-- AssertionError +-- SyntaxError
+-- LookupError(*) +-- TypeError
| | +-- AssertionError
| +-- IndexError +-- LookupError(*)
| +-- KeyError | |
| | +-- IndexError
+-- NumberError(*) | +-- KeyError
| | |
| +-- OverflowError +-- NumberError(*)
| +-- ZeroDivisionError | |
| +-- FloatingPointError | +-- OverflowError
| | +-- ZeroDivisionError
+-- ValueError | +-- FloatingPointError
+-- SystemError |
+-- MemoryError +-- ValueError
+-- SystemError
+-- MemoryError
""" """
class StandardError: class Exception:
def __init__(self, *args): def __init__(self, *args):
if len(args) == 0: self.args = args
self.args = None
elif len(args) == 1:
# de-tuplify
self.args = args[0]
else:
self.args = args
def __str__(self): def __str__(self):
if self.args == None: if not self.args:
return '' return ''
elif len(self.args) == 1:
return str(self.args[0])
else: else:
return str(self.args) return str(self.args)
class StandardError(Exception):
def __getitem__(self, i): def __getitem__(self, i):
if type(self.args) == type(()): return self.args[i]
return self.args[i]
elif i == 0:
return self.args
else:
raise IndexError
class SyntaxError(StandardError): class SyntaxError(StandardError):
filename = lineno = offset = text = None filename = lineno = offset = text = None
def __init__(self, msg, info=None): msg = ""
self.msg = msg def __init__(self, *args):
if info: self.args = args
self.args = msg if len(self.args) >= 1:
else: self.msg = self.args[0]
self.args = (msg, info) if len(self.args) == 2:
if info: info = self.args[1]
self.filename, self.lineno, self.offset, self.text = info try:
self.filename, self.lineno, self.offset, self.text = info
except:
pass
def __str__(self): def __str__(self):
return str(self.msg) return str(self.msg)
class IOError(StandardError): class IOError(StandardError):
def __init__(self, *args): def __init__(self, *args):
self.args = args
self.errno = None self.errno = None
self.strerror = None self.strerror = None
if len(args) == 1: if len(args) == 2:
# de-tuplify
self.args = args[0]
elif len(args) == 2:
# common case: PyErr_SetFromErrno() # common case: PyErr_SetFromErrno()
self.args = args
self.errno = args[0] self.errno = args[0]
self.strerror = args[1] self.strerror = args[1]
else:
self.args = args
class RuntimeError(StandardError): class RuntimeError(StandardError):
pass pass
@ -143,24 +133,21 @@ class IndexError(LookupError):
class KeyError(LookupError): class KeyError(LookupError):
pass pass
# debate: should these two inherit from LookupError?
class AttributeError(StandardError): class AttributeError(StandardError):
pass pass
class NameError(StandardError): class NameError(StandardError):
pass pass
class SystemExit(StandardError):
def __init__(self, *args):
if len(args) == 0:
self.args = None
elif len(args) == 1:
# de-tuplify
self.args = args[0]
else:
self.args = args
self.code = self.args
class MemoryError(StandardError): class MemoryError(StandardError):
pass pass
class SystemExit(Exception):
def __init__(self, *args):
self.args = args
if len(args) == 0:
self.code = None
elif len(args) == 1:
self.code = args[0]
else:
self.code = args