|
15 | 15 | are new with this feature. They are defined as tuples containing the |
16 | 16 | derived exceptions when string-based exceptions are used. |
17 | 17 |
|
18 | | -StandardError(*) |
| 18 | +Exception(*) |
19 | 19 | | |
20 | | - +-- SystemExit |
21 | | - +-- KeyboardInterrupt |
22 | | - +-- ImportError |
23 | | - +-- IOError |
24 | | - +-- EOFError |
25 | | - +-- RuntimeError |
26 | | - +-- NameError |
27 | | - +-- AttributeError |
28 | | - +-- SyntaxError |
29 | | - +-- TypeError |
30 | | - +-- AssertionError |
31 | | - +-- LookupError(*) |
32 | | - | | |
33 | | - | +-- IndexError |
34 | | - | +-- KeyError |
35 | | - | |
36 | | - +-- NumberError(*) |
37 | | - | | |
38 | | - | +-- OverflowError |
39 | | - | +-- ZeroDivisionError |
40 | | - | +-- FloatingPointError |
41 | | - | |
42 | | - +-- ValueError |
43 | | - +-- SystemError |
44 | | - +-- MemoryError |
| 20 | + +-- StandardError(*) |
| 21 | + | |
| 22 | + +-- SystemExit |
| 23 | + +-- KeyboardInterrupt |
| 24 | + +-- ImportError |
| 25 | + +-- IOError |
| 26 | + +-- EOFError |
| 27 | + +-- RuntimeError |
| 28 | + +-- NameError |
| 29 | + +-- AttributeError |
| 30 | + +-- SyntaxError |
| 31 | + +-- TypeError |
| 32 | + +-- AssertionError |
| 33 | + +-- LookupError(*) |
| 34 | + | | |
| 35 | + | +-- IndexError |
| 36 | + | +-- KeyError |
| 37 | + | |
| 38 | + +-- NumberError(*) |
| 39 | + | | |
| 40 | + | +-- OverflowError |
| 41 | + | +-- ZeroDivisionError |
| 42 | + | +-- FloatingPointError |
| 43 | + | |
| 44 | + +-- ValueError |
| 45 | + +-- SystemError |
| 46 | + +-- MemoryError |
45 | 47 | """ |
46 | 48 |
|
47 | | -class StandardError: |
| 49 | +class Exception: |
48 | 50 | def __init__(self, *args): |
49 | | - if len(args) == 0: |
50 | | - self.args = None |
51 | | - elif len(args) == 1: |
52 | | - # de-tuplify |
53 | | - self.args = args[0] |
54 | | - else: |
55 | | - self.args = args |
| 51 | + self.args = args |
56 | 52 |
|
57 | 53 | def __str__(self): |
58 | | - if self.args == None: |
| 54 | + if not self.args: |
59 | 55 | return '' |
| 56 | + elif len(self.args) == 1: |
| 57 | + return str(self.args[0]) |
60 | 58 | else: |
61 | 59 | return str(self.args) |
62 | 60 |
|
| 61 | +class StandardError(Exception): |
63 | 62 | def __getitem__(self, i): |
64 | | - if type(self.args) == type(()): |
65 | | - return self.args[i] |
66 | | - elif i == 0: |
67 | | - return self.args |
68 | | - else: |
69 | | - raise IndexError |
| 63 | + return self.args[i] |
70 | 64 |
|
71 | 65 | class SyntaxError(StandardError): |
72 | 66 | filename = lineno = offset = text = None |
73 | | - def __init__(self, msg, info=None): |
74 | | - self.msg = msg |
75 | | - if info: |
76 | | - self.args = msg |
77 | | - else: |
78 | | - self.args = (msg, info) |
79 | | - if info: |
80 | | - self.filename, self.lineno, self.offset, self.text = info |
| 67 | + msg = "" |
| 68 | + def __init__(self, *args): |
| 69 | + self.args = args |
| 70 | + if len(self.args) >= 1: |
| 71 | + self.msg = self.args[0] |
| 72 | + if len(self.args) == 2: |
| 73 | + info = self.args[1] |
| 74 | + try: |
| 75 | + self.filename, self.lineno, self.offset, self.text = info |
| 76 | + except: |
| 77 | + pass |
81 | 78 | def __str__(self): |
82 | 79 | return str(self.msg) |
83 | 80 |
|
84 | | - |
85 | 81 | class IOError(StandardError): |
86 | 82 | def __init__(self, *args): |
| 83 | + self.args = args |
87 | 84 | self.errno = None |
88 | 85 | self.strerror = None |
89 | | - if len(args) == 1: |
90 | | - # de-tuplify |
91 | | - self.args = args[0] |
92 | | - elif len(args) == 2: |
| 86 | + if len(args) == 2: |
93 | 87 | # common case: PyErr_SetFromErrno() |
94 | | - self.args = args |
95 | 88 | self.errno = args[0] |
96 | 89 | self.strerror = args[1] |
97 | | - else: |
98 | | - self.args = args |
99 | | - |
100 | 90 |
|
101 | 91 | class RuntimeError(StandardError): |
102 | 92 | pass |
@@ -143,24 +133,21 @@ class IndexError(LookupError): |
143 | 133 | class KeyError(LookupError): |
144 | 134 | pass |
145 | 135 |
|
146 | | -# debate: should these two inherit from LookupError? |
147 | 136 | class AttributeError(StandardError): |
148 | 137 | pass |
149 | 138 |
|
150 | 139 | class NameError(StandardError): |
151 | 140 | pass |
152 | 141 |
|
153 | | -class SystemExit(StandardError): |
| 142 | +class MemoryError(StandardError): |
| 143 | + pass |
| 144 | + |
| 145 | +class SystemExit(Exception): |
154 | 146 | def __init__(self, *args): |
| 147 | + self.args = args |
155 | 148 | if len(args) == 0: |
156 | | - self.args = None |
| 149 | + self.code = None |
157 | 150 | elif len(args) == 1: |
158 | | - # de-tuplify |
159 | | - self.args = args[0] |
| 151 | + self.code = args[0] |
160 | 152 | else: |
161 | | - self.args = args |
162 | | - self.code = self.args |
163 | | - |
164 | | - |
165 | | -class MemoryError(StandardError): |
166 | | - pass |
| 153 | + self.code = args |
0 commit comments