blob: 71874751b2db1b3da739c8368c8b957fe0e2129d [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
Damien Georgec5966122014-02-15 16:10:44 +000024STATIC 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 +000025 mp_obj_exception_t *o = o_in;
Damien George1ba1fac2014-01-29 18:57:20 +000026 if (o->msg != NULL) {
Damien Georgec5966122014-02-15 16:10:44 +000027 print(env, "%s: %s", qstr_str(o->base.type->name), vstr_str(o->msg));
Paul Sokolovskyddf21782014-01-12 23:30:20 +020028 } else {
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020029 // Yes, that's how CPython has it
Damien Georgec8f78bc2014-02-15 22:55:00 +000030 // 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 +020031 if (kind == PRINT_REPR) {
Damien Georgec5966122014-02-15 16:10:44 +000032 print(env, "%s", qstr_str(o->base.type->name));
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020033 }
34 if (kind == PRINT_STR) {
35 if (o->args.len == 0) {
36 print(env, "");
37 return;
38 } else if (o->args.len == 1) {
39 mp_obj_print_helper(print, env, o->args.items[0], PRINT_STR);
40 return;
41 }
42 }
43 tuple_print(print, env, &o->args, kind);
Damiend99b0522013-12-21 18:17:45 +000044 }
45}
46
Damien Georgec5966122014-02-15 16:10:44 +000047STATIC mp_obj_t mp_obj_exception_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
48 mp_obj_type_t *type = type_in;
Damien George20006db2014-01-18 14:10:48 +000049
50 if (n_kw != 0) {
Damien George0ec6bd42014-03-09 16:29:36 +000051 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 +000052 }
53
54 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 +000055 o->base.type = type;
Paul Sokolovsky60a0d3f2014-02-14 21:01:12 +020056 o->traceback = MP_OBJ_NULL;
Damien George1ba1fac2014-01-29 18:57:20 +000057 o->msg = NULL;
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +020058 o->args.base.type = &tuple_type;
Paul Sokolovskyddf21782014-01-12 23:30:20 +020059 o->args.len = n_args;
Damien George20006db2014-01-18 14:10:48 +000060 memcpy(o->args.items, args, n_args * sizeof(mp_obj_t));
Paul Sokolovskyddf21782014-01-12 23:30:20 +020061 return o;
62}
63
Paul Sokolovskyaf1ae302014-03-26 19:17:20 +020064// Get exception "value" - that is, first argument, or None
65mp_obj_t mp_obj_exception_get_value(mp_obj_t self_in) {
66 mp_obj_exception_t *self = self_in;
67 if (self->args.len == 0) {
68 return mp_const_none;
69 } else {
70 return self->args.items[0];
71 }
72}
73
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +020074STATIC void exception_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
75 mp_obj_exception_t *self = self_in;
76 if (attr == MP_QSTR_args) {
77 dest[0] = &self->args;
78 } else if (self->base.type == &mp_type_StopIteration && attr == MP_QSTR_value) {
Paul Sokolovskyaf1ae302014-03-26 19:17:20 +020079 dest[0] = mp_obj_exception_get_value(self);
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +020080 }
81}
82
Damien Georgec5966122014-02-15 16:10:44 +000083const mp_obj_type_t mp_type_BaseException = {
84 { &mp_type_type },
85 .name = MP_QSTR_BaseException,
86 .print = mp_obj_exception_print,
87 .make_new = mp_obj_exception_make_new,
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +020088 .load_attr = exception_load_attr,
Damiend99b0522013-12-21 18:17:45 +000089};
90
Damien George22a08652014-02-15 21:05:25 +000091#define MP_DEFINE_EXCEPTION_BASE(base_name) \
92STATIC const mp_obj_tuple_t mp_type_ ## base_name ## _base_tuple = {{&tuple_type}, 1, {(mp_obj_t)&mp_type_ ## base_name}};\
93
94#define MP_DEFINE_EXCEPTION(exc_name, base_name) \
Damien Georgec5966122014-02-15 16:10:44 +000095const mp_obj_type_t mp_type_ ## exc_name = { \
96 { &mp_type_type }, \
97 .name = MP_QSTR_ ## exc_name, \
98 .print = mp_obj_exception_print, \
99 .make_new = mp_obj_exception_make_new, \
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +0200100 .load_attr = exception_load_attr, \
Damien George22a08652014-02-15 21:05:25 +0000101 .bases_tuple = (mp_obj_t)&mp_type_ ## base_name ## _base_tuple, \
Damien Georgec5966122014-02-15 16:10:44 +0000102};
103
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000104// List of all exceptions, arranged as in the table at:
105// http://docs.python.org/3.3/library/exceptions.html
Damien George22a08652014-02-15 21:05:25 +0000106MP_DEFINE_EXCEPTION_BASE(BaseException)
Damien Georgeffb5cfc2014-03-25 14:29:40 +0000107//MP_DEFINE_EXCEPTION(SystemExit, BaseException)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000108//MP_DEFINE_EXCEPTION(KeyboardInterrupt, BaseException)
109MP_DEFINE_EXCEPTION(GeneratorExit, BaseException)
110MP_DEFINE_EXCEPTION(Exception, BaseException)
111 MP_DEFINE_EXCEPTION_BASE(Exception)
112 MP_DEFINE_EXCEPTION(StopIteration, Exception)
113 MP_DEFINE_EXCEPTION(ArithmeticError, Exception)
114 MP_DEFINE_EXCEPTION_BASE(ArithmeticError)
Damien Georgec63f9842014-03-27 23:49:06 +0000115 //MP_DEFINE_EXCEPTION(FloatingPointError, ArithmeticError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000116 MP_DEFINE_EXCEPTION(OverflowError, ArithmeticError)
117 MP_DEFINE_EXCEPTION(ZeroDivisionError, ArithmeticError)
118 MP_DEFINE_EXCEPTION(AssertionError, Exception)
119 MP_DEFINE_EXCEPTION(AttributeError, Exception)
Damien Georgec63f9842014-03-27 23:49:06 +0000120 //MP_DEFINE_EXCEPTION(BufferError, Exception)
Damien Georgeffb5cfc2014-03-25 14:29:40 +0000121 //MP_DEFINE_EXCEPTION(EnvironmentError, Exception)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000122 MP_DEFINE_EXCEPTION(EOFError, Exception)
123 MP_DEFINE_EXCEPTION(ImportError, Exception)
124 MP_DEFINE_EXCEPTION(IOError, Exception)
125 MP_DEFINE_EXCEPTION(LookupError, Exception)
126 MP_DEFINE_EXCEPTION_BASE(LookupError)
127 MP_DEFINE_EXCEPTION(IndexError, LookupError)
128 MP_DEFINE_EXCEPTION(KeyError, LookupError)
129 MP_DEFINE_EXCEPTION(MemoryError, Exception)
130 MP_DEFINE_EXCEPTION(NameError, Exception)
131 MP_DEFINE_EXCEPTION_BASE(NameError)
Damien Georgec63f9842014-03-27 23:49:06 +0000132 //MP_DEFINE_EXCEPTION(UnboundLocalError, NameError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000133 MP_DEFINE_EXCEPTION(OSError, Exception)
Damien Georgec9109722014-03-22 23:40:02 +0000134 MP_DEFINE_EXCEPTION_BASE(OSError)
Damien Georgec63f9842014-03-27 23:49:06 +0000135 /*
136 MP_DEFINE_EXCEPTION(BlockingIOError, OSError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000137 MP_DEFINE_EXCEPTION(ChildProcessError, OSError)
138 MP_DEFINE_EXCEPTION(ConnectionError, OSError)
139 MP_DEFINE_EXCEPTION(BrokenPipeError, ConnectionError)
140 MP_DEFINE_EXCEPTION(ConnectionAbortedError, ConnectionError)
141 MP_DEFINE_EXCEPTION(ConnectionRefusedError, ConnectionError)
142 MP_DEFINE_EXCEPTION(ConnectionResetError, ConnectionError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000143 MP_DEFINE_EXCEPTION(InterruptedError, OSError)
144 MP_DEFINE_EXCEPTION(IsADirectoryError, OSError)
145 MP_DEFINE_EXCEPTION(NotADirectoryError, OSError)
146 MP_DEFINE_EXCEPTION(PermissionError, OSError)
147 MP_DEFINE_EXCEPTION(ProcessLookupError, OSError)
Damien Georgec63f9842014-03-27 23:49:06 +0000148 MP_DEFINE_EXCEPTION(TimeoutError, OSError)
Damien Georgec9109722014-03-22 23:40:02 +0000149 MP_DEFINE_EXCEPTION(FileExistsError, OSError)
150 MP_DEFINE_EXCEPTION(FileNotFoundError, OSError)
Damien Georgec63f9842014-03-27 23:49:06 +0000151 MP_DEFINE_EXCEPTION(ReferenceError, Exception)
152 */
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000153 MP_DEFINE_EXCEPTION(RuntimeError, Exception)
154 MP_DEFINE_EXCEPTION_BASE(RuntimeError)
155 MP_DEFINE_EXCEPTION(NotImplementedError, RuntimeError)
156 MP_DEFINE_EXCEPTION(SyntaxError, Exception)
157 MP_DEFINE_EXCEPTION_BASE(SyntaxError)
158 MP_DEFINE_EXCEPTION(IndentationError, SyntaxError)
Damien Georgec63f9842014-03-27 23:49:06 +0000159 /*
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000160 MP_DEFINE_EXCEPTION_BASE(IndentationError)
Damien Georgec63f9842014-03-27 23:49:06 +0000161 MP_DEFINE_EXCEPTION(TabError, IndentationError)
162 */
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000163 MP_DEFINE_EXCEPTION(SystemError, Exception)
164 MP_DEFINE_EXCEPTION(TypeError, Exception)
165 MP_DEFINE_EXCEPTION(ValueError, Exception)
166 //TODO: Implement UnicodeErrors which take arguments
Damien Georgec9109722014-03-22 23:40:02 +0000167 /*
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000168 MP_DEFINE_EXCEPTION(Warning, Exception)
169 MP_DEFINE_EXCEPTION_BASE(Warning)
170 MP_DEFINE_EXCEPTION(DeprecationWarning, Warning)
171 MP_DEFINE_EXCEPTION(PendingDeprecationWarning, Warning)
172 MP_DEFINE_EXCEPTION(RuntimeWarning, Warning)
173 MP_DEFINE_EXCEPTION(SyntaxWarning, Warning)
174 MP_DEFINE_EXCEPTION(UserWarning, Warning)
175 MP_DEFINE_EXCEPTION(FutureWarning, Warning)
176 MP_DEFINE_EXCEPTION(ImportWarning, Warning)
177 MP_DEFINE_EXCEPTION(UnicodeWarning, Warning)
178 MP_DEFINE_EXCEPTION(BytesWarning, Warning)
179 MP_DEFINE_EXCEPTION(ResourceWarning, Warning)
Damien Georgec9109722014-03-22 23:40:02 +0000180 */
Damien Georgec5966122014-02-15 16:10:44 +0000181
182mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type) {
183 return mp_obj_new_exception_msg_varg(exc_type, NULL);
Damiend99b0522013-12-21 18:17:45 +0000184}
185
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200186mp_obj_t mp_obj_new_exception_args(const mp_obj_type_t *exc_type, uint n_args, const mp_obj_t *args) {
187 assert(exc_type->make_new == mp_obj_exception_make_new);
188 return exc_type->make_new((mp_obj_t)exc_type, n_args, 0, args);
189}
190
Damien Georgec5966122014-02-15 16:10:44 +0000191mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, const char *msg) {
192 return mp_obj_new_exception_msg_varg(exc_type, msg);
Damiend99b0522013-12-21 18:17:45 +0000193}
194
Damien Georgec5966122014-02-15 16:10:44 +0000195mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char *fmt, ...) {
196 // check that the given type is an exception type
197 assert(exc_type->make_new == mp_obj_exception_make_new);
198
Damien George6c73ca12014-01-08 18:11:23 +0000199 // make exception object
Damien Georgec5966122014-02-15 16:10:44 +0000200 mp_obj_exception_t *o = m_new_obj_var(mp_obj_exception_t, mp_obj_t, 0);
201 o->base.type = exc_type;
Damien George136b1492014-01-19 12:38:49 +0000202 o->traceback = MP_OBJ_NULL;
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200203 o->args.len = 0;
Damien Georgec5966122014-02-15 16:10:44 +0000204
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200205 if (fmt == NULL) {
Damien Georgec5966122014-02-15 16:10:44 +0000206 // no message
Damien George1ba1fac2014-01-29 18:57:20 +0000207 o->msg = NULL;
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200208 } else {
209 // render exception message
Damien George1ba1fac2014-01-29 18:57:20 +0000210 o->msg = vstr_new();
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200211 va_list ap;
212 va_start(ap, fmt);
Damien George1ba1fac2014-01-29 18:57:20 +0000213 vstr_vprintf(o->msg, fmt, ap);
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200214 va_end(ap);
Damien George6c73ca12014-01-08 18:11:23 +0000215 }
Damien George6c73ca12014-01-08 18:11:23 +0000216
217 return o;
218}
219
Damien Georgec5966122014-02-15 16:10:44 +0000220// return true if the given object is an exception type
Damien Georgec5966122014-02-15 16:10:44 +0000221bool mp_obj_is_exception_type(mp_obj_t self_in) {
222 if (MP_OBJ_IS_TYPE(self_in, &mp_type_type)) {
Damien George9e6e9352014-03-26 18:37:06 +0000223 // optimisation when self_in is a builtin exception
Damien Georgec5966122014-02-15 16:10:44 +0000224 mp_obj_type_t *self = self_in;
Damien George9e6e9352014-03-26 18:37:06 +0000225 if (self->make_new == mp_obj_exception_make_new) {
226 return true;
227 }
Damien Georgec5966122014-02-15 16:10:44 +0000228 }
Damien George9e6e9352014-03-26 18:37:06 +0000229 return mp_obj_is_subclass_fast(self_in, &mp_type_BaseException);
Damien Georgec5966122014-02-15 16:10:44 +0000230}
231
232// return true if the given object is an instance of an exception type
Damien Georgec5966122014-02-15 16:10:44 +0000233bool mp_obj_is_exception_instance(mp_obj_t self_in) {
Damien George9e6e9352014-03-26 18:37:06 +0000234 return mp_obj_is_exception_type(mp_obj_get_type(self_in));
Damien Georgec5966122014-02-15 16:10:44 +0000235}
236
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200237// return true if exception (type or instance) is a subclass of given
238// exception type.
239bool mp_obj_exception_match(mp_obj_t exc, const mp_obj_type_t *exc_type) {
240 // TODO: move implementation from RT_BINARY_OP_EXCEPTION_MATCH here.
241 return rt_binary_op(RT_BINARY_OP_EXCEPTION_MATCH, exc, (mp_obj_t)exc_type) == mp_const_true;
242}
243
Damien Georgec5966122014-02-15 16:10:44 +0000244void mp_obj_exception_clear_traceback(mp_obj_t self_in) {
245 // make sure self_in is an exception instance
Damien George9e6e9352014-03-26 18:37:06 +0000246 // TODO add traceback information to user-defined exceptions (need proper builtin subclassing for that)
247 if (mp_obj_get_type(self_in)->make_new == mp_obj_exception_make_new) {
248 mp_obj_exception_t *self = self_in;
Damien Georgec5966122014-02-15 16:10:44 +0000249
Damien George9e6e9352014-03-26 18:37:06 +0000250 // just set the traceback to the null object
251 // we don't want to call any memory management functions here
252 self->traceback = MP_OBJ_NULL;
253 }
Damienb86e3f92013-12-29 17:17:43 +0000254}
Damien George08335002014-01-18 23:24:36 +0000255
Damien George136b1492014-01-19 12:38:49 +0000256void 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 +0000257 // make sure self_in is an exception instance
Damien George9e6e9352014-03-26 18:37:06 +0000258 // TODO add traceback information to user-defined exceptions (need proper builtin subclassing for that)
259 if (mp_obj_get_type(self_in)->make_new == mp_obj_exception_make_new) {
260 mp_obj_exception_t *self = self_in;
Damien Georgec5966122014-02-15 16:10:44 +0000261
Damien George9e6e9352014-03-26 18:37:06 +0000262 // for traceback, we are just using the list object for convenience, it's not really a list of Python objects
263 if (self->traceback == MP_OBJ_NULL) {
264 self->traceback = mp_obj_new_list(0, NULL);
265 }
266 mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)file);
267 mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)line);
268 mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)block);
Damien George08335002014-01-18 23:24:36 +0000269 }
Damien George08335002014-01-18 23:24:36 +0000270}
271
Damien George136b1492014-01-19 12:38:49 +0000272void 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 +0000273 // make sure self_in is an exception instance
274 assert(mp_obj_get_type(self_in)->make_new == mp_obj_exception_make_new);
Damien George08335002014-01-18 23:24:36 +0000275 mp_obj_exception_t *self = self_in;
Damien Georgec5966122014-02-15 16:10:44 +0000276
Damien George136b1492014-01-19 12:38:49 +0000277 if (self->traceback == MP_OBJ_NULL) {
278 *n = 0;
279 *values = NULL;
280 } else {
281 uint n2;
282 mp_obj_list_get(self->traceback, &n2, (mp_obj_t**)values);
283 *n = n2;
284 }
Damien George08335002014-01-18 23:24:36 +0000285}