Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c56ba38

Browse files
committed
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).
1 parent 3d26cc9 commit c56ba38

1 file changed

Lines changed: 56 additions & 69 deletions

File tree

Lib/exceptions.py

Lines changed: 56 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -15,88 +15,78 @@
1515
are new with this feature. They are defined as tuples containing the
1616
derived exceptions when string-based exceptions are used.
1717
18-
StandardError(*)
18+
Exception(*)
1919
|
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
4547
"""
4648

47-
class StandardError:
49+
class Exception:
4850
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
5652

5753
def __str__(self):
58-
if self.args == None:
54+
if not self.args:
5955
return ''
56+
elif len(self.args) == 1:
57+
return str(self.args[0])
6058
else:
6159
return str(self.args)
6260

61+
class StandardError(Exception):
6362
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]
7064

7165
class SyntaxError(StandardError):
7266
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
8178
def __str__(self):
8279
return str(self.msg)
8380

84-
8581
class IOError(StandardError):
8682
def __init__(self, *args):
83+
self.args = args
8784
self.errno = None
8885
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:
9387
# common case: PyErr_SetFromErrno()
94-
self.args = args
9588
self.errno = args[0]
9689
self.strerror = args[1]
97-
else:
98-
self.args = args
99-
10090

10191
class RuntimeError(StandardError):
10292
pass
@@ -143,24 +133,21 @@ class IndexError(LookupError):
143133
class KeyError(LookupError):
144134
pass
145135

146-
# debate: should these two inherit from LookupError?
147136
class AttributeError(StandardError):
148137
pass
149138

150139
class NameError(StandardError):
151140
pass
152141

153-
class SystemExit(StandardError):
142+
class MemoryError(StandardError):
143+
pass
144+
145+
class SystemExit(Exception):
154146
def __init__(self, *args):
147+
self.args = args
155148
if len(args) == 0:
156-
self.args = None
149+
self.code = None
157150
elif len(args) == 1:
158-
# de-tuplify
159-
self.args = args[0]
151+
self.code = args[0]
160152
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

Comments
 (0)