blob: d4c4b124920cf009d8f6620db821d6ce191aa3a2 [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.
Damiend99b0522013-12-21 18:17:45 +000017typedef struct mp_obj_exception_t {
18 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.
27STATIC mp_obj_exception_t GeneratorExit_obj = {{&mp_type_GeneratorExit}, MP_OBJ_NULL, NULL, {{&tuple_type}, 0}};
28const mp_obj_t mp_const_GeneratorExit = (mp_obj_t)&GeneratorExit_obj;
29
Damien Georgec5966122014-02-15 16:10:44 +000030STATIC 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 +000031 mp_obj_exception_t *o = o_in;
Damien George1ba1fac2014-01-29 18:57:20 +000032 if (o->msg != NULL) {
Damien Georgec5966122014-02-15 16:10:44 +000033 print(env, "%s: %s", qstr_str(o->base.type->name), vstr_str(o->msg));
Paul Sokolovskyddf21782014-01-12 23:30:20 +020034 } else {
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020035 // Yes, that's how CPython has it
Damien Georgec8f78bc2014-02-15 22:55:00 +000036 // 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 +020037 if (kind == PRINT_REPR) {
Damien Georgec5966122014-02-15 16:10:44 +000038 print(env, "%s", qstr_str(o->base.type->name));
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020039 }
40 if (kind == PRINT_STR) {
41 if (o->args.len == 0) {
42 print(env, "");
43 return;
44 } else if (o->args.len == 1) {
45 mp_obj_print_helper(print, env, o->args.items[0], PRINT_STR);
46 return;
47 }
48 }
49 tuple_print(print, env, &o->args, kind);
Damiend99b0522013-12-21 18:17:45 +000050 }
51}
52
Damien Georgec5966122014-02-15 16:10:44 +000053STATIC mp_obj_t mp_obj_exception_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
54 mp_obj_type_t *type = type_in;
Damien George20006db2014-01-18 14:10:48 +000055
56 if (n_kw != 0) {
Damien George0ec6bd42014-03-09 16:29:36 +000057 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 +000058 }
59
60 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 +000061 o->base.type = type;
Paul Sokolovsky60a0d3f2014-02-14 21:01:12 +020062 o->traceback = MP_OBJ_NULL;
Damien George1ba1fac2014-01-29 18:57:20 +000063 o->msg = NULL;
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +020064 o->args.base.type = &tuple_type;
Paul Sokolovskyddf21782014-01-12 23:30:20 +020065 o->args.len = n_args;
Damien George20006db2014-01-18 14:10:48 +000066 memcpy(o->args.items, args, n_args * sizeof(mp_obj_t));
Paul Sokolovskyddf21782014-01-12 23:30:20 +020067 return o;
68}
69
Paul Sokolovskyaf1ae302014-03-26 19:17:20 +020070// Get exception "value" - that is, first argument, or None
71mp_obj_t mp_obj_exception_get_value(mp_obj_t self_in) {
72 mp_obj_exception_t *self = self_in;
73 if (self->args.len == 0) {
74 return mp_const_none;
75 } else {
76 return self->args.items[0];
77 }
78}
79
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +020080STATIC void exception_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
81 mp_obj_exception_t *self = self_in;
82 if (attr == MP_QSTR_args) {
83 dest[0] = &self->args;
84 } else if (self->base.type == &mp_type_StopIteration && attr == MP_QSTR_value) {
Paul Sokolovskyaf1ae302014-03-26 19:17:20 +020085 dest[0] = mp_obj_exception_get_value(self);
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +020086 }
87}
88
Damien Georgec5966122014-02-15 16:10:44 +000089const mp_obj_type_t mp_type_BaseException = {
90 { &mp_type_type },
91 .name = MP_QSTR_BaseException,
92 .print = mp_obj_exception_print,
93 .make_new = mp_obj_exception_make_new,
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +020094 .load_attr = exception_load_attr,
Damiend99b0522013-12-21 18:17:45 +000095};
96
Damien George22a08652014-02-15 21:05:25 +000097#define MP_DEFINE_EXCEPTION_BASE(base_name) \
98STATIC const mp_obj_tuple_t mp_type_ ## base_name ## _base_tuple = {{&tuple_type}, 1, {(mp_obj_t)&mp_type_ ## base_name}};\
99
100#define MP_DEFINE_EXCEPTION(exc_name, base_name) \
Damien Georgec5966122014-02-15 16:10:44 +0000101const mp_obj_type_t mp_type_ ## exc_name = { \
102 { &mp_type_type }, \
103 .name = MP_QSTR_ ## exc_name, \
104 .print = mp_obj_exception_print, \
105 .make_new = mp_obj_exception_make_new, \
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +0200106 .load_attr = exception_load_attr, \
Damien George22a08652014-02-15 21:05:25 +0000107 .bases_tuple = (mp_obj_t)&mp_type_ ## base_name ## _base_tuple, \
Damien Georgec5966122014-02-15 16:10:44 +0000108};
109
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000110// List of all exceptions, arranged as in the table at:
111// http://docs.python.org/3.3/library/exceptions.html
Damien George22a08652014-02-15 21:05:25 +0000112MP_DEFINE_EXCEPTION_BASE(BaseException)
Damien Georgeffb5cfc2014-03-25 14:29:40 +0000113//MP_DEFINE_EXCEPTION(SystemExit, BaseException)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000114//MP_DEFINE_EXCEPTION(KeyboardInterrupt, BaseException)
115MP_DEFINE_EXCEPTION(GeneratorExit, BaseException)
116MP_DEFINE_EXCEPTION(Exception, BaseException)
117 MP_DEFINE_EXCEPTION_BASE(Exception)
118 MP_DEFINE_EXCEPTION(StopIteration, Exception)
119 MP_DEFINE_EXCEPTION(ArithmeticError, Exception)
120 MP_DEFINE_EXCEPTION_BASE(ArithmeticError)
Damien Georgec63f9842014-03-27 23:49:06 +0000121 //MP_DEFINE_EXCEPTION(FloatingPointError, ArithmeticError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000122 MP_DEFINE_EXCEPTION(OverflowError, ArithmeticError)
123 MP_DEFINE_EXCEPTION(ZeroDivisionError, ArithmeticError)
124 MP_DEFINE_EXCEPTION(AssertionError, Exception)
125 MP_DEFINE_EXCEPTION(AttributeError, Exception)
Damien Georgec63f9842014-03-27 23:49:06 +0000126 //MP_DEFINE_EXCEPTION(BufferError, Exception)
Damien Georgeffb5cfc2014-03-25 14:29:40 +0000127 //MP_DEFINE_EXCEPTION(EnvironmentError, Exception)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000128 MP_DEFINE_EXCEPTION(EOFError, Exception)
129 MP_DEFINE_EXCEPTION(ImportError, Exception)
130 MP_DEFINE_EXCEPTION(IOError, Exception)
131 MP_DEFINE_EXCEPTION(LookupError, Exception)
132 MP_DEFINE_EXCEPTION_BASE(LookupError)
133 MP_DEFINE_EXCEPTION(IndexError, LookupError)
134 MP_DEFINE_EXCEPTION(KeyError, LookupError)
135 MP_DEFINE_EXCEPTION(MemoryError, Exception)
136 MP_DEFINE_EXCEPTION(NameError, Exception)
137 MP_DEFINE_EXCEPTION_BASE(NameError)
Damien Georgec63f9842014-03-27 23:49:06 +0000138 //MP_DEFINE_EXCEPTION(UnboundLocalError, NameError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000139 MP_DEFINE_EXCEPTION(OSError, Exception)
Damien Georgec9109722014-03-22 23:40:02 +0000140 MP_DEFINE_EXCEPTION_BASE(OSError)
Damien Georgec63f9842014-03-27 23:49:06 +0000141 /*
142 MP_DEFINE_EXCEPTION(BlockingIOError, OSError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000143 MP_DEFINE_EXCEPTION(ChildProcessError, OSError)
144 MP_DEFINE_EXCEPTION(ConnectionError, OSError)
145 MP_DEFINE_EXCEPTION(BrokenPipeError, ConnectionError)
146 MP_DEFINE_EXCEPTION(ConnectionAbortedError, ConnectionError)
147 MP_DEFINE_EXCEPTION(ConnectionRefusedError, ConnectionError)
148 MP_DEFINE_EXCEPTION(ConnectionResetError, ConnectionError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000149 MP_DEFINE_EXCEPTION(InterruptedError, OSError)
150 MP_DEFINE_EXCEPTION(IsADirectoryError, OSError)
151 MP_DEFINE_EXCEPTION(NotADirectoryError, OSError)
152 MP_DEFINE_EXCEPTION(PermissionError, OSError)
153 MP_DEFINE_EXCEPTION(ProcessLookupError, OSError)
Damien Georgec63f9842014-03-27 23:49:06 +0000154 MP_DEFINE_EXCEPTION(TimeoutError, OSError)
Damien Georgec9109722014-03-22 23:40:02 +0000155 MP_DEFINE_EXCEPTION(FileExistsError, OSError)
156 MP_DEFINE_EXCEPTION(FileNotFoundError, OSError)
Damien Georgec63f9842014-03-27 23:49:06 +0000157 MP_DEFINE_EXCEPTION(ReferenceError, Exception)
158 */
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000159 MP_DEFINE_EXCEPTION(RuntimeError, Exception)
160 MP_DEFINE_EXCEPTION_BASE(RuntimeError)
161 MP_DEFINE_EXCEPTION(NotImplementedError, RuntimeError)
162 MP_DEFINE_EXCEPTION(SyntaxError, Exception)
163 MP_DEFINE_EXCEPTION_BASE(SyntaxError)
164 MP_DEFINE_EXCEPTION(IndentationError, SyntaxError)
Damien Georgec63f9842014-03-27 23:49:06 +0000165 /*
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000166 MP_DEFINE_EXCEPTION_BASE(IndentationError)
Damien Georgec63f9842014-03-27 23:49:06 +0000167 MP_DEFINE_EXCEPTION(TabError, IndentationError)
168 */
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000169 MP_DEFINE_EXCEPTION(SystemError, Exception)
170 MP_DEFINE_EXCEPTION(TypeError, Exception)
171 MP_DEFINE_EXCEPTION(ValueError, Exception)
172 //TODO: Implement UnicodeErrors which take arguments
Damien Georgec9109722014-03-22 23:40:02 +0000173 /*
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000174 MP_DEFINE_EXCEPTION(Warning, Exception)
175 MP_DEFINE_EXCEPTION_BASE(Warning)
176 MP_DEFINE_EXCEPTION(DeprecationWarning, Warning)
177 MP_DEFINE_EXCEPTION(PendingDeprecationWarning, Warning)
178 MP_DEFINE_EXCEPTION(RuntimeWarning, Warning)
179 MP_DEFINE_EXCEPTION(SyntaxWarning, Warning)
180 MP_DEFINE_EXCEPTION(UserWarning, Warning)
181 MP_DEFINE_EXCEPTION(FutureWarning, Warning)
182 MP_DEFINE_EXCEPTION(ImportWarning, Warning)
183 MP_DEFINE_EXCEPTION(UnicodeWarning, Warning)
184 MP_DEFINE_EXCEPTION(BytesWarning, Warning)
185 MP_DEFINE_EXCEPTION(ResourceWarning, Warning)
Damien Georgec9109722014-03-22 23:40:02 +0000186 */
Damien Georgec5966122014-02-15 16:10:44 +0000187
188mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type) {
189 return mp_obj_new_exception_msg_varg(exc_type, NULL);
Damiend99b0522013-12-21 18:17:45 +0000190}
191
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200192mp_obj_t mp_obj_new_exception_args(const mp_obj_type_t *exc_type, uint n_args, const mp_obj_t *args) {
193 assert(exc_type->make_new == mp_obj_exception_make_new);
194 return exc_type->make_new((mp_obj_t)exc_type, n_args, 0, args);
195}
196
Damien Georgec5966122014-02-15 16:10:44 +0000197mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, const char *msg) {
198 return mp_obj_new_exception_msg_varg(exc_type, msg);
Damiend99b0522013-12-21 18:17:45 +0000199}
200
Damien Georgec5966122014-02-15 16:10:44 +0000201mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char *fmt, ...) {
202 // check that the given type is an exception type
203 assert(exc_type->make_new == mp_obj_exception_make_new);
204
Damien George6c73ca12014-01-08 18:11:23 +0000205 // make exception object
Damien Georgec5966122014-02-15 16:10:44 +0000206 mp_obj_exception_t *o = m_new_obj_var(mp_obj_exception_t, mp_obj_t, 0);
207 o->base.type = exc_type;
Damien George136b1492014-01-19 12:38:49 +0000208 o->traceback = MP_OBJ_NULL;
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.
247 return rt_binary_op(RT_BINARY_OP_EXCEPTION_MATCH, exc, (mp_obj_t)exc_type) == mp_const_true;
248}
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}