blob: 79b85b28b71012342487748950a6c5a0d8c84d9f [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"
Damiend99b0522013-12-21 18:17:45 +000011
Paul Sokolovskyddf21782014-01-12 23:30:20 +020012// This is unified class for C-level and Python-level exceptions
Paul Sokolovsky027594e2014-01-31 17:13:51 +020013// Python-level exceptions have empty ->msg and all arguments are in
14// args tuple. C-level exceptions likely have ->msg set, and args is empty.
Damiend99b0522013-12-21 18:17:45 +000015typedef struct mp_obj_exception_t {
16 mp_obj_base_t base;
Damien George136b1492014-01-19 12:38:49 +000017 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 +000018 vstr_t *msg;
Paul Sokolovskyddf21782014-01-12 23:30:20 +020019 mp_obj_tuple_t args;
Damiend99b0522013-12-21 18:17:45 +000020} mp_obj_exception_t;
21
Damien Georgec5966122014-02-15 16:10:44 +000022STATIC 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 +000023 mp_obj_exception_t *o = o_in;
Damien George1ba1fac2014-01-29 18:57:20 +000024 if (o->msg != NULL) {
Damien Georgec5966122014-02-15 16:10:44 +000025 print(env, "%s: %s", qstr_str(o->base.type->name), vstr_str(o->msg));
Paul Sokolovskyddf21782014-01-12 23:30:20 +020026 } else {
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020027 // Yes, that's how CPython has it
Damien Georgec8f78bc2014-02-15 22:55:00 +000028 // 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 +020029 if (kind == PRINT_REPR) {
Damien Georgec5966122014-02-15 16:10:44 +000030 print(env, "%s", qstr_str(o->base.type->name));
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020031 }
32 if (kind == PRINT_STR) {
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}
44
Damien Georgec5966122014-02-15 16:10:44 +000045STATIC mp_obj_t mp_obj_exception_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
46 mp_obj_type_t *type = type_in;
Damien George20006db2014-01-18 14:10:48 +000047
48 if (n_kw != 0) {
Damien George0ec6bd42014-03-09 16:29:36 +000049 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 +000050 }
51
52 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 +000053 o->base.type = type;
Paul Sokolovsky60a0d3f2014-02-14 21:01:12 +020054 o->traceback = MP_OBJ_NULL;
Damien George1ba1fac2014-01-29 18:57:20 +000055 o->msg = NULL;
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +020056 o->args.base.type = &tuple_type;
Paul Sokolovskyddf21782014-01-12 23:30:20 +020057 o->args.len = n_args;
Damien George20006db2014-01-18 14:10:48 +000058 memcpy(o->args.items, args, n_args * sizeof(mp_obj_t));
Paul Sokolovskyddf21782014-01-12 23:30:20 +020059 return o;
60}
61
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +020062STATIC void exception_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
63 mp_obj_exception_t *self = self_in;
64 if (attr == MP_QSTR_args) {
65 dest[0] = &self->args;
66 } else if (self->base.type == &mp_type_StopIteration && attr == MP_QSTR_value) {
67 if (self->args.len == 0) {
68 dest[0] = mp_const_none;
69 } else {
70 dest[0] = self->args.items[0];
71 }
72 }
73}
74
Damien Georgec5966122014-02-15 16:10:44 +000075const mp_obj_type_t mp_type_BaseException = {
76 { &mp_type_type },
77 .name = MP_QSTR_BaseException,
78 .print = mp_obj_exception_print,
79 .make_new = mp_obj_exception_make_new,
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +020080 .load_attr = exception_load_attr,
Damiend99b0522013-12-21 18:17:45 +000081};
82
Damien George22a08652014-02-15 21:05:25 +000083#define MP_DEFINE_EXCEPTION_BASE(base_name) \
84STATIC const mp_obj_tuple_t mp_type_ ## base_name ## _base_tuple = {{&tuple_type}, 1, {(mp_obj_t)&mp_type_ ## base_name}};\
85
86#define MP_DEFINE_EXCEPTION(exc_name, base_name) \
Damien Georgec5966122014-02-15 16:10:44 +000087const mp_obj_type_t mp_type_ ## exc_name = { \
88 { &mp_type_type }, \
89 .name = MP_QSTR_ ## exc_name, \
90 .print = mp_obj_exception_print, \
91 .make_new = mp_obj_exception_make_new, \
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +020092 .load_attr = exception_load_attr, \
Damien George22a08652014-02-15 21:05:25 +000093 .bases_tuple = (mp_obj_t)&mp_type_ ## base_name ## _base_tuple, \
Damien Georgec5966122014-02-15 16:10:44 +000094};
95
Rachel Dowdall721c55d2014-03-22 15:28:16 +000096// List of all exceptions, arranged as in the table at:
97// http://docs.python.org/3.3/library/exceptions.html
Damien George22a08652014-02-15 21:05:25 +000098MP_DEFINE_EXCEPTION_BASE(BaseException)
Damien Georgeffb5cfc2014-03-25 14:29:40 +000099//MP_DEFINE_EXCEPTION(SystemExit, BaseException)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000100//MP_DEFINE_EXCEPTION(KeyboardInterrupt, BaseException)
101MP_DEFINE_EXCEPTION(GeneratorExit, BaseException)
102MP_DEFINE_EXCEPTION(Exception, BaseException)
103 MP_DEFINE_EXCEPTION_BASE(Exception)
104 MP_DEFINE_EXCEPTION(StopIteration, Exception)
105 MP_DEFINE_EXCEPTION(ArithmeticError, Exception)
106 MP_DEFINE_EXCEPTION_BASE(ArithmeticError)
107 MP_DEFINE_EXCEPTION(FloatingPointError, ArithmeticError)
108 MP_DEFINE_EXCEPTION(OverflowError, ArithmeticError)
109 MP_DEFINE_EXCEPTION(ZeroDivisionError, ArithmeticError)
110 MP_DEFINE_EXCEPTION(AssertionError, Exception)
111 MP_DEFINE_EXCEPTION(AttributeError, Exception)
112 MP_DEFINE_EXCEPTION(BufferError, Exception)
Damien Georgeffb5cfc2014-03-25 14:29:40 +0000113 //MP_DEFINE_EXCEPTION(EnvironmentError, Exception)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000114 MP_DEFINE_EXCEPTION(EOFError, Exception)
115 MP_DEFINE_EXCEPTION(ImportError, Exception)
116 MP_DEFINE_EXCEPTION(IOError, Exception)
117 MP_DEFINE_EXCEPTION(LookupError, Exception)
118 MP_DEFINE_EXCEPTION_BASE(LookupError)
119 MP_DEFINE_EXCEPTION(IndexError, LookupError)
120 MP_DEFINE_EXCEPTION(KeyError, LookupError)
121 MP_DEFINE_EXCEPTION(MemoryError, Exception)
122 MP_DEFINE_EXCEPTION(NameError, Exception)
123 MP_DEFINE_EXCEPTION_BASE(NameError)
124 MP_DEFINE_EXCEPTION(UnboundLocalError, NameError)
125 MP_DEFINE_EXCEPTION(OSError, Exception)
Damien Georgec9109722014-03-22 23:40:02 +0000126 MP_DEFINE_EXCEPTION_BASE(OSError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000127 // Probably don't need these
Damien Georgec9109722014-03-22 23:40:02 +0000128 /*MP_DEFINE_EXCEPTION(BlockingIOError, OSError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000129 MP_DEFINE_EXCEPTION(ChildProcessError, OSError)
130 MP_DEFINE_EXCEPTION(ConnectionError, OSError)
131 MP_DEFINE_EXCEPTION(BrokenPipeError, ConnectionError)
132 MP_DEFINE_EXCEPTION(ConnectionAbortedError, ConnectionError)
133 MP_DEFINE_EXCEPTION(ConnectionRefusedError, ConnectionError)
134 MP_DEFINE_EXCEPTION(ConnectionResetError, ConnectionError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000135 MP_DEFINE_EXCEPTION(InterruptedError, OSError)
136 MP_DEFINE_EXCEPTION(IsADirectoryError, OSError)
137 MP_DEFINE_EXCEPTION(NotADirectoryError, OSError)
138 MP_DEFINE_EXCEPTION(PermissionError, OSError)
139 MP_DEFINE_EXCEPTION(ProcessLookupError, OSError)
140 MP_DEFINE_EXCEPTION(TimeoutError, OSError)*/
Damien Georgec9109722014-03-22 23:40:02 +0000141 MP_DEFINE_EXCEPTION(FileExistsError, OSError)
142 MP_DEFINE_EXCEPTION(FileNotFoundError, OSError)
Damien Georgeffb5cfc2014-03-25 14:29:40 +0000143 //MP_DEFINE_EXCEPTION(ReferenceError, Exception)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000144 MP_DEFINE_EXCEPTION(RuntimeError, Exception)
145 MP_DEFINE_EXCEPTION_BASE(RuntimeError)
146 MP_DEFINE_EXCEPTION(NotImplementedError, RuntimeError)
147 MP_DEFINE_EXCEPTION(SyntaxError, Exception)
148 MP_DEFINE_EXCEPTION_BASE(SyntaxError)
149 MP_DEFINE_EXCEPTION(IndentationError, SyntaxError)
150 MP_DEFINE_EXCEPTION_BASE(IndentationError)
Damien Georgeffb5cfc2014-03-25 14:29:40 +0000151 //MP_DEFINE_EXCEPTION(TabError, IndentationError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000152 MP_DEFINE_EXCEPTION(SystemError, Exception)
153 MP_DEFINE_EXCEPTION(TypeError, Exception)
154 MP_DEFINE_EXCEPTION(ValueError, Exception)
155 //TODO: Implement UnicodeErrors which take arguments
Damien Georgec9109722014-03-22 23:40:02 +0000156 /*
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000157 MP_DEFINE_EXCEPTION(Warning, Exception)
158 MP_DEFINE_EXCEPTION_BASE(Warning)
159 MP_DEFINE_EXCEPTION(DeprecationWarning, Warning)
160 MP_DEFINE_EXCEPTION(PendingDeprecationWarning, Warning)
161 MP_DEFINE_EXCEPTION(RuntimeWarning, Warning)
162 MP_DEFINE_EXCEPTION(SyntaxWarning, Warning)
163 MP_DEFINE_EXCEPTION(UserWarning, Warning)
164 MP_DEFINE_EXCEPTION(FutureWarning, Warning)
165 MP_DEFINE_EXCEPTION(ImportWarning, Warning)
166 MP_DEFINE_EXCEPTION(UnicodeWarning, Warning)
167 MP_DEFINE_EXCEPTION(BytesWarning, Warning)
168 MP_DEFINE_EXCEPTION(ResourceWarning, Warning)
Damien Georgec9109722014-03-22 23:40:02 +0000169 */
Damien Georgec5966122014-02-15 16:10:44 +0000170
171mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type) {
172 return mp_obj_new_exception_msg_varg(exc_type, NULL);
Damiend99b0522013-12-21 18:17:45 +0000173}
174
Damien Georgec5966122014-02-15 16:10:44 +0000175mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, const char *msg) {
176 return mp_obj_new_exception_msg_varg(exc_type, msg);
Damiend99b0522013-12-21 18:17:45 +0000177}
178
Damien Georgec5966122014-02-15 16:10:44 +0000179mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char *fmt, ...) {
180 // check that the given type is an exception type
181 assert(exc_type->make_new == mp_obj_exception_make_new);
182
Damien George6c73ca12014-01-08 18:11:23 +0000183 // make exception object
Damien Georgec5966122014-02-15 16:10:44 +0000184 mp_obj_exception_t *o = m_new_obj_var(mp_obj_exception_t, mp_obj_t, 0);
185 o->base.type = exc_type;
Damien George136b1492014-01-19 12:38:49 +0000186 o->traceback = MP_OBJ_NULL;
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200187 o->args.len = 0;
Damien Georgec5966122014-02-15 16:10:44 +0000188
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200189 if (fmt == NULL) {
Damien Georgec5966122014-02-15 16:10:44 +0000190 // no message
Damien George1ba1fac2014-01-29 18:57:20 +0000191 o->msg = NULL;
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200192 } else {
193 // render exception message
Damien George1ba1fac2014-01-29 18:57:20 +0000194 o->msg = vstr_new();
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200195 va_list ap;
196 va_start(ap, fmt);
Damien George1ba1fac2014-01-29 18:57:20 +0000197 vstr_vprintf(o->msg, fmt, ap);
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200198 va_end(ap);
Damien George6c73ca12014-01-08 18:11:23 +0000199 }
Damien George6c73ca12014-01-08 18:11:23 +0000200
201 return o;
202}
203
Damien Georgec5966122014-02-15 16:10:44 +0000204// return true if the given object is an exception type
Damien Georgec5966122014-02-15 16:10:44 +0000205bool mp_obj_is_exception_type(mp_obj_t self_in) {
206 if (MP_OBJ_IS_TYPE(self_in, &mp_type_type)) {
Damien George9e6e9352014-03-26 18:37:06 +0000207 // optimisation when self_in is a builtin exception
Damien Georgec5966122014-02-15 16:10:44 +0000208 mp_obj_type_t *self = self_in;
Damien George9e6e9352014-03-26 18:37:06 +0000209 if (self->make_new == mp_obj_exception_make_new) {
210 return true;
211 }
Damien Georgec5966122014-02-15 16:10:44 +0000212 }
Damien George9e6e9352014-03-26 18:37:06 +0000213 return mp_obj_is_subclass_fast(self_in, &mp_type_BaseException);
Damien Georgec5966122014-02-15 16:10:44 +0000214}
215
216// return true if the given object is an instance of an exception type
Damien Georgec5966122014-02-15 16:10:44 +0000217bool mp_obj_is_exception_instance(mp_obj_t self_in) {
Damien George9e6e9352014-03-26 18:37:06 +0000218 return mp_obj_is_exception_type(mp_obj_get_type(self_in));
Damien Georgec5966122014-02-15 16:10:44 +0000219}
220
221void mp_obj_exception_clear_traceback(mp_obj_t self_in) {
222 // make sure self_in is an exception instance
Damien George9e6e9352014-03-26 18:37:06 +0000223 // TODO add traceback information to user-defined exceptions (need proper builtin subclassing for that)
224 if (mp_obj_get_type(self_in)->make_new == mp_obj_exception_make_new) {
225 mp_obj_exception_t *self = self_in;
Damien Georgec5966122014-02-15 16:10:44 +0000226
Damien George9e6e9352014-03-26 18:37:06 +0000227 // just set the traceback to the null object
228 // we don't want to call any memory management functions here
229 self->traceback = MP_OBJ_NULL;
230 }
Damienb86e3f92013-12-29 17:17:43 +0000231}
Damien George08335002014-01-18 23:24:36 +0000232
Damien George136b1492014-01-19 12:38:49 +0000233void 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 +0000234 // make sure self_in is an exception instance
Damien George9e6e9352014-03-26 18:37:06 +0000235 // TODO add traceback information to user-defined exceptions (need proper builtin subclassing for that)
236 if (mp_obj_get_type(self_in)->make_new == mp_obj_exception_make_new) {
237 mp_obj_exception_t *self = self_in;
Damien Georgec5966122014-02-15 16:10:44 +0000238
Damien George9e6e9352014-03-26 18:37:06 +0000239 // for traceback, we are just using the list object for convenience, it's not really a list of Python objects
240 if (self->traceback == MP_OBJ_NULL) {
241 self->traceback = mp_obj_new_list(0, NULL);
242 }
243 mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)file);
244 mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)line);
245 mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)block);
Damien George08335002014-01-18 23:24:36 +0000246 }
Damien George08335002014-01-18 23:24:36 +0000247}
248
Damien George136b1492014-01-19 12:38:49 +0000249void 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 +0000250 // make sure self_in is an exception instance
251 assert(mp_obj_get_type(self_in)->make_new == mp_obj_exception_make_new);
Damien George08335002014-01-18 23:24:36 +0000252 mp_obj_exception_t *self = self_in;
Damien Georgec5966122014-02-15 16:10:44 +0000253
Damien George136b1492014-01-19 12:38:49 +0000254 if (self->traceback == MP_OBJ_NULL) {
255 *n = 0;
256 *values = NULL;
257 } else {
258 uint n2;
259 mp_obj_list_get(self->traceback, &n2, (mp_obj_t**)values);
260 *n = n2;
261 }
Damien George08335002014-01-18 23:24:36 +0000262}