-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexception.py
281 lines (227 loc) · 8.15 KB
/
exception.py
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# -*- coding: utf-8 -*-
# This file is part of the pymfony package.
#
# (c) Alexandre Quercia <[email protected]>
#
# For the full copyright and license information, please view the LICENSE
# file that was distributed with this source code.
from __future__ import absolute_import;
import sys;
import linecache;
from pymfony.component.system import Object;
from pymfony.component.system.oop import final;
"""
"""
class StandardException(Exception, Object):
STACK_PATTERN = 'File "{filename}", line {lineno}, in {name}';
def __init__(self, message="", code=None, previous=None):
"""Construct the exception
@param message: string The Exception message to raise.
@param code: int The Exception code.
@param previous: BaseException The previous exception used for
the exception chaining.
"""
if previous: assert isinstance(previous, BaseException);
else: previous = None;
if code: assert isinstance(code, int);
else: code = 0;
self._message = None;
self._code = None;
self._previous = None;
self._file = None;
self._line = None;
self._lineno = None;
self._lines = None;
self.__string = None;
self.__traceAsString = None;
self.__trace = None;
self.__trace = self.__createTrace();
self._message = str(message);
self._code = int(code);
self._previous = previous;
def __createTrace(self):
trace = [];
try:
raise Exception;
except Exception:
tb = sys.exc_info()[2];
f = tb.tb_frame.f_back.f_back;
while f is not None:
localVars = f.f_locals;
if 'self' in localVars and isinstance(localVars['self'], self.__class__):
f = f.f_back;
continue;
filename = f.f_code.co_filename;
lineno = f.f_lineno;
name = f.f_code.co_name;
argcount = f.f_code.co_argcount;
allLines = linecache.getlines(filename, f.f_globals);
line = "";
lines = list();
nbLines = len(allLines);
if 1 <= lineno <= nbLines:
line = allLines[lineno - 1].strip();
upAndDown = 5;
startLineno = lineno - upAndDown;
startLineno = 1 if startLineno <= 1 else startLineno;
endLineno = lineno + upAndDown;
endLineno = nbLines if endLineno >= nbLines else endLineno;
for i in range(startLineno, endLineno + 1):
lines.append( (i, allLines[i-1]) );
stack = {
'filename' : filename,
'lineno' : lineno,
'name' : name,
'line' : line,
'lines' : lines, # [(int, string), ...]
'locals' : localVars,
'argcount' : argcount,
};
trace.append(stack);
f = f.f_back;
return trace;
def __formatTrace(self, trace):
string = "";
i = -1;
for stack in reversed(trace):
i += 1;
string += "#" + str(i) + " " + self._formatStack(stack) + "\n";
return string;
def _formatStack(self, stack):
argList = [];
args = "";
if not stack['name'].startswith('<'):
for name, value in stack['locals'].items():
argList.append(name + '=' + repr(value));
if argList:
args = "(" + ", ".join(argList) + ")";
formater = self.STACK_PATTERN + ", with {0}"
return formater.format(args, **stack);
@final
def getMessage(self):
"""Return the exception message
@return: string The exception message
"""
return self._message;
@final
def getPrevious(self):
"""Return the previous exception
@return: BaseException The previous exception
"""
return self._previous;
@final
def getCode(self):
"""Return the Exception code.
@return: int The Exception code.
"""
return self._code;
@final
def getFile(self):
"""Return the name of the file where the exception was raise.
@return: string the name of the file where the exception was raise.
"""
if self._file:
return self._file;
self._file = self.getTrace()[0]['filename'];
return self._file;
@final
def getLineno(self):
"""Return the line number where the exception was raise.
@return: int The line number where the exception was raise.
"""
if self._lineno:
return self._lineno;
self._lineno = self.getTrace()[0]['lineno'];
return self._lineno;
@final
def getLine(self):
"""Return the line content where the exception was raise.
@return: string The line content where the exception was raise.
"""
if self._line:
return self._line;
self._line = self.getTrace()[0]['line'];
return self._line;
@final
def getLines(self):
"""Return lines content where the exception was raise.
@return: dict lines content where the exception was raise.
[(int, string), ...]
"""
if self._lines:
return self._lines;
self._lines = self.getTrace()[0]['lines'];
return self._lines;
@final
def getTrace(self):
"""Return the stack trace as list.
@return: list of tuples like
(filename, lineno, name, line, lines, locals, argcount)
"""
return self.__trace;
@final
def getTraceAsString(self):
"""Return the stack trace as string.
@return: string The stack trace as string.
"""
if self.__traceAsString:
return self.__traceAsString;
self.__traceAsString = self.__formatTrace(self.getTrace());
return self.__traceAsString;
def __str__(self):
"""Return the string representation of exception.
@return: string representation of exception.
"""
if self.__string:
return self.__string;
self.__string = '';
trace = self.getTrace();
if self._previous:
if not isinstance(self._previous, self.__class__):
self.__string += "An exception '{0}' was previously raised with message '{1}'\n".format(
type(self._previous).__name__,
str(self._previous),
);
else:
self.__string += str(self._previous);
trace = list();
for stack in self.getTrace():
if stack in self._previous.getTrace():
break;
trace.append(stack);
self.__string += '\nNext ';
self.__string += "exception '{0}' with message '{1}'\n".format(
type(self).__name__,
self._message,
self._file,
self._line,
);
self.__string += "Traceback (most recent call last):\n";
self.__string += self.__formatTrace(trace) if trace != self.getTraceAsString() else self.getTraceAsString();
self.__string += "{0}: {1}\n".format(
type(self).__name__,
self._message
);
return self.__string;
@final
def __copy__(self):
"""Prevent clone.
"""
raise TypeError();
class LogicException(StandardException):
pass;
class InvalidArgumentException(LogicException):
pass;
class BadMethodCallException(LogicException):
pass;
class OutOfBoundsException(LogicException, IndexError):
"""Exception thrown if a value is not a valid key.
"""
class RuntimeException(StandardException):
pass;
class UnexpectedValueException(RuntimeException):
"""Exception raise if a value does not match with a set of values.
Typically this happens when a function calls another function and expects
the return value to be of a certain type or value not including arithmetic
or buffer related errors.
"""