blob: 781a00405c81ca65ae34f569abe3df693bbcaddf [file] [log] [blame]
Damiend99b0522013-12-21 18:17:45 +00001#include <string.h>
Damien George6c73ca12014-01-08 18:11:23 +00002#include <stdarg.h>
Damiend99b0522013-12-21 18:17:45 +00003#include <assert.h>
4
5#include "nlr.h"
6#include "misc.h"
7#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +00008#include "qstr.h"
Damiend99b0522013-12-21 18:17:45 +00009#include "obj.h"
Paul Sokolovskyddf21782014-01-12 23:30:20 +020010#include "objtuple.h"
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +020011#include "runtime.h"
12#include "runtime0.h"
Damiend99b0522013-12-21 18:17:45 +000013
Damien George07ddab52014-03-29 13:15:08 +000014typedef struct _mp_obj_exception_t {
Damiend99b0522013-12-21 18:17:45 +000015 mp_obj_base_t base;
Damien George136b1492014-01-19 12:38:49 +000016 mp_obj_t traceback; // a list object, holding (file,line,block) as numbers (not Python objects); a hack for now
Paul Sokolovskyddf21782014-01-12 23:30:20 +020017 mp_obj_tuple_t args;
Damiend99b0522013-12-21 18:17:45 +000018} mp_obj_exception_t;
19
Damien George6902eed2014-04-04 10:52:59 +000020// Instance of MemoryError exception - needed by mp_malloc_fail
21const mp_obj_exception_t mp_const_MemoryError_obj = {{&mp_type_MemoryError}, MP_OBJ_NULL, {{&mp_type_tuple}, 0}};
22
Damien Georgef0954e32014-04-10 14:38:25 +010023// Local non-heap memory for allocating an exception when we run out of RAM
24STATIC mp_obj_exception_t mp_emergency_exception_obj;
25
Paul Sokolovskyc4d589e2014-03-28 02:37:28 +020026// Instance of GeneratorExit exception - needed by generator.close()
27// This would belong to objgenerator.c, but to keep mp_obj_exception_t
28// definition module-private so far, have it here.
Paul Sokolovskya96d3d02014-03-31 01:10:10 +030029const mp_obj_exception_t mp_const_GeneratorExit_obj = {{&mp_type_GeneratorExit}, MP_OBJ_NULL, {{&mp_type_tuple}, 0}};
Paul Sokolovskyc4d589e2014-03-28 02:37:28 +020030
Damien Georgec5966122014-02-15 16:10:44 +000031STATIC void mp_obj_exception_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +000032 mp_obj_exception_t *o = o_in;
Paul Sokolovskya96d3d02014-03-31 01:10:10 +030033 if (kind == PRINT_REPR) {
34 print(env, "%s", qstr_str(o->base.type->name));
35 } else if (kind == PRINT_EXC) {
36 print(env, "%s: ", qstr_str(o->base.type->name));
Damiend99b0522013-12-21 18:17:45 +000037 }
Paul Sokolovskya96d3d02014-03-31 01:10:10 +030038 if (kind == PRINT_STR || kind == PRINT_EXC) {
39 if (o->args.len == 0) {
40 print(env, "");
41 return;
42 } else if (o->args.len == 1) {
43 mp_obj_print_helper(print, env, o->args.items[0], PRINT_STR);
44 return;
45 }
46 }
47 tuple_print(print, env, &o->args, kind);
Damiend99b0522013-12-21 18:17:45 +000048}
49
Damien Georgec5966122014-02-15 16:10:44 +000050STATIC mp_obj_t mp_obj_exception_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
51 mp_obj_type_t *type = type_in;
Damien George20006db2014-01-18 14:10:48 +000052
53 if (n_kw != 0) {
Damien Georgeea13f402014-04-05 18:32:08 +010054 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "%s does not take keyword arguments", mp_obj_get_type_str(type_in)));
Damien George20006db2014-01-18 14:10:48 +000055 }
56
Damien George58ba4c32014-04-10 14:27:31 +000057 mp_obj_exception_t *o = m_new_obj_var_maybe(mp_obj_exception_t, mp_obj_t, n_args);
Damien Georgef0954e32014-04-10 14:38:25 +010058 if (o == NULL) {
59 // Couldn't allocate heap memory; use local data instead.
60 o = &mp_emergency_exception_obj;
61 // We can't store any args.
62 n_args = 0;
63 }
Damien Georgec5966122014-02-15 16:10:44 +000064 o->base.type = type;
Paul Sokolovsky60a0d3f2014-02-14 21:01:12 +020065 o->traceback = MP_OBJ_NULL;
Damien George07ddab52014-03-29 13:15:08 +000066 o->args.base.type = &mp_type_tuple;
Paul Sokolovskyddf21782014-01-12 23:30:20 +020067 o->args.len = n_args;
Damien George20006db2014-01-18 14:10:48 +000068 memcpy(o->args.items, args, n_args * sizeof(mp_obj_t));
Paul Sokolovskyddf21782014-01-12 23:30:20 +020069 return o;
70}
71
Paul Sokolovskyaf1ae302014-03-26 19:17:20 +020072// Get exception "value" - that is, first argument, or None
73mp_obj_t mp_obj_exception_get_value(mp_obj_t self_in) {
74 mp_obj_exception_t *self = self_in;
75 if (self->args.len == 0) {
76 return mp_const_none;
77 } else {
78 return self->args.items[0];
79 }
80}
81
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +020082STATIC void exception_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
83 mp_obj_exception_t *self = self_in;
84 if (attr == MP_QSTR_args) {
85 dest[0] = &self->args;
86 } else if (self->base.type == &mp_type_StopIteration && attr == MP_QSTR_value) {
Paul Sokolovskyaf1ae302014-03-26 19:17:20 +020087 dest[0] = mp_obj_exception_get_value(self);
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +020088 }
89}
90
Damien Georgec5966122014-02-15 16:10:44 +000091const mp_obj_type_t mp_type_BaseException = {
92 { &mp_type_type },
93 .name = MP_QSTR_BaseException,
94 .print = mp_obj_exception_print,
95 .make_new = mp_obj_exception_make_new,
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +020096 .load_attr = exception_load_attr,
Damiend99b0522013-12-21 18:17:45 +000097};
98
Damien George22a08652014-02-15 21:05:25 +000099#define MP_DEFINE_EXCEPTION_BASE(base_name) \
Damien George07ddab52014-03-29 13:15:08 +0000100STATIC const mp_obj_tuple_t mp_type_ ## base_name ## _base_tuple = {{&mp_type_tuple}, 1, {(mp_obj_t)&mp_type_ ## base_name}};\
Damien George22a08652014-02-15 21:05:25 +0000101
102#define MP_DEFINE_EXCEPTION(exc_name, base_name) \
Damien Georgec5966122014-02-15 16:10:44 +0000103const mp_obj_type_t mp_type_ ## exc_name = { \
104 { &mp_type_type }, \
105 .name = MP_QSTR_ ## exc_name, \
106 .print = mp_obj_exception_print, \
107 .make_new = mp_obj_exception_make_new, \
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +0200108 .load_attr = exception_load_attr, \
Damien George22a08652014-02-15 21:05:25 +0000109 .bases_tuple = (mp_obj_t)&mp_type_ ## base_name ## _base_tuple, \
Damien Georgec5966122014-02-15 16:10:44 +0000110};
111
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000112// List of all exceptions, arranged as in the table at:
113// http://docs.python.org/3.3/library/exceptions.html
Damien George22a08652014-02-15 21:05:25 +0000114MP_DEFINE_EXCEPTION_BASE(BaseException)
Damien Georgeffb5cfc2014-03-25 14:29:40 +0000115//MP_DEFINE_EXCEPTION(SystemExit, BaseException)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000116//MP_DEFINE_EXCEPTION(KeyboardInterrupt, BaseException)
117MP_DEFINE_EXCEPTION(GeneratorExit, BaseException)
118MP_DEFINE_EXCEPTION(Exception, BaseException)
119 MP_DEFINE_EXCEPTION_BASE(Exception)
120 MP_DEFINE_EXCEPTION(StopIteration, Exception)
121 MP_DEFINE_EXCEPTION(ArithmeticError, Exception)
122 MP_DEFINE_EXCEPTION_BASE(ArithmeticError)
Damien Georgec63f9842014-03-27 23:49:06 +0000123 //MP_DEFINE_EXCEPTION(FloatingPointError, ArithmeticError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000124 MP_DEFINE_EXCEPTION(OverflowError, ArithmeticError)
125 MP_DEFINE_EXCEPTION(ZeroDivisionError, ArithmeticError)
126 MP_DEFINE_EXCEPTION(AssertionError, Exception)
127 MP_DEFINE_EXCEPTION(AttributeError, Exception)
Damien Georgec63f9842014-03-27 23:49:06 +0000128 //MP_DEFINE_EXCEPTION(BufferError, Exception)
Damien Georgeffb5cfc2014-03-25 14:29:40 +0000129 //MP_DEFINE_EXCEPTION(EnvironmentError, Exception)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000130 MP_DEFINE_EXCEPTION(EOFError, Exception)
131 MP_DEFINE_EXCEPTION(ImportError, Exception)
132 MP_DEFINE_EXCEPTION(IOError, Exception)
133 MP_DEFINE_EXCEPTION(LookupError, Exception)
134 MP_DEFINE_EXCEPTION_BASE(LookupError)
135 MP_DEFINE_EXCEPTION(IndexError, LookupError)
136 MP_DEFINE_EXCEPTION(KeyError, LookupError)
137 MP_DEFINE_EXCEPTION(MemoryError, Exception)
138 MP_DEFINE_EXCEPTION(NameError, Exception)
139 MP_DEFINE_EXCEPTION_BASE(NameError)
Damien Georgec63f9842014-03-27 23:49:06 +0000140 //MP_DEFINE_EXCEPTION(UnboundLocalError, NameError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000141 MP_DEFINE_EXCEPTION(OSError, Exception)
Damien Georgec9109722014-03-22 23:40:02 +0000142 MP_DEFINE_EXCEPTION_BASE(OSError)
Damien Georgec63f9842014-03-27 23:49:06 +0000143 /*
144 MP_DEFINE_EXCEPTION(BlockingIOError, OSError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000145 MP_DEFINE_EXCEPTION(ChildProcessError, OSError)
146 MP_DEFINE_EXCEPTION(ConnectionError, OSError)
147 MP_DEFINE_EXCEPTION(BrokenPipeError, ConnectionError)
148 MP_DEFINE_EXCEPTION(ConnectionAbortedError, ConnectionError)
149 MP_DEFINE_EXCEPTION(ConnectionRefusedError, ConnectionError)
150 MP_DEFINE_EXCEPTION(ConnectionResetError, ConnectionError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000151 MP_DEFINE_EXCEPTION(InterruptedError, OSError)
152 MP_DEFINE_EXCEPTION(IsADirectoryError, OSError)
153 MP_DEFINE_EXCEPTION(NotADirectoryError, OSError)
154 MP_DEFINE_EXCEPTION(PermissionError, OSError)
155 MP_DEFINE_EXCEPTION(ProcessLookupError, OSError)
Damien Georgec63f9842014-03-27 23:49:06 +0000156 MP_DEFINE_EXCEPTION(TimeoutError, OSError)
Damien Georgec9109722014-03-22 23:40:02 +0000157 MP_DEFINE_EXCEPTION(FileExistsError, OSError)
158 MP_DEFINE_EXCEPTION(FileNotFoundError, OSError)
Damien Georgec63f9842014-03-27 23:49:06 +0000159 MP_DEFINE_EXCEPTION(ReferenceError, Exception)
160 */
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000161 MP_DEFINE_EXCEPTION(RuntimeError, Exception)
162 MP_DEFINE_EXCEPTION_BASE(RuntimeError)
163 MP_DEFINE_EXCEPTION(NotImplementedError, RuntimeError)
164 MP_DEFINE_EXCEPTION(SyntaxError, Exception)
165 MP_DEFINE_EXCEPTION_BASE(SyntaxError)
166 MP_DEFINE_EXCEPTION(IndentationError, SyntaxError)
Damien Georgec63f9842014-03-27 23:49:06 +0000167 /*
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000168 MP_DEFINE_EXCEPTION_BASE(IndentationError)
Damien Georgec63f9842014-03-27 23:49:06 +0000169 MP_DEFINE_EXCEPTION(TabError, IndentationError)
170 */
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000171 MP_DEFINE_EXCEPTION(SystemError, Exception)
172 MP_DEFINE_EXCEPTION(TypeError, Exception)
173 MP_DEFINE_EXCEPTION(ValueError, Exception)
174 //TODO: Implement UnicodeErrors which take arguments
Damien Georgec9109722014-03-22 23:40:02 +0000175 /*
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000176 MP_DEFINE_EXCEPTION(Warning, Exception)
177 MP_DEFINE_EXCEPTION_BASE(Warning)
178 MP_DEFINE_EXCEPTION(DeprecationWarning, Warning)
179 MP_DEFINE_EXCEPTION(PendingDeprecationWarning, Warning)
180 MP_DEFINE_EXCEPTION(RuntimeWarning, Warning)
181 MP_DEFINE_EXCEPTION(SyntaxWarning, Warning)
182 MP_DEFINE_EXCEPTION(UserWarning, Warning)
183 MP_DEFINE_EXCEPTION(FutureWarning, Warning)
184 MP_DEFINE_EXCEPTION(ImportWarning, Warning)
185 MP_DEFINE_EXCEPTION(UnicodeWarning, Warning)
186 MP_DEFINE_EXCEPTION(BytesWarning, Warning)
187 MP_DEFINE_EXCEPTION(ResourceWarning, Warning)
Damien Georgec9109722014-03-22 23:40:02 +0000188 */
Damien Georgec5966122014-02-15 16:10:44 +0000189
190mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type) {
Paul Sokolovskya96d3d02014-03-31 01:10:10 +0300191 return mp_obj_new_exception_args(exc_type, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000192}
193
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200194mp_obj_t mp_obj_new_exception_args(const mp_obj_type_t *exc_type, uint n_args, const mp_obj_t *args) {
195 assert(exc_type->make_new == mp_obj_exception_make_new);
196 return exc_type->make_new((mp_obj_t)exc_type, n_args, 0, args);
197}
198
Damien Georgec5966122014-02-15 16:10:44 +0000199mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, const char *msg) {
200 return mp_obj_new_exception_msg_varg(exc_type, msg);
Damiend99b0522013-12-21 18:17:45 +0000201}
202
Damien Georgec5966122014-02-15 16:10:44 +0000203mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char *fmt, ...) {
204 // check that the given type is an exception type
205 assert(exc_type->make_new == mp_obj_exception_make_new);
206
Damien George6c73ca12014-01-08 18:11:23 +0000207 // make exception object
Damien George58ba4c32014-04-10 14:27:31 +0000208 mp_obj_exception_t *o = m_new_obj_var_maybe(mp_obj_exception_t, mp_obj_t, 1);
Damien Georgef0954e32014-04-10 14:38:25 +0100209 if (o == NULL) {
210 // Couldn't allocate heap memory; use local data instead.
211 // Unfortunately, we won't be able to format the string...
212 o = &mp_emergency_exception_obj;
213 o->base.type = exc_type;
214 o->traceback = MP_OBJ_NULL;
215 o->args.base.type = &mp_type_tuple;
216 o->args.len = 0;
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200217 } else {
Damien Georgef0954e32014-04-10 14:38:25 +0100218 o->base.type = exc_type;
219 o->traceback = MP_OBJ_NULL;
220 o->args.base.type = &mp_type_tuple;
221 o->args.len = 1;
222
223 if (fmt == NULL) {
224 // no message
225 assert(0);
226 } else {
227 // render exception message and store as .args[0]
228 // TODO: optimize bufferbloat
229 vstr_t *vstr = vstr_new();
230 va_list ap;
231 va_start(ap, fmt);
232 vstr_vprintf(vstr, fmt, ap);
233 va_end(ap);
234 o->args.items[0] = mp_obj_new_str((byte*)vstr->buf, vstr->len, false);
235 vstr_free(vstr);
236 }
Damien George6c73ca12014-01-08 18:11:23 +0000237 }
Damien George6c73ca12014-01-08 18:11:23 +0000238
239 return o;
240}
241
Damien Georgec5966122014-02-15 16:10:44 +0000242// return true if the given object is an exception type
Damien Georgec5966122014-02-15 16:10:44 +0000243bool mp_obj_is_exception_type(mp_obj_t self_in) {
244 if (MP_OBJ_IS_TYPE(self_in, &mp_type_type)) {
Damien George9e6e9352014-03-26 18:37:06 +0000245 // optimisation when self_in is a builtin exception
Damien Georgec5966122014-02-15 16:10:44 +0000246 mp_obj_type_t *self = self_in;
Damien George9e6e9352014-03-26 18:37:06 +0000247 if (self->make_new == mp_obj_exception_make_new) {
248 return true;
249 }
Damien Georgec5966122014-02-15 16:10:44 +0000250 }
Damien George9e6e9352014-03-26 18:37:06 +0000251 return mp_obj_is_subclass_fast(self_in, &mp_type_BaseException);
Damien Georgec5966122014-02-15 16:10:44 +0000252}
253
254// return true if the given object is an instance of an exception type
Damien Georgec5966122014-02-15 16:10:44 +0000255bool mp_obj_is_exception_instance(mp_obj_t self_in) {
Damien George9e6e9352014-03-26 18:37:06 +0000256 return mp_obj_is_exception_type(mp_obj_get_type(self_in));
Damien Georgec5966122014-02-15 16:10:44 +0000257}
258
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200259// return true if exception (type or instance) is a subclass of given
260// exception type.
261bool mp_obj_exception_match(mp_obj_t exc, const mp_obj_type_t *exc_type) {
262 // TODO: move implementation from RT_BINARY_OP_EXCEPTION_MATCH here.
Damien Georged17926d2014-03-30 13:35:08 +0100263 return mp_binary_op(MP_BINARY_OP_EXCEPTION_MATCH, exc, (mp_obj_t)exc_type) == mp_const_true;
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200264}
265
Damien Georgec5966122014-02-15 16:10:44 +0000266void mp_obj_exception_clear_traceback(mp_obj_t self_in) {
267 // make sure self_in is an exception instance
Damien George9e6e9352014-03-26 18:37:06 +0000268 // TODO add traceback information to user-defined exceptions (need proper builtin subclassing for that)
269 if (mp_obj_get_type(self_in)->make_new == mp_obj_exception_make_new) {
270 mp_obj_exception_t *self = self_in;
Damien Georgec5966122014-02-15 16:10:44 +0000271
Damien George9e6e9352014-03-26 18:37:06 +0000272 // just set the traceback to the null object
273 // we don't want to call any memory management functions here
274 self->traceback = MP_OBJ_NULL;
275 }
Damienb86e3f92013-12-29 17:17:43 +0000276}
Damien George08335002014-01-18 23:24:36 +0000277
Damien George136b1492014-01-19 12:38:49 +0000278void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, machine_uint_t line, qstr block) {
Damien Georgec5966122014-02-15 16:10:44 +0000279 // make sure self_in is an exception instance
Damien George9e6e9352014-03-26 18:37:06 +0000280 // TODO add traceback information to user-defined exceptions (need proper builtin subclassing for that)
Damien Georgef0954e32014-04-10 14:38:25 +0100281 if (mp_obj_get_type(self_in)->make_new == mp_obj_exception_make_new && self_in != &mp_emergency_exception_obj) {
Damien George9e6e9352014-03-26 18:37:06 +0000282 mp_obj_exception_t *self = self_in;
Damien Georgec5966122014-02-15 16:10:44 +0000283
Damien George9e6e9352014-03-26 18:37:06 +0000284 // for traceback, we are just using the list object for convenience, it's not really a list of Python objects
285 if (self->traceback == MP_OBJ_NULL) {
286 self->traceback = mp_obj_new_list(0, NULL);
287 }
288 mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)file);
289 mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)line);
290 mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)block);
Damien George08335002014-01-18 23:24:36 +0000291 }
Damien George08335002014-01-18 23:24:36 +0000292}
293
Damien George136b1492014-01-19 12:38:49 +0000294void mp_obj_exception_get_traceback(mp_obj_t self_in, machine_uint_t *n, machine_uint_t **values) {
Damien Georgec5966122014-02-15 16:10:44 +0000295 // make sure self_in is an exception instance
296 assert(mp_obj_get_type(self_in)->make_new == mp_obj_exception_make_new);
Damien George08335002014-01-18 23:24:36 +0000297 mp_obj_exception_t *self = self_in;
Damien Georgec5966122014-02-15 16:10:44 +0000298
Damien George136b1492014-01-19 12:38:49 +0000299 if (self->traceback == MP_OBJ_NULL) {
300 *n = 0;
301 *values = NULL;
302 } else {
303 uint n2;
304 mp_obj_list_get(self->traceback, &n2, (mp_obj_t**)values);
305 *n = n2;
306 }
Damien George08335002014-01-18 23:24:36 +0000307}