blob: 11651025f06fd30a26f8f63f236c5f42035cc67b [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
Paul Sokolovskyc4d589e2014-03-28 02:37:28 +020023// Instance of GeneratorExit exception - needed by generator.close()
24// This would belong to objgenerator.c, but to keep mp_obj_exception_t
25// definition module-private so far, have it here.
Paul Sokolovskya96d3d02014-03-31 01:10:10 +030026const 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 +020027
Damien Georgec5966122014-02-15 16:10:44 +000028STATIC 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 +000029 mp_obj_exception_t *o = o_in;
Paul Sokolovskya96d3d02014-03-31 01:10:10 +030030 if (kind == PRINT_REPR) {
31 print(env, "%s", qstr_str(o->base.type->name));
32 } else if (kind == PRINT_EXC) {
33 print(env, "%s: ", qstr_str(o->base.type->name));
Damiend99b0522013-12-21 18:17:45 +000034 }
Paul Sokolovskya96d3d02014-03-31 01:10:10 +030035 if (kind == PRINT_STR || kind == PRINT_EXC) {
36 if (o->args.len == 0) {
37 print(env, "");
38 return;
39 } else if (o->args.len == 1) {
40 mp_obj_print_helper(print, env, o->args.items[0], PRINT_STR);
41 return;
42 }
43 }
44 tuple_print(print, env, &o->args, kind);
Damiend99b0522013-12-21 18:17:45 +000045}
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 George07ddab52014-03-29 13:15:08 +000057 o->args.base.type = &mp_type_tuple;
Paul Sokolovskyddf21782014-01-12 23:30:20 +020058 o->args.len = n_args;
Damien George20006db2014-01-18 14:10:48 +000059 memcpy(o->args.items, args, n_args * sizeof(mp_obj_t));
Paul Sokolovskyddf21782014-01-12 23:30:20 +020060 return o;
61}
62
Paul Sokolovskyaf1ae302014-03-26 19:17:20 +020063// Get exception "value" - that is, first argument, or None
64mp_obj_t mp_obj_exception_get_value(mp_obj_t self_in) {
65 mp_obj_exception_t *self = self_in;
66 if (self->args.len == 0) {
67 return mp_const_none;
68 } else {
69 return self->args.items[0];
70 }
71}
72
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +020073STATIC void exception_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
74 mp_obj_exception_t *self = self_in;
75 if (attr == MP_QSTR_args) {
76 dest[0] = &self->args;
77 } else if (self->base.type == &mp_type_StopIteration && attr == MP_QSTR_value) {
Paul Sokolovskyaf1ae302014-03-26 19:17:20 +020078 dest[0] = mp_obj_exception_get_value(self);
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +020079 }
80}
81
Damien Georgec5966122014-02-15 16:10:44 +000082const mp_obj_type_t mp_type_BaseException = {
83 { &mp_type_type },
84 .name = MP_QSTR_BaseException,
85 .print = mp_obj_exception_print,
86 .make_new = mp_obj_exception_make_new,
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +020087 .load_attr = exception_load_attr,
Damiend99b0522013-12-21 18:17:45 +000088};
89
Damien George22a08652014-02-15 21:05:25 +000090#define MP_DEFINE_EXCEPTION_BASE(base_name) \
Damien George07ddab52014-03-29 13:15:08 +000091STATIC 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 +000092
93#define MP_DEFINE_EXCEPTION(exc_name, base_name) \
Damien Georgec5966122014-02-15 16:10:44 +000094const mp_obj_type_t mp_type_ ## exc_name = { \
95 { &mp_type_type }, \
96 .name = MP_QSTR_ ## exc_name, \
97 .print = mp_obj_exception_print, \
98 .make_new = mp_obj_exception_make_new, \
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +020099 .load_attr = exception_load_attr, \
Damien George22a08652014-02-15 21:05:25 +0000100 .bases_tuple = (mp_obj_t)&mp_type_ ## base_name ## _base_tuple, \
Damien Georgec5966122014-02-15 16:10:44 +0000101};
102
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000103// List of all exceptions, arranged as in the table at:
104// http://docs.python.org/3.3/library/exceptions.html
Damien George22a08652014-02-15 21:05:25 +0000105MP_DEFINE_EXCEPTION_BASE(BaseException)
Damien Georgeffb5cfc2014-03-25 14:29:40 +0000106//MP_DEFINE_EXCEPTION(SystemExit, BaseException)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000107//MP_DEFINE_EXCEPTION(KeyboardInterrupt, BaseException)
108MP_DEFINE_EXCEPTION(GeneratorExit, BaseException)
109MP_DEFINE_EXCEPTION(Exception, BaseException)
110 MP_DEFINE_EXCEPTION_BASE(Exception)
111 MP_DEFINE_EXCEPTION(StopIteration, Exception)
112 MP_DEFINE_EXCEPTION(ArithmeticError, Exception)
113 MP_DEFINE_EXCEPTION_BASE(ArithmeticError)
Damien Georgec63f9842014-03-27 23:49:06 +0000114 //MP_DEFINE_EXCEPTION(FloatingPointError, ArithmeticError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000115 MP_DEFINE_EXCEPTION(OverflowError, ArithmeticError)
116 MP_DEFINE_EXCEPTION(ZeroDivisionError, ArithmeticError)
117 MP_DEFINE_EXCEPTION(AssertionError, Exception)
118 MP_DEFINE_EXCEPTION(AttributeError, Exception)
Damien Georgec63f9842014-03-27 23:49:06 +0000119 //MP_DEFINE_EXCEPTION(BufferError, Exception)
Damien Georgeffb5cfc2014-03-25 14:29:40 +0000120 //MP_DEFINE_EXCEPTION(EnvironmentError, Exception)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000121 MP_DEFINE_EXCEPTION(EOFError, Exception)
122 MP_DEFINE_EXCEPTION(ImportError, Exception)
123 MP_DEFINE_EXCEPTION(IOError, Exception)
124 MP_DEFINE_EXCEPTION(LookupError, Exception)
125 MP_DEFINE_EXCEPTION_BASE(LookupError)
126 MP_DEFINE_EXCEPTION(IndexError, LookupError)
127 MP_DEFINE_EXCEPTION(KeyError, LookupError)
128 MP_DEFINE_EXCEPTION(MemoryError, Exception)
129 MP_DEFINE_EXCEPTION(NameError, Exception)
130 MP_DEFINE_EXCEPTION_BASE(NameError)
Damien Georgec63f9842014-03-27 23:49:06 +0000131 //MP_DEFINE_EXCEPTION(UnboundLocalError, NameError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000132 MP_DEFINE_EXCEPTION(OSError, Exception)
Damien Georgec9109722014-03-22 23:40:02 +0000133 MP_DEFINE_EXCEPTION_BASE(OSError)
Damien Georgec63f9842014-03-27 23:49:06 +0000134 /*
135 MP_DEFINE_EXCEPTION(BlockingIOError, OSError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000136 MP_DEFINE_EXCEPTION(ChildProcessError, OSError)
137 MP_DEFINE_EXCEPTION(ConnectionError, OSError)
138 MP_DEFINE_EXCEPTION(BrokenPipeError, ConnectionError)
139 MP_DEFINE_EXCEPTION(ConnectionAbortedError, ConnectionError)
140 MP_DEFINE_EXCEPTION(ConnectionRefusedError, ConnectionError)
141 MP_DEFINE_EXCEPTION(ConnectionResetError, ConnectionError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000142 MP_DEFINE_EXCEPTION(InterruptedError, OSError)
143 MP_DEFINE_EXCEPTION(IsADirectoryError, OSError)
144 MP_DEFINE_EXCEPTION(NotADirectoryError, OSError)
145 MP_DEFINE_EXCEPTION(PermissionError, OSError)
146 MP_DEFINE_EXCEPTION(ProcessLookupError, OSError)
Damien Georgec63f9842014-03-27 23:49:06 +0000147 MP_DEFINE_EXCEPTION(TimeoutError, OSError)
Damien Georgec9109722014-03-22 23:40:02 +0000148 MP_DEFINE_EXCEPTION(FileExistsError, OSError)
149 MP_DEFINE_EXCEPTION(FileNotFoundError, OSError)
Damien Georgec63f9842014-03-27 23:49:06 +0000150 MP_DEFINE_EXCEPTION(ReferenceError, Exception)
151 */
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000152 MP_DEFINE_EXCEPTION(RuntimeError, Exception)
153 MP_DEFINE_EXCEPTION_BASE(RuntimeError)
154 MP_DEFINE_EXCEPTION(NotImplementedError, RuntimeError)
155 MP_DEFINE_EXCEPTION(SyntaxError, Exception)
156 MP_DEFINE_EXCEPTION_BASE(SyntaxError)
157 MP_DEFINE_EXCEPTION(IndentationError, SyntaxError)
Damien Georgec63f9842014-03-27 23:49:06 +0000158 /*
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000159 MP_DEFINE_EXCEPTION_BASE(IndentationError)
Damien Georgec63f9842014-03-27 23:49:06 +0000160 MP_DEFINE_EXCEPTION(TabError, IndentationError)
161 */
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000162 MP_DEFINE_EXCEPTION(SystemError, Exception)
163 MP_DEFINE_EXCEPTION(TypeError, Exception)
164 MP_DEFINE_EXCEPTION(ValueError, Exception)
165 //TODO: Implement UnicodeErrors which take arguments
Damien Georgec9109722014-03-22 23:40:02 +0000166 /*
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000167 MP_DEFINE_EXCEPTION(Warning, Exception)
168 MP_DEFINE_EXCEPTION_BASE(Warning)
169 MP_DEFINE_EXCEPTION(DeprecationWarning, Warning)
170 MP_DEFINE_EXCEPTION(PendingDeprecationWarning, Warning)
171 MP_DEFINE_EXCEPTION(RuntimeWarning, Warning)
172 MP_DEFINE_EXCEPTION(SyntaxWarning, Warning)
173 MP_DEFINE_EXCEPTION(UserWarning, Warning)
174 MP_DEFINE_EXCEPTION(FutureWarning, Warning)
175 MP_DEFINE_EXCEPTION(ImportWarning, Warning)
176 MP_DEFINE_EXCEPTION(UnicodeWarning, Warning)
177 MP_DEFINE_EXCEPTION(BytesWarning, Warning)
178 MP_DEFINE_EXCEPTION(ResourceWarning, Warning)
Damien Georgec9109722014-03-22 23:40:02 +0000179 */
Damien Georgec5966122014-02-15 16:10:44 +0000180
181mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type) {
Paul Sokolovskya96d3d02014-03-31 01:10:10 +0300182 return mp_obj_new_exception_args(exc_type, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000183}
184
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200185mp_obj_t mp_obj_new_exception_args(const mp_obj_type_t *exc_type, uint n_args, const mp_obj_t *args) {
186 assert(exc_type->make_new == mp_obj_exception_make_new);
187 return exc_type->make_new((mp_obj_t)exc_type, n_args, 0, args);
188}
189
Damien Georgec5966122014-02-15 16:10:44 +0000190mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, const char *msg) {
191 return mp_obj_new_exception_msg_varg(exc_type, msg);
Damiend99b0522013-12-21 18:17:45 +0000192}
193
Damien Georgec5966122014-02-15 16:10:44 +0000194mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char *fmt, ...) {
195 // check that the given type is an exception type
196 assert(exc_type->make_new == mp_obj_exception_make_new);
197
Damien George6c73ca12014-01-08 18:11:23 +0000198 // make exception object
Paul Sokolovskya96d3d02014-03-31 01:10:10 +0300199 mp_obj_exception_t *o = m_new_obj_var(mp_obj_exception_t, mp_obj_t, 1);
Damien Georgec5966122014-02-15 16:10:44 +0000200 o->base.type = exc_type;
Damien George136b1492014-01-19 12:38:49 +0000201 o->traceback = MP_OBJ_NULL;
Paul Sokolovsky24a140a2014-03-30 13:29:33 +0300202 o->args.base.type = &mp_type_tuple;
Paul Sokolovskya96d3d02014-03-31 01:10:10 +0300203 o->args.len = 1;
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
Paul Sokolovskya96d3d02014-03-31 01:10:10 +0300207 assert(0);
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200208 } else {
Paul Sokolovskya96d3d02014-03-31 01:10:10 +0300209 // render exception message and store as .args[0]
210 // TODO: optimize bufferbloat
211 vstr_t *vstr = vstr_new();
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200212 va_list ap;
213 va_start(ap, fmt);
Paul Sokolovskya96d3d02014-03-31 01:10:10 +0300214 vstr_vprintf(vstr, fmt, ap);
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200215 va_end(ap);
Paul Sokolovskya96d3d02014-03-31 01:10:10 +0300216 o->args.items[0] = mp_obj_new_str((byte*)vstr->buf, vstr->len, false);
217 vstr_free(vstr);
Damien George6c73ca12014-01-08 18:11:23 +0000218 }
Damien George6c73ca12014-01-08 18:11:23 +0000219
220 return o;
221}
222
Damien Georgec5966122014-02-15 16:10:44 +0000223// return true if the given object is an exception type
Damien Georgec5966122014-02-15 16:10:44 +0000224bool mp_obj_is_exception_type(mp_obj_t self_in) {
225 if (MP_OBJ_IS_TYPE(self_in, &mp_type_type)) {
Damien George9e6e9352014-03-26 18:37:06 +0000226 // optimisation when self_in is a builtin exception
Damien Georgec5966122014-02-15 16:10:44 +0000227 mp_obj_type_t *self = self_in;
Damien George9e6e9352014-03-26 18:37:06 +0000228 if (self->make_new == mp_obj_exception_make_new) {
229 return true;
230 }
Damien Georgec5966122014-02-15 16:10:44 +0000231 }
Damien George9e6e9352014-03-26 18:37:06 +0000232 return mp_obj_is_subclass_fast(self_in, &mp_type_BaseException);
Damien Georgec5966122014-02-15 16:10:44 +0000233}
234
235// return true if the given object is an instance of an exception type
Damien Georgec5966122014-02-15 16:10:44 +0000236bool mp_obj_is_exception_instance(mp_obj_t self_in) {
Damien George9e6e9352014-03-26 18:37:06 +0000237 return mp_obj_is_exception_type(mp_obj_get_type(self_in));
Damien Georgec5966122014-02-15 16:10:44 +0000238}
239
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200240// return true if exception (type or instance) is a subclass of given
241// exception type.
242bool mp_obj_exception_match(mp_obj_t exc, const mp_obj_type_t *exc_type) {
243 // TODO: move implementation from RT_BINARY_OP_EXCEPTION_MATCH here.
Damien Georged17926d2014-03-30 13:35:08 +0100244 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 +0200245}
246
Damien Georgec5966122014-02-15 16:10:44 +0000247void mp_obj_exception_clear_traceback(mp_obj_t self_in) {
248 // make sure self_in is an exception instance
Damien George9e6e9352014-03-26 18:37:06 +0000249 // TODO add traceback information to user-defined exceptions (need proper builtin subclassing for that)
250 if (mp_obj_get_type(self_in)->make_new == mp_obj_exception_make_new) {
251 mp_obj_exception_t *self = self_in;
Damien Georgec5966122014-02-15 16:10:44 +0000252
Damien George9e6e9352014-03-26 18:37:06 +0000253 // just set the traceback to the null object
254 // we don't want to call any memory management functions here
255 self->traceback = MP_OBJ_NULL;
256 }
Damienb86e3f92013-12-29 17:17:43 +0000257}
Damien George08335002014-01-18 23:24:36 +0000258
Damien George136b1492014-01-19 12:38:49 +0000259void 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 +0000260 // make sure self_in is an exception instance
Damien George9e6e9352014-03-26 18:37:06 +0000261 // TODO add traceback information to user-defined exceptions (need proper builtin subclassing for that)
262 if (mp_obj_get_type(self_in)->make_new == mp_obj_exception_make_new) {
263 mp_obj_exception_t *self = self_in;
Damien Georgec5966122014-02-15 16:10:44 +0000264
Damien George9e6e9352014-03-26 18:37:06 +0000265 // for traceback, we are just using the list object for convenience, it's not really a list of Python objects
266 if (self->traceback == MP_OBJ_NULL) {
267 self->traceback = mp_obj_new_list(0, NULL);
268 }
269 mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)file);
270 mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)line);
271 mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)block);
Damien George08335002014-01-18 23:24:36 +0000272 }
Damien George08335002014-01-18 23:24:36 +0000273}
274
Damien George136b1492014-01-19 12:38:49 +0000275void 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 +0000276 // make sure self_in is an exception instance
277 assert(mp_obj_get_type(self_in)->make_new == mp_obj_exception_make_new);
Damien George08335002014-01-18 23:24:36 +0000278 mp_obj_exception_t *self = self_in;
Damien Georgec5966122014-02-15 16:10:44 +0000279
Damien George136b1492014-01-19 12:38:49 +0000280 if (self->traceback == MP_OBJ_NULL) {
281 *n = 0;
282 *values = NULL;
283 } else {
284 uint n2;
285 mp_obj_list_get(self->traceback, &n2, (mp_obj_t**)values);
286 *n = n2;
287 }
Damien George08335002014-01-18 23:24:36 +0000288}