summaryrefslogtreecommitdiffstats
path: root/Lib/exceptions.py
blob: 4b039692a7ca5461d984d63a2e6c110ba0038dbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
"""Class based built-in exception hierarchy.

This is an experimental new feature whereby all the standard built-in
exceptions, traditionally string object, are replaced with classes.
This gives Python's exception handling mechanism a more
object-oriented feel.

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.

To use this new feature, start the python executable with the -X option.

Here is a rundown of the class hierarchy.  You can change this by
editing this file, but it isn't recommended.  The classes with a `*'
are new with this feature and are not available unless class based
exceptions are used.

StandardError(*)
 |
 +-- SystemExit
 +-- KeyboardInterrupt
 +-- ImportError
 +-- IOError
 +-- EOFError
 +-- RuntimeError
 +-- NameError
 +-- AttributeError
 +-- SyntaxError
 +-- TypeError
 +-- AssertionError
 +-- LookupError(*)
 |    |
 |    +-- IndexError
 |    +-- KeyError
 |
 +-- NumberError(*)
 |    |
 |    +-- OverflowError
 |    +-- ZeroDivisionError
 |    +-- FloatingPointError
 |
 +-- ValueError
 +-- SystemError
 +-- MemoryError
"""

class 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

    def __str__(self):
        if self.args == None:
            return ''
        elif type(self.args) == type(''):
            return self.args
        else:
            return `self.args`

    def __getitem__(self, i):
	if type(self.args) == type(()):
	    return self.args[i]
	elif i == 0:
	    return self.args
	else:
	    raise IndexError

class SyntaxError(StandardError):
    def __init__(self, msg, info):
        self.msg = msg
        self.filename, self.lineno, self.offset, self.text = info

    def __str__(self):
        return msg


class IOError(StandardError):
    def __init__(self, *args):
        self.errno = None
        self.strerror = None
        if len(args) == 1:
            # de-tuplify
            self.args = args[0]
        elif len(args) == 2:
            # common case: PyErr_SetFromErrno()
            self.args = args
            self.errno = args[0]
            self.strerror = args[1]
        else:
            self.args = args


class RuntimeError(StandardError):
    pass

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

class NumberError(StandardError):
    pass

class OverflowError(NumberError):
    pass

class FloatingPointError(NumberError):
    pass

class ZeroDivisionError(NumberError):
    pass

class LookupError(StandardError):
    pass

class IndexError(LookupError):
    pass

class KeyError(LookupError):
    pass

# debate: should these two inherit from LookupError?
class AttributeError(StandardError):
    pass

class NameError(StandardError):
    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):
    pass