-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy patherrors.py
More file actions
229 lines (187 loc) · 5.75 KB
/
errors.py
File metadata and controls
229 lines (187 loc) · 5.75 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
from builtin import signature
from interface import Object, null
from string import String
import space
import os
# To have custom exceptions, we resort to having an unwinder.
class Unwinder(Exception):
_immutable_fields_ = ['exception', 'traceback']
__slots__ = ['exception', 'traceback']
__attrs__ = ['exception', 'traceback']
def __init__(self, exception, traceback):
self.exception = exception
self.traceback = traceback
# The exceptions themselves must be able to hold traceback.
class LException(Object):
def __init__(self):
self.traceback = null
def getattr(self, name):
if name == u"traceback":
return self.traceback
return Object.getattr(self, name)
def setattr(self, name, value):
if name == u"traceback":
self.traceback = value
return null
return Object.setattr(self, name, value)
class LError(LException):
def __init__(self, message):
self.message = message
self.traceback = null
def getattr(self, name):
if name == u"message":
return space.String(self.message)
return LException.getattr(self, name)
def repr(self):
return self.message
@LError.instantiator
@signature(String)
def _(message):
return LError(message.string)
class LAssertionError(LException):
def __init__(self, thing):
self.thing = thing
self.traceback = null
def repr(self):
return self.thing.repr()
@LAssertionError.instantiator
@signature(Object)
def _(thing):
return LAssertionError(thing)
class LSystemExit(LException):
def __init__(self, status):
self.status = status
self.traceback = null
def repr(self):
return u"%d" % self.status
class LUncatchedStopIteration(LException):
def __init__(self):
self.traceback = null
def repr(self):
return u""
class LAttributeError(LException):
def __init__(self, obj, name):
self.traceback = null
self.obj = obj
self.name = name
def repr(self):
return u"%s.%s" % (self.obj.repr(), self.name)
@LAttributeError.instantiator
@signature(Object, String)
def _(obj, name):
return LAttributeError(obj, name.string)
class LKeyError(LException):
def __init__(self, obj, key):
self.traceback = null
self.obj = obj
self.key = key
def repr(self):
return u"%s[%s]" % (self.obj.repr(), self.key.repr())
@LKeyError.instantiator
@signature(Object, String)
def _(obj, name):
return LAttributeError(obj, name.string)
class LValueError(LException):
def __init__(self, obj, value):
self.traceback = null
self.obj = obj
self.value = value
def repr(self):
return u"%s in %s" % (self.value.repr(), self.obj.repr())
@LValueError.instantiator
@signature(Object, Object)
def _(obj, value):
return LValueError(obj, value)
class LTypeError(LException):
def __init__(self, message):
self.traceback = null
self.message = message
def repr(self):
return self.message
@LTypeError.instantiator
@signature(String)
def _(message):
return LTypeError(message.string)
class LFrozenError(LException):
def __init__(self, obj):
self.traceback = null
self.obj = obj
def repr(self):
return u"%s is frozen" % self.obj.repr()
@LFrozenError.instantiator
@signature(Object)
def _(obj):
return LFrozenError(obj)
# TODO: improve this. :)
# Consider putting trace entries into Builtins, so
# that tracebacks present movement through builtin
# entries as well.
class LCallError(LException):
def __init__(self, min, max, variadic, got):
self.traceback = null
self.min = min
self.max = max
self.variadic = variadic
self.got = got
def repr(self):
if self.got < self.min:
return u"expected at least %d arguments, received %d" % (self.min, self.got)
return u"expected maximum %d arguments, received %d" % (self.max, self.got)
class LInstructionError(LException):
def __init__(self, name, opcode):
self.traceback = null
self.name = name
self.opcode = opcode
def repr(self):
return u"unexpected instruction: " + self.name
class LIOError(LException):
def __init__(self, filename, errno):
self.traceback = null
self.filename = filename
self.errno = errno
def repr(self):
message = os.strerror(self.errno).decode('utf-8')
return u"%s: %s" % (self.filename.repr(), message)
class LUVError(LException):
def __init__(self, error_name, strerror):
self.traceback = null
self.error_name = error_name
self.strerror = strerror
def getattr(self, name):
if name == u"name":
return String(self.error_name)
if name == u"strerror":
return String(self.strerror)
return LException.getattr(self, name)
def listattr(self):
listing = LException.listattr(self)
listing.append(String(u"name"))
listing.append(String(u"strerror"))
return listing
def repr(self):
return u"LibUV " + self.error_name + u":" + self.strerror
# Legacy handling for errors.
def OldError(message):
return unwind(LError(message))
# convenience function to produce a valid unwinder.
# Note that you don't want to pass user tracebacks with this thing.
def unwind(exc):
exc.traceback = traceback = space.List([])
return Unwinder(exc, traceback)
# This is used to plug them into base module
all_errors = [
LError,
LException,
LAssertionError,
LSystemExit,
LUncatchedStopIteration,
LAttributeError,
LKeyError,
LValueError,
LTypeError,
LFrozenError,
LCallError,
LInstructionError,
LIOError,
LUVError,
]