blob: 7dd5c7ac39e8659c340bfa4d7252ffe85383af7e [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
Paul Sokolovskyddf21782014-01-12 23:30:20 +020014// This is unified class for C-level and Python-level exceptions
Paul Sokolovsky027594e2014-01-31 17:13:51 +020015// Python-level exceptions have empty ->msg and all arguments are in
16// args tuple. C-level exceptions likely have ->msg set, and args is empty.
Damien George07ddab52014-03-29 13:15:08 +000017typedef struct _mp_obj_exception_t {
Damiend99b0522013-12-21 18:17:45 +000018 mp_obj_base_t base;
Damien George136b1492014-01-19 12:38:49 +000019 mp_obj_t traceback; // a list object, holding (file,line,block) as numbers (not Python objects); a hack for now
Damien George1ba1fac2014-01-29 18:57:20 +000020 vstr_t *msg;
Paul Sokolovskyddf21782014-01-12 23:30:20 +020021 mp_obj_tuple_t args;
Damiend99b0522013-12-21 18:17:45 +000022} mp_obj_exception_t;
23
Paul Sokolovskyc4d589e2014-03-28 02:37:28 +020024// Instance of GeneratorExit exception - needed by generator.close()
25// This would belong to objgenerator.c, but to keep mp_obj_exception_t
26// definition module-private so far, have it here.
Damien George07ddab52014-03-29 13:15:08 +000027const mp_obj_exception_t mp_const_GeneratorExit_obj = {{&mp_type_GeneratorExit}, MP_OBJ_NULL, NULL, {{&mp_type_tuple}, 0}};
Paul Sokolovskyc4d589e2014-03-28 02:37:28 +020028
Damien Georgec5966122014-02-15 16:10:44 +000029STATIC 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 +000030 mp_obj_exception_t *o = o_in;
Damien George1ba1fac2014-01-29 18:57:20 +000031 if (o->msg != NULL) {
Damien Georgec5966122014-02-15 16:10:44 +000032 print(env, "%s: %s", qstr_str(o->base.type->name), vstr_str(o->msg));
Paul Sokolovskyddf21782014-01-12 23:30:20 +020033 } else {
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020034 // Yes, that's how CPython has it
Damien Georgec8f78bc2014-02-15 22:55:00 +000035 // TODO now that exceptions are classes and instances, I think this needs to be changed to match CPython
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020036 if (kind == PRINT_REPR) {
Damien Georgec5966122014-02-15 16:10:44 +000037 print(env, "%s", qstr_str(o->base.type->name));
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020038 }
39 if (kind == PRINT_STR) {
40 if (o->args.len == 0) {
41 print(env, "");
42 return;
43 } else if (o->args.len == 1) {
44 mp_obj_print_helper(print, env, o->args.items[0], PRINT_STR);
45 return;
46 }
47 }
48 tuple_print(print, env, &o->args, kind);
Damiend99b0522013-12-21 18:17:45 +000049 }
50}
51
Damien Georgec5966122014-02-15 16:10:44 +000052STATIC mp_obj_t mp_obj_exception_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
53 mp_obj_type_t *type = type_in;
Damien George20006db2014-01-18 14:10:48 +000054
55 if (n_kw != 0) {
Damien George0ec6bd42014-03-09 16:29:36 +000056 nlr_jump(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 +000057 }
58
59 mp_obj_exception_t *o = m_new_obj_var(mp_obj_exception_t, mp_obj_t, n_args);
Damien Georgec5966122014-02-15 16:10:44 +000060 o->base.type = type;
Paul Sokolovsky60a0d3f2014-02-14 21:01:12 +020061 o->traceback = MP_OBJ_NULL;
Damien George1ba1fac2014-01-29 18:57:20 +000062 o->msg = NULL;
Damien George07ddab52014-03-29 13:15:08 +000063 o->args.base.type = &mp_type_tuple;
Paul Sokolovskyddf21782014-01-12 23:30:20 +020064 o->args.len = n_args;
Damien George20006db2014-01-18 14:10:48 +000065 memcpy(o->args.items, args, n_args * sizeof(mp_obj_t));
Paul Sokolovskyddf21782014-01-12 23:30:20 +020066 return o;
67}
68
Paul Sokolovskyaf1ae302014-03-26 19:17:20 +020069// Get exception "value" - that is, first argument, or None
70mp_obj_t mp_obj_exception_get_value(mp_obj_t self_in) {
71 mp_obj_exception_t *self = self_in;
72 if (self->args.len == 0) {
73 return mp_const_none;
74 } else {
75 return self->args.items[0];
76 }
77}
78
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +020079STATIC void exception_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
80 mp_obj_exception_t *self = self_in;
81 if (attr == MP_QSTR_args) {
82 dest[0] = &self->args;
83 } else if (self->base.type == &mp_type_StopIteration && attr == MP_QSTR_value) {
Paul Sokolovskyaf1ae302014-03-26 19:17:20 +020084 dest[0] = mp_obj_exception_get_value(self);
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +020085 }
86}
87
Damien Georgec5966122014-02-15 16:10:44 +000088const mp_obj_type_t mp_type_BaseException = {
89 { &mp_type_type },
90 .name = MP_QSTR_BaseException,
91 .print = mp_obj_exception_print,
92 .make_new = mp_obj_exception_make_new,
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +020093 .load_attr = exception_load_attr,
Damiend99b0522013-12-21 18:17:45 +000094};
95
Damien George22a08652014-02-15 21:05:25 +000096#define MP_DEFINE_EXCEPTION_BASE(base_name) \
Damien George07ddab52014-03-29 13:15:08 +000097STATIC 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 +000098
99#define MP_DEFINE_EXCEPTION(exc_name, base_name) \
Damien Georgec5966122014-02-15 16:10:44 +0000100const mp_obj_type_t mp_type_ ## exc_name = { \
101 { &mp_type_type }, \
102 .name = MP_QSTR_ ## exc_name, \
103 .print = mp_obj_exception_print, \
104 .make_new = mp_obj_exception_make_new, \
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +0200105 .load_attr = exception_load_attr, \
Damien George22a08652014-02-15 21:05:25 +0000106 .bases_tuple = (mp_obj_t)&mp_type_ ## base_name ## _base_tuple, \
Damien Georgec5966122014-02-15 16:10:44 +0000107};
108
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000109// List of all exceptions, arranged as in the table at:
110// http://docs.python.org/3.3/library/exceptions.html
Damien George22a08652014-02-15 21:05:25 +0000111MP_DEFINE_EXCEPTION_BASE(BaseException)
Damien Georgeffb5cfc2014-03-25 14:29:40 +0000112//MP_DEFINE_EXCEPTION(SystemExit, BaseException)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000113//MP_DEFINE_EXCEPTION(KeyboardInterrupt, BaseException)
114MP_DEFINE_EXCEPTION(GeneratorExit, BaseException)
115MP_DEFINE_EXCEPTION(Exception, BaseException)
116 MP_DEFINE_EXCEPTION_BASE(Exception)
117 MP_DEFINE_EXCEPTION(StopIteration, Exception)
118 MP_DEFINE_EXCEPTION(ArithmeticError, Exception)
119 MP_DEFINE_EXCEPTION_BASE(ArithmeticError)
Damien Georgec63f9842014-03-27 23:49:06 +0000120 //MP_DEFINE_EXCEPTION(FloatingPointError, ArithmeticError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000121 MP_DEFINE_EXCEPTION(OverflowError, ArithmeticError)
122 MP_DEFINE_EXCEPTION(ZeroDivisionError, ArithmeticError)
123 MP_DEFINE_EXCEPTION(AssertionError, Exception)
124 MP_DEFINE_EXCEPTION(AttributeError, Exception)
Damien Georgec63f9842014-03-27 23:49:06 +0000125 //MP_DEFINE_EXCEPTION(BufferError, Exception)
Damien Georgeffb5cfc2014-03-25 14:29:40 +0000126 //MP_DEFINE_EXCEPTION(EnvironmentError, Exception)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000127 MP_DEFINE_EXCEPTION(EOFError, Exception)
128 MP_DEFINE_EXCEPTION(ImportError, Exception)
129 MP_DEFINE_EXCEPTION(IOError, Exception)
130 MP_DEFINE_EXCEPTION(LookupError, Exception)
131 MP_DEFINE_EXCEPTION_BASE(LookupError)
132 MP_DEFINE_EXCEPTION(IndexError, LookupError)
133 MP_DEFINE_EXCEPTION(KeyError, LookupError)
134 MP_DEFINE_EXCEPTION(MemoryError, Exception)
135 MP_DEFINE_EXCEPTION(NameError, Exception)
136 MP_DEFINE_EXCEPTION_BASE(NameError)
Damien Georgec63f9842014-03-27 23:49:06 +0000137 //MP_DEFINE_EXCEPTION(UnboundLocalError, NameError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000138 MP_DEFINE_EXCEPTION(OSError, Exception)
Damien Georgec9109722014-03-22 23:40:02 +0000139 MP_DEFINE_EXCEPTION_BASE(OSError)
Damien Georgec63f9842014-03-27 23:49:06 +0000140 /*
141 MP_DEFINE_EXCEPTION(BlockingIOError, OSError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000142 MP_DEFINE_EXCEPTION(ChildProcessError, OSError)
143 MP_DEFINE_EXCEPTION(ConnectionError, OSError)
144 MP_DEFINE_EXCEPTION(BrokenPipeError, ConnectionError)
145 MP_DEFINE_EXCEPTION(ConnectionAbortedError, ConnectionError)
146 MP_DEFINE_EXCEPTION(ConnectionRefusedError, ConnectionError)
147 MP_DEFINE_EXCEPTION(ConnectionResetError, ConnectionError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000148 MP_DEFINE_EXCEPTION(InterruptedError, OSError)
149 MP_DEFINE_EXCEPTION(IsADirectoryError, OSError)
150 MP_DEFINE_EXCEPTION(NotADirectoryError, OSError)
151 MP_DEFINE_EXCEPTION(PermissionError, OSError)
152 MP_DEFINE_EXCEPTION(ProcessLookupError, OSError)
Damien Georgec63f9842014-03-27 23:49:06 +0000153 MP_DEFINE_EXCEPTION(TimeoutError, OSError)
Damien Georgec9109722014-03-22 23:40:02 +0000154 MP_DEFINE_EXCEPTION(FileExistsError, OSError)
155 MP_DEFINE_EXCEPTION(FileNotFoundError, OSError)
Damien Georgec63f9842014-03-27 23:49:06 +0000156 MP_DEFINE_EXCEPTION(ReferenceError, Exception)
157 */
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000158 MP_DEFINE_EXCEPTION(RuntimeError, Exception)
159 MP_DEFINE_EXCEPTION_BASE(RuntimeError)
160 MP_DEFINE_EXCEPTION(NotImplementedError, RuntimeError)
161 MP_DEFINE_EXCEPTION(SyntaxError, Exception)
162 MP_DEFINE_EXCEPTION_BASE(SyntaxError)
163 MP_DEFINE_EXCEPTION(IndentationError, SyntaxError)
Damien Georgec63f9842014-03-27 23:49:06 +0000164 /*
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000165 MP_DEFINE_EXCEPTION_BASE(IndentationError)
Damien Georgec63f9842014-03-27 23:49:06 +0000166 MP_DEFINE_EXCEPTION(TabError, IndentationError)
167 */
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000168 MP_DEFINE_EXCEPTION(SystemError, Exception)
169 MP_DEFINE_EXCEPTION(TypeError, Exception)
170 MP_DEFINE_EXCEPTION(ValueError, Exception)
171 //TODO: Implement UnicodeErrors which take arguments
Damien Georgec9109722014-03-22 23:40:02 +0000172 /*
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000173 MP_DEFINE_EXCEPTION(Warning, Exception)
174 MP_DEFINE_EXCEPTION_BASE(Warning)
175 MP_DEFINE_EXCEPTION(DeprecationWarning, Warning)
176 MP_DEFINE_EXCEPTION(PendingDeprecationWarning, Warning)
177 MP_DEFINE_EXCEPTION(RuntimeWarning, Warning)
178 MP_DEFINE_EXCEPTION(SyntaxWarning, Warning)
179 MP_DEFINE_EXCEPTION(UserWarning, Warning)
180 MP_DEFINE_EXCEPTION(FutureWarning, Warning)
181 MP_DEFINE_EXCEPTION(ImportWarning, Warning)
182 MP_DEFINE_EXCEPTION(UnicodeWarning, Warning)
183 MP_DEFINE_EXCEPTION(BytesWarning, Warning)
184 MP_DEFINE_EXCEPTION(ResourceWarning, Warning)
Damien Georgec9109722014-03-22 23:40:02 +0000185 */
Damien Georgec5966122014-02-15 16:10:44 +0000186
187mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type) {
188 return mp_obj_new_exception_msg_varg(exc_type, NULL);
Damiend99b0522013-12-21 18:17:45 +0000189}
190
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200191mp_obj_t mp_obj_new_exception_args(const mp_obj_type_t *exc_type, uint n_args, const mp_obj_t *args) {
192 assert(exc_type->make_new == mp_obj_exception_make_new);
193 return exc_type->make_new((mp_obj_t)exc_type, n_args, 0, args);
194}
195
Damien Georgec5966122014-02-15 16:10:44 +0000196mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, const char *msg) {
197 return mp_obj_new_exception_msg_varg(exc_type, msg);
Damiend99b0522013-12-21 18:17:45 +0000198}
199
Damien Georgec5966122014-02-15 16:10:44 +0000200mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char *fmt, ...) {
201 // check that the given type is an exception type
202 assert(exc_type->make_new == mp_obj_exception_make_new);
203
Damien George6c73ca12014-01-08 18:11:23 +0000204 // make exception object
Damien Georgec5966122014-02-15 16:10:44 +0000205 mp_obj_exception_t *o = m_new_obj_var(mp_obj_exception_t, mp_obj_t, 0);
206 o->base.type = exc_type;
Damien George136b1492014-01-19 12:38:49 +0000207 o->traceback = MP_OBJ_NULL;
Paul Sokolovsky24a140a2014-03-30 13:29:33 +0300208 o->args.base.type = &mp_type_tuple;
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200209 o->args.len = 0;
Damien Georgec5966122014-02-15 16:10:44 +0000210
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200211 if (fmt == NULL) {
Damien Georgec5966122014-02-15 16:10:44 +0000212 // no message
Damien George1ba1fac2014-01-29 18:57:20 +0000213 o->msg = NULL;
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200214 } else {
215 // render exception message
Damien George1ba1fac2014-01-29 18:57:20 +0000216 o->msg = vstr_new();
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200217 va_list ap;
218 va_start(ap, fmt);
Damien George1ba1fac2014-01-29 18:57:20 +0000219 vstr_vprintf(o->msg, fmt, ap);
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200220 va_end(ap);
Damien George6c73ca12014-01-08 18:11:23 +0000221 }
Damien George6c73ca12014-01-08 18:11:23 +0000222
223 return o;
224}
225
Damien Georgec5966122014-02-15 16:10:44 +0000226// return true if the given object is an exception type
Damien Georgec5966122014-02-15 16:10:44 +0000227bool mp_obj_is_exception_type(mp_obj_t self_in) {
228 if (MP_OBJ_IS_TYPE(self_in, &mp_type_type)) {
Damien George9e6e9352014-03-26 18:37:06 +0000229 // optimisation when self_in is a builtin exception
Damien Georgec5966122014-02-15 16:10:44 +0000230 mp_obj_type_t *self = self_in;
Damien George9e6e9352014-03-26 18:37:06 +0000231 if (self->make_new == mp_obj_exception_make_new) {
232 return true;
233 }
Damien Georgec5966122014-02-15 16:10:44 +0000234 }
Damien George9e6e9352014-03-26 18:37:06 +0000235 return mp_obj_is_subclass_fast(self_in, &mp_type_BaseException);
Damien Georgec5966122014-02-15 16:10:44 +0000236}
237
238// return true if the given object is an instance of an exception type
Damien Georgec5966122014-02-15 16:10:44 +0000239bool mp_obj_is_exception_instance(mp_obj_t self_in) {
Damien George9e6e9352014-03-26 18:37:06 +0000240 return mp_obj_is_exception_type(mp_obj_get_type(self_in));
Damien Georgec5966122014-02-15 16:10:44 +0000241}
242
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200243// return true if exception (type or instance) is a subclass of given
244// exception type.
245bool mp_obj_exception_match(mp_obj_t exc, const mp_obj_type_t *exc_type) {
246 // TODO: move implementation from RT_BINARY_OP_EXCEPTION_MATCH here.
Damien Georged17926d2014-03-30 13:35:08 +0100247 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 +0200248}
249
Damien Georgec5966122014-02-15 16:10:44 +0000250void mp_obj_exception_clear_traceback(mp_obj_t self_in) {
251 // make sure self_in is an exception instance
Damien George9e6e9352014-03-26 18:37:06 +0000252 // TODO add traceback information to user-defined exceptions (need proper builtin subclassing for that)
253 if (mp_obj_get_type(self_in)->make_new == mp_obj_exception_make_new) {
254 mp_obj_exception_t *self = self_in;
Damien Georgec5966122014-02-15 16:10:44 +0000255
Damien George9e6e9352014-03-26 18:37:06 +0000256 // just set the traceback to the null object
257 // we don't want to call any memory management functions here
258 self->traceback = MP_OBJ_NULL;
259 }
Damienb86e3f92013-12-29 17:17:43 +0000260}
Damien George08335002014-01-18 23:24:36 +0000261
Damien George136b1492014-01-19 12:38:49 +0000262void 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 +0000263 // make sure self_in is an exception instance
Damien George9e6e9352014-03-26 18:37:06 +0000264 // TODO add traceback information to user-defined exceptions (need proper builtin subclassing for that)
265 if (mp_obj_get_type(self_in)->make_new == mp_obj_exception_make_new) {
266 mp_obj_exception_t *self = self_in;
Damien Georgec5966122014-02-15 16:10:44 +0000267
Damien George9e6e9352014-03-26 18:37:06 +0000268 // for traceback, we are just using the list object for convenience, it's not really a list of Python objects
269 if (self->traceback == MP_OBJ_NULL) {
270 self->traceback = mp_obj_new_list(0, NULL);
271 }
272 mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)file);
273 mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)line);
274 mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)block);
Damien George08335002014-01-18 23:24:36 +0000275 }
Damien George08335002014-01-18 23:24:36 +0000276}
277
Damien George136b1492014-01-19 12:38:49 +0000278void 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 +0000279 // make sure self_in is an exception instance
280 assert(mp_obj_get_type(self_in)->make_new == mp_obj_exception_make_new);
Damien George08335002014-01-18 23:24:36 +0000281 mp_obj_exception_t *self = self_in;
Damien Georgec5966122014-02-15 16:10:44 +0000282
Damien George136b1492014-01-19 12:38:49 +0000283 if (self->traceback == MP_OBJ_NULL) {
284 *n = 0;
285 *values = NULL;
286 } else {
287 uint n2;
288 mp_obj_list_get(self->traceback, &n2, (mp_obj_t**)values);
289 *n = n2;
290 }
Damien George08335002014-01-18 23:24:36 +0000291}