Added 'list_only' option (and modified 'run()' to respect it).
[python/dscho.git] / Lib / exceptions.py
blob56eed92d804be5e5547ce584f4c2506316a682a6
1 """Class based built-in exception hierarchy.
3 New with Python 1.5, all standard built-in exceptions are now class objects by
4 default. This gives Python's exception handling mechanism a more
5 object-oriented feel. Traditionally they were string objects. Python will
6 fallback to string based exceptions if the interpreter is invoked with the -X
7 option, or if some failure occurs during class exception initialization (in
8 this case a warning will be printed).
10 Most existing code should continue to work with class based exceptions. Some
11 tricky uses of IOError may break, but the most common uses should work.
13 Here is a rundown of the class hierarchy. You can change this by editing this
14 file, but it isn't recommended because the old string based exceptions won't
15 be kept in sync. The class names described here are expected to be found by
16 the bltinmodule.c file. If you add classes here, you must modify
17 bltinmodule.c or the exceptions won't be available in the __builtin__ module,
18 nor will they be accessible from C.
20 The classes with a `*' are new since Python 1.5. They are defined as tuples
21 containing the derived exceptions when string-based exceptions are used. If
22 you define your own class based exceptions, they should be derived from
23 Exception.
25 Exception(*)
27 +-- SystemExit
28 +-- StandardError(*)
30 +-- KeyboardInterrupt
31 +-- ImportError
32 +-- EnvironmentError(*)
33 | |
34 | +-- IOError
35 | +-- OSError(*)
37 +-- EOFError
38 +-- RuntimeError
39 | |
40 | +-- NotImplementedError(*)
42 +-- NameError
43 | |
44 | +-- UnboundLocalError(*)
46 +-- AttributeError
47 +-- SyntaxError
48 +-- TypeError
49 +-- AssertionError
50 +-- LookupError(*)
51 | |
52 | +-- IndexError
53 | +-- KeyError
55 +-- ArithmeticError(*)
56 | |
57 | +-- OverflowError
58 | +-- ZeroDivisionError
59 | +-- FloatingPointError
61 +-- ValueError
62 +-- SystemError
63 +-- MemoryError
64 """
66 class Exception:
67 """Proposed base class for all exceptions."""
68 def __init__(self, *args):
69 self.args = args
71 def __str__(self):
72 if not self.args:
73 return ''
74 elif len(self.args) == 1:
75 return str(self.args[0])
76 else:
77 return str(self.args)
79 def __getitem__(self, i):
80 return self.args[i]
82 class StandardError(Exception):
83 """Base class for all standard Python exceptions."""
84 pass
86 class SyntaxError(StandardError):
87 """Invalid syntax."""
88 filename = lineno = offset = text = None
89 msg = ""
90 def __init__(self, *args):
91 self.args = args
92 if len(self.args) >= 1:
93 self.msg = self.args[0]
94 if len(self.args) == 2:
95 info = self.args[1]
96 try:
97 self.filename, self.lineno, self.offset, self.text = info
98 except:
99 pass
100 def __str__(self):
101 return str(self.msg)
103 class EnvironmentError(StandardError):
104 """Base class for I/O related errors."""
105 def __init__(self, *args):
106 self.args = args
107 self.errno = None
108 self.strerror = None
109 self.filename = None
110 if len(args) == 3:
111 # open() errors give third argument which is the filename. BUT,
112 # so common in-place unpacking doesn't break, e.g.:
114 # except IOError, (errno, strerror):
116 # we hack args so that it only contains two items. This also
117 # means we need our own __str__() which prints out the filename
118 # when it was supplied.
119 self.errno, self.strerror, self.filename = args
120 self.args = args[0:2]
121 if len(args) == 2:
122 # common case: PyErr_SetFromErrno()
123 self.errno, self.strerror = args
125 def __str__(self):
126 if self.filename is not None:
127 return '[Errno %s] %s: %s' % (self.errno, self.strerror,
128 repr(self.filename))
129 elif self.errno and self.strerror:
130 return '[Errno %s] %s' % (self.errno, self.strerror)
131 else:
132 return StandardError.__str__(self)
134 class IOError(EnvironmentError):
135 """I/O operation failed."""
136 pass
138 class OSError(EnvironmentError):
139 """OS system call failed."""
140 pass
142 class RuntimeError(StandardError):
143 """Unspecified run-time error."""
144 pass
146 class NotImplementedError(RuntimeError):
147 """Method or function hasn't been implemented yet."""
148 pass
150 class SystemError(StandardError):
151 """Internal error in the Python interpreter.
153 Please report this to the Python maintainer, along with the traceback,
154 the Python version, and the hardware/OS platform and version."""
155 pass
157 class EOFError(StandardError):
158 """Read beyond end of file."""
159 pass
161 class ImportError(StandardError):
162 """Import can't find module, or can't find name in module."""
163 pass
165 class TypeError(StandardError):
166 """Inappropriate argument type."""
167 pass
169 class ValueError(StandardError):
170 """Inappropriate argument value (of correct type)."""
171 pass
173 class KeyboardInterrupt(StandardError):
174 """Program interrupted by user."""
175 pass
177 class AssertionError(StandardError):
178 """Assertion failed."""
179 pass
181 class ArithmeticError(StandardError):
182 """Base class for arithmetic errors."""
183 pass
185 class OverflowError(ArithmeticError):
186 """Result too large to be represented."""
187 pass
189 class FloatingPointError(ArithmeticError):
190 """Floating point operation failed."""
191 pass
193 class ZeroDivisionError(ArithmeticError):
194 """Second argument to a division or modulo operation was zero."""
195 pass
197 class LookupError(StandardError):
198 """Base class for lookup errors."""
199 pass
201 class IndexError(LookupError):
202 """Sequence index out of range."""
203 pass
205 class KeyError(LookupError):
206 """Mapping key not found."""
207 pass
209 class AttributeError(StandardError):
210 """Attribute not found."""
211 pass
213 class NameError(StandardError):
214 """Name not found globally."""
215 pass
217 class UnboundLocalError(NameError):
218 """Local name referenced but not bound to a value."""
219 pass
221 class MemoryError(StandardError):
222 """Out of memory."""
223 pass
225 class SystemExit(Exception):
226 """Request to exit from the interpreter."""
227 def __init__(self, *args):
228 self.args = args
229 if len(args) == 0:
230 self.code = None
231 elif len(args) == 1:
232 self.code = args[0]
233 else:
234 self.code = args