blob: 0efc21d3606924b38c668f9010dcc894dd46a916 [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
Paul Sokolovskyc4d589e2014-03-28 02:37:28 +020020// Instance of GeneratorExit exception - needed by generator.close()
21// This would belong to objgenerator.c, but to keep mp_obj_exception_t
22// definition module-private so far, have it here.
Paul Sokolovskya96d3d02014-03-31 01:10:10 +030023const 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 +020024
Damien Georgec5966122014-02-15 16:10:44 +000025STATIC 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 +000026 mp_obj_exception_t *o = o_in;
Paul Sokolovskya96d3d02014-03-31 01:10:10 +030027 if (kind == PRINT_REPR) {
28 print(env, "%s", qstr_str(o->base.type->name));
29 } else if (kind == PRINT_EXC) {
30 print(env, "%s: ", qstr_str(o->base.type->name));
Damiend99b0522013-12-21 18:17:45 +000031 }
Paul Sokolovskya96d3d02014-03-31 01:10:10 +030032 if (kind == PRINT_STR || kind == PRINT_EXC) {
33 if (o->args.len == 0) {
34 print(env, "");
35 return;
36 } else if (o->args.len == 1) {
37 mp_obj_print_helper(print, env, o->args.items[0], PRINT_STR);
38 return;
39 }
40 }
41 tuple_print(print, env, &o->args, kind);
Damiend99b0522013-12-21 18:17:45 +000042}
43
Damien Georgec5966122014-02-15 16:10:44 +000044STATIC mp_obj_t mp_obj_exception_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
45 mp_obj_type_t *type = type_in;
Damien George20006db2014-01-18 14:10:48 +000046
47 if (n_kw != 0) {
Damien George0ec6bd42014-03-09 16:29:36 +000048 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 +000049 }
50
51 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 +000052 o->base.type = type;
Paul Sokolovsky60a0d3f2014-02-14 21:01:12 +020053 o->traceback = MP_OBJ_NULL;
Damien George07ddab52014-03-29 13:15:08 +000054 o->args.base.type = &mp_type_tuple;
Paul Sokolovskyddf21782014-01-12 23:30:20 +020055 o->args.len = n_args;
Damien George20006db2014-01-18 14:10:48 +000056 memcpy(o->args.items, args, n_args * sizeof(mp_obj_t));
Paul Sokolovskyddf21782014-01-12 23:30:20 +020057 return o;
58}
59
Paul Sokolovskyaf1ae302014-03-26 19:17:20 +020060// Get exception "value" - that is, first argument, or None
61mp_obj_t mp_obj_exception_get_value(mp_obj_t self_in) {
62 mp_obj_exception_t *self = self_in;
63 if (self->args.len == 0) {
64 return mp_const_none;
65 } else {
66 return self->args.items[0];
67 }
68}
69
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +020070STATIC void exception_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
71 mp_obj_exception_t *self = self_in;
72 if (attr == MP_QSTR_args) {
73 dest[0] = &self->args;
74 } else if (self->base.type == &mp_type_StopIteration && attr == MP_QSTR_value) {
Paul Sokolovskyaf1ae302014-03-26 19:17:20 +020075 dest[0] = mp_obj_exception_get_value(self);
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +020076 }
77}
78
Damien Georgec5966122014-02-15 16:10:44 +000079const mp_obj_type_t mp_type_BaseException = {
80 { &mp_type_type },
81 .name = MP_QSTR_BaseException,
82 .print = mp_obj_exception_print,
83 .make_new = mp_obj_exception_make_new,
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +020084 .load_attr = exception_load_attr,
Damiend99b0522013-12-21 18:17:45 +000085};
86
Damien George22a08652014-02-15 21:05:25 +000087#define MP_DEFINE_EXCEPTION_BASE(base_name) \
Damien George07ddab52014-03-29 13:15:08 +000088STATIC 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 +000089
90#define MP_DEFINE_EXCEPTION(exc_name, base_name) \
Damien Georgec5966122014-02-15 16:10:44 +000091const mp_obj_type_t mp_type_ ## exc_name = { \
92 { &mp_type_type }, \
93 .name = MP_QSTR_ ## exc_name, \
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, \
Damien George22a08652014-02-15 21:05:25 +000097 .bases_tuple = (mp_obj_t)&mp_type_ ## base_name ## _base_tuple, \
Damien Georgec5966122014-02-15 16:10:44 +000098};
99
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000100// List of all exceptions, arranged as in the table at:
101// http://docs.python.org/3.3/library/exceptions.html
Damien George22a08652014-02-15 21:05:25 +0000102MP_DEFINE_EXCEPTION_BASE(BaseException)
Damien Georgeffb5cfc2014-03-25 14:29:40 +0000103//MP_DEFINE_EXCEPTION(SystemExit, BaseException)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000104//MP_DEFINE_EXCEPTION(KeyboardInterrupt, BaseException)
105MP_DEFINE_EXCEPTION(GeneratorExit, BaseException)
106MP_DEFINE_EXCEPTION(Exception, BaseException)
107 MP_DEFINE_EXCEPTION_BASE(Exception)
108 MP_DEFINE_EXCEPTION(StopIteration, Exception)
109 MP_DEFINE_EXCEPTION(ArithmeticError, Exception)
110 MP_DEFINE_EXCEPTION_BASE(ArithmeticError)
Damien Georgec63f9842014-03-27 23:49:06 +0000111 //MP_DEFINE_EXCEPTION(FloatingPointError, ArithmeticError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000112 MP_DEFINE_EXCEPTION(OverflowError, ArithmeticError)
113 MP_DEFINE_EXCEPTION(ZeroDivisionError, ArithmeticError)
114 MP_DEFINE_EXCEPTION(AssertionError, Exception)
115 MP_DEFINE_EXCEPTION(AttributeError, Exception)
Damien Georgec63f9842014-03-27 23:49:06 +0000116 //MP_DEFINE_EXCEPTION(BufferError, Exception)
Damien Georgeffb5cfc2014-03-25 14:29:40 +0000117 //MP_DEFINE_EXCEPTION(EnvironmentError, Exception)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000118 MP_DEFINE_EXCEPTION(EOFError, Exception)
119 MP_DEFINE_EXCEPTION(ImportError, Exception)
120 MP_DEFINE_EXCEPTION(IOError, Exception)
121 MP_DEFINE_EXCEPTION(LookupError, Exception)
122 MP_DEFINE_EXCEPTION_BASE(LookupError)
123 MP_DEFINE_EXCEPTION(IndexError, LookupError)
124 MP_DEFINE_EXCEPTION(KeyError, LookupError)
125 MP_DEFINE_EXCEPTION(MemoryError, Exception)
126 MP_DEFINE_EXCEPTION(NameError, Exception)
127 MP_DEFINE_EXCEPTION_BASE(NameError)
Damien Georgec63f9842014-03-27 23:49:06 +0000128 //MP_DEFINE_EXCEPTION(UnboundLocalError, NameError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000129 MP_DEFINE_EXCEPTION(OSError, Exception)
Damien Georgec9109722014-03-22 23:40:02 +0000130 MP_DEFINE_EXCEPTION_BASE(OSError)
Damien Georgec63f9842014-03-27 23:49:06 +0000131 /*
132 MP_DEFINE_EXCEPTION(BlockingIOError, OSError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000133 MP_DEFINE_EXCEPTION(ChildProcessError, OSError)
134 MP_DEFINE_EXCEPTION(ConnectionError, OSError)
135 MP_DEFINE_EXCEPTION(BrokenPipeError, ConnectionError)
136 MP_DEFINE_EXCEPTION(ConnectionAbortedError, ConnectionError)
137 MP_DEFINE_EXCEPTION(ConnectionRefusedError, ConnectionError)
138 MP_DEFINE_EXCEPTION(ConnectionResetError, ConnectionError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000139 MP_DEFINE_EXCEPTION(InterruptedError, OSError)
140 MP_DEFINE_EXCEPTION(IsADirectoryError, OSError)
141 MP_DEFINE_EXCEPTION(NotADirectoryError, OSError)
142 MP_DEFINE_EXCEPTION(PermissionError, OSError)
143 MP_DEFINE_EXCEPTION(ProcessLookupError, OSError)
Damien Georgec63f9842014-03-27 23:49:06 +0000144 MP_DEFINE_EXCEPTION(TimeoutError, OSError)
Damien Georgec9109722014-03-22 23:40:02 +0000145 MP_DEFINE_EXCEPTION(FileExistsError, OSError)
146 MP_DEFINE_EXCEPTION(FileNotFoundError, OSError)
Damien Georgec63f9842014-03-27 23:49:06 +0000147 MP_DEFINE_EXCEPTION(ReferenceError, Exception)
148 */
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000149 MP_DEFINE_EXCEPTION(RuntimeError, Exception)
150 MP_DEFINE_EXCEPTION_BASE(RuntimeError)
151 MP_DEFINE_EXCEPTION(NotImplementedError, RuntimeError)
152 MP_DEFINE_EXCEPTION(SyntaxError, Exception)
153 MP_DEFINE_EXCEPTION_BASE(SyntaxError)
154 MP_DEFINE_EXCEPTION(IndentationError, SyntaxError)
Damien Georgec63f9842014-03-27 23:49:06 +0000155 /*
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000156 MP_DEFINE_EXCEPTION_BASE(IndentationError)
Damien Georgec63f9842014-03-27 23:49:06 +0000157 MP_DEFINE_EXCEPTION(TabError, IndentationError)
158 */
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000159 MP_DEFINE_EXCEPTION(SystemError, Exception)
160 MP_DEFINE_EXCEPTION(TypeError, Exception)
161 MP_DEFINE_EXCEPTION(ValueError, Exception)
162 //TODO: Implement UnicodeErrors which take arguments
Damien Georgec9109722014-03-22 23:40:02 +0000163 /*
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000164 MP_DEFINE_EXCEPTION(Warning, Exception)
165 MP_DEFINE_EXCEPTION_BASE(Warning)
166 MP_DEFINE_EXCEPTION(DeprecationWarning, Warning)
167 MP_DEFINE_EXCEPTION(PendingDeprecationWarning, Warning)
168 MP_DEFINE_EXCEPTION(RuntimeWarning, Warning)
169 MP_DEFINE_EXCEPTION(SyntaxWarning, Warning)
170 MP_DEFINE_EXCEPTION(UserWarning, Warning)
171 MP_DEFINE_EXCEPTION(FutureWarning, Warning)
172 MP_DEFINE_EXCEPTION(ImportWarning, Warning)
173 MP_DEFINE_EXCEPTION(UnicodeWarning, Warning)
174 MP_DEFINE_EXCEPTION(BytesWarning, Warning)
175 MP_DEFINE_EXCEPTION(ResourceWarning, Warning)
Damien Georgec9109722014-03-22 23:40:02 +0000176 */
Damien Georgec5966122014-02-15 16:10:44 +0000177
178mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type) {
Paul Sokolovskya96d3d02014-03-31 01:10:10 +0300179 return mp_obj_new_exception_args(exc_type, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000180}
181
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200182mp_obj_t mp_obj_new_exception_args(const mp_obj_type_t *exc_type, uint n_args, const mp_obj_t *args) {
183 assert(exc_type->make_new == mp_obj_exception_make_new);
184 return exc_type->make_new((mp_obj_t)exc_type, n_args, 0, args);
185}
186
Damien Georgec5966122014-02-15 16:10:44 +0000187mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, const char *msg) {
188 return mp_obj_new_exception_msg_varg(exc_type, msg);
Damiend99b0522013-12-21 18:17:45 +0000189}
190
Damien Georgec5966122014-02-15 16:10:44 +0000191mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char *fmt, ...) {
192 // check that the given type is an exception type
193 assert(exc_type->make_new == mp_obj_exception_make_new);
194
Damien George6c73ca12014-01-08 18:11:23 +0000195 // make exception object
Paul Sokolovskya96d3d02014-03-31 01:10:10 +0300196 mp_obj_exception_t *o = m_new_obj_var(mp_obj_exception_t, mp_obj_t, 1);
Damien Georgec5966122014-02-15 16:10:44 +0000197 o->base.type = exc_type;
Damien George136b1492014-01-19 12:38:49 +0000198 o->traceback = MP_OBJ_NULL;
Paul Sokolovsky24a140a2014-03-30 13:29:33 +0300199 o->args.base.type = &mp_type_tuple;
Paul Sokolovskya96d3d02014-03-31 01:10:10 +0300200 o->args.len = 1;
Damien Georgec5966122014-02-15 16:10:44 +0000201
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200202 if (fmt == NULL) {
Damien Georgec5966122014-02-15 16:10:44 +0000203 // no message
Paul Sokolovskya96d3d02014-03-31 01:10:10 +0300204 assert(0);
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200205 } else {
Paul Sokolovskya96d3d02014-03-31 01:10:10 +0300206 // render exception message and store as .args[0]
207 // TODO: optimize bufferbloat
208 vstr_t *vstr = vstr_new();
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200209 va_list ap;
210 va_start(ap, fmt);
Paul Sokolovskya96d3d02014-03-31 01:10:10 +0300211 vstr_vprintf(vstr, fmt, ap);
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200212 va_end(ap);
Paul Sokolovskya96d3d02014-03-31 01:10:10 +0300213 o->args.items[0] = mp_obj_new_str((byte*)vstr->buf, vstr->len, false);
214 vstr_free(vstr);
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.
Damien Georged17926d2014-03-30 13:35:08 +0100241 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 +0200242}
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}