blob: de9bf1694f49235e7e53047d669def63e7117844 [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 Sokolovskyddf21782014-01-12 23:30:20 +020056 o->args.len = n_args;
Damien George20006db2014-01-18 14:10:48 +000057 memcpy(o->args.items, args, n_args * sizeof(mp_obj_t));
Paul Sokolovskyddf21782014-01-12 23:30:20 +020058 return o;
59}
60
Damien Georgec5966122014-02-15 16:10:44 +000061const mp_obj_type_t mp_type_BaseException = {
62 { &mp_type_type },
63 .name = MP_QSTR_BaseException,
64 .print = mp_obj_exception_print,
65 .make_new = mp_obj_exception_make_new,
Damiend99b0522013-12-21 18:17:45 +000066};
67
Damien George22a08652014-02-15 21:05:25 +000068#define MP_DEFINE_EXCEPTION_BASE(base_name) \
69STATIC const mp_obj_tuple_t mp_type_ ## base_name ## _base_tuple = {{&tuple_type}, 1, {(mp_obj_t)&mp_type_ ## base_name}};\
70
71#define MP_DEFINE_EXCEPTION(exc_name, base_name) \
Damien Georgec5966122014-02-15 16:10:44 +000072const mp_obj_type_t mp_type_ ## exc_name = { \
73 { &mp_type_type }, \
74 .name = MP_QSTR_ ## exc_name, \
75 .print = mp_obj_exception_print, \
76 .make_new = mp_obj_exception_make_new, \
Damien George22a08652014-02-15 21:05:25 +000077 .bases_tuple = (mp_obj_t)&mp_type_ ## base_name ## _base_tuple, \
Damien Georgec5966122014-02-15 16:10:44 +000078};
79
Rachel Dowdall721c55d2014-03-22 15:28:16 +000080// List of all exceptions, arranged as in the table at:
81// http://docs.python.org/3.3/library/exceptions.html
Damien George22a08652014-02-15 21:05:25 +000082MP_DEFINE_EXCEPTION_BASE(BaseException)
Rachel Dowdall721c55d2014-03-22 15:28:16 +000083MP_DEFINE_EXCEPTION(SystemExit, BaseException)
84//MP_DEFINE_EXCEPTION(KeyboardInterrupt, BaseException)
85MP_DEFINE_EXCEPTION(GeneratorExit, BaseException)
86MP_DEFINE_EXCEPTION(Exception, BaseException)
87 MP_DEFINE_EXCEPTION_BASE(Exception)
88 MP_DEFINE_EXCEPTION(StopIteration, Exception)
89 MP_DEFINE_EXCEPTION(ArithmeticError, Exception)
90 MP_DEFINE_EXCEPTION_BASE(ArithmeticError)
91 MP_DEFINE_EXCEPTION(FloatingPointError, ArithmeticError)
92 MP_DEFINE_EXCEPTION(OverflowError, ArithmeticError)
93 MP_DEFINE_EXCEPTION(ZeroDivisionError, ArithmeticError)
94 MP_DEFINE_EXCEPTION(AssertionError, Exception)
95 MP_DEFINE_EXCEPTION(AttributeError, Exception)
96 MP_DEFINE_EXCEPTION(BufferError, Exception)
97 MP_DEFINE_EXCEPTION(EnvironmentError, Exception)
98 MP_DEFINE_EXCEPTION(EOFError, Exception)
99 MP_DEFINE_EXCEPTION(ImportError, Exception)
100 MP_DEFINE_EXCEPTION(IOError, Exception)
101 MP_DEFINE_EXCEPTION(LookupError, Exception)
102 MP_DEFINE_EXCEPTION_BASE(LookupError)
103 MP_DEFINE_EXCEPTION(IndexError, LookupError)
104 MP_DEFINE_EXCEPTION(KeyError, LookupError)
105 MP_DEFINE_EXCEPTION(MemoryError, Exception)
106 MP_DEFINE_EXCEPTION(NameError, Exception)
107 MP_DEFINE_EXCEPTION_BASE(NameError)
108 MP_DEFINE_EXCEPTION(UnboundLocalError, NameError)
109 MP_DEFINE_EXCEPTION(OSError, Exception)
Damien Georgec9109722014-03-22 23:40:02 +0000110 MP_DEFINE_EXCEPTION_BASE(OSError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000111 // Probably don't need these
Damien Georgec9109722014-03-22 23:40:02 +0000112 /*MP_DEFINE_EXCEPTION(BlockingIOError, OSError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000113 MP_DEFINE_EXCEPTION(ChildProcessError, OSError)
114 MP_DEFINE_EXCEPTION(ConnectionError, OSError)
115 MP_DEFINE_EXCEPTION(BrokenPipeError, ConnectionError)
116 MP_DEFINE_EXCEPTION(ConnectionAbortedError, ConnectionError)
117 MP_DEFINE_EXCEPTION(ConnectionRefusedError, ConnectionError)
118 MP_DEFINE_EXCEPTION(ConnectionResetError, ConnectionError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000119 MP_DEFINE_EXCEPTION(InterruptedError, OSError)
120 MP_DEFINE_EXCEPTION(IsADirectoryError, OSError)
121 MP_DEFINE_EXCEPTION(NotADirectoryError, OSError)
122 MP_DEFINE_EXCEPTION(PermissionError, OSError)
123 MP_DEFINE_EXCEPTION(ProcessLookupError, OSError)
124 MP_DEFINE_EXCEPTION(TimeoutError, OSError)*/
Damien Georgec9109722014-03-22 23:40:02 +0000125 MP_DEFINE_EXCEPTION(FileExistsError, OSError)
126 MP_DEFINE_EXCEPTION(FileNotFoundError, OSError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000127 MP_DEFINE_EXCEPTION(ReferenceError, Exception)
128 MP_DEFINE_EXCEPTION(RuntimeError, Exception)
129 MP_DEFINE_EXCEPTION_BASE(RuntimeError)
130 MP_DEFINE_EXCEPTION(NotImplementedError, RuntimeError)
131 MP_DEFINE_EXCEPTION(SyntaxError, Exception)
132 MP_DEFINE_EXCEPTION_BASE(SyntaxError)
133 MP_DEFINE_EXCEPTION(IndentationError, SyntaxError)
134 MP_DEFINE_EXCEPTION_BASE(IndentationError)
135 MP_DEFINE_EXCEPTION(TabError, IndentationError)
136 MP_DEFINE_EXCEPTION(SystemError, Exception)
137 MP_DEFINE_EXCEPTION(TypeError, Exception)
138 MP_DEFINE_EXCEPTION(ValueError, Exception)
139 //TODO: Implement UnicodeErrors which take arguments
Damien Georgec9109722014-03-22 23:40:02 +0000140 /*
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000141 MP_DEFINE_EXCEPTION(Warning, Exception)
142 MP_DEFINE_EXCEPTION_BASE(Warning)
143 MP_DEFINE_EXCEPTION(DeprecationWarning, Warning)
144 MP_DEFINE_EXCEPTION(PendingDeprecationWarning, Warning)
145 MP_DEFINE_EXCEPTION(RuntimeWarning, Warning)
146 MP_DEFINE_EXCEPTION(SyntaxWarning, Warning)
147 MP_DEFINE_EXCEPTION(UserWarning, Warning)
148 MP_DEFINE_EXCEPTION(FutureWarning, Warning)
149 MP_DEFINE_EXCEPTION(ImportWarning, Warning)
150 MP_DEFINE_EXCEPTION(UnicodeWarning, Warning)
151 MP_DEFINE_EXCEPTION(BytesWarning, Warning)
152 MP_DEFINE_EXCEPTION(ResourceWarning, Warning)
Damien Georgec9109722014-03-22 23:40:02 +0000153 */
Damien Georgec5966122014-02-15 16:10:44 +0000154
155mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type) {
156 return mp_obj_new_exception_msg_varg(exc_type, NULL);
Damiend99b0522013-12-21 18:17:45 +0000157}
158
Damien Georgec5966122014-02-15 16:10:44 +0000159mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, const char *msg) {
160 return mp_obj_new_exception_msg_varg(exc_type, msg);
Damiend99b0522013-12-21 18:17:45 +0000161}
162
Damien Georgec5966122014-02-15 16:10:44 +0000163mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char *fmt, ...) {
164 // check that the given type is an exception type
165 assert(exc_type->make_new == mp_obj_exception_make_new);
166
Damien George6c73ca12014-01-08 18:11:23 +0000167 // make exception object
Damien Georgec5966122014-02-15 16:10:44 +0000168 mp_obj_exception_t *o = m_new_obj_var(mp_obj_exception_t, mp_obj_t, 0);
169 o->base.type = exc_type;
Damien George136b1492014-01-19 12:38:49 +0000170 o->traceback = MP_OBJ_NULL;
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200171 o->args.len = 0;
Damien Georgec5966122014-02-15 16:10:44 +0000172
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200173 if (fmt == NULL) {
Damien Georgec5966122014-02-15 16:10:44 +0000174 // no message
Damien George1ba1fac2014-01-29 18:57:20 +0000175 o->msg = NULL;
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200176 } else {
177 // render exception message
Damien George1ba1fac2014-01-29 18:57:20 +0000178 o->msg = vstr_new();
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200179 va_list ap;
180 va_start(ap, fmt);
Damien George1ba1fac2014-01-29 18:57:20 +0000181 vstr_vprintf(o->msg, fmt, ap);
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200182 va_end(ap);
Damien George6c73ca12014-01-08 18:11:23 +0000183 }
Damien George6c73ca12014-01-08 18:11:23 +0000184
185 return o;
186}
187
Damien Georgec5966122014-02-15 16:10:44 +0000188// return true if the given object is an exception type
189// TODO make this work for user defined exceptions
190bool mp_obj_is_exception_type(mp_obj_t self_in) {
191 if (MP_OBJ_IS_TYPE(self_in, &mp_type_type)) {
192 mp_obj_type_t *self = self_in;
193 return self->make_new == mp_obj_exception_make_new;
194 } else {
195 return false;
196 }
197}
198
199// return true if the given object is an instance of an exception type
200// TODO make this work for user defined exceptions
201bool mp_obj_is_exception_instance(mp_obj_t self_in) {
202 return mp_obj_get_type(self_in)->make_new == mp_obj_exception_make_new;
203}
204
205void mp_obj_exception_clear_traceback(mp_obj_t self_in) {
206 // make sure self_in is an exception instance
207 assert(mp_obj_get_type(self_in)->make_new == mp_obj_exception_make_new);
Damienb86e3f92013-12-29 17:17:43 +0000208 mp_obj_exception_t *self = self_in;
Damien Georgec5966122014-02-15 16:10:44 +0000209
210 // just set the traceback to the null object
211 // we don't want to call any memory management functions here
212 self->traceback = MP_OBJ_NULL;
Damienb86e3f92013-12-29 17:17:43 +0000213}
Damien George08335002014-01-18 23:24:36 +0000214
Damien George136b1492014-01-19 12:38:49 +0000215void 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 +0000216 // make sure self_in is an exception instance
217 assert(mp_obj_get_type(self_in)->make_new == mp_obj_exception_make_new);
Damien George08335002014-01-18 23:24:36 +0000218 mp_obj_exception_t *self = self_in;
Damien Georgec5966122014-02-15 16:10:44 +0000219
Damien George136b1492014-01-19 12:38:49 +0000220 // for traceback, we are just using the list object for convenience, it's not really a list of Python objects
221 if (self->traceback == MP_OBJ_NULL) {
Damien Georgec8f78bc2014-02-15 22:55:00 +0000222 self->traceback = mp_obj_new_list(0, NULL);
Damien George08335002014-01-18 23:24:36 +0000223 }
Damien George136b1492014-01-19 12:38:49 +0000224 mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)file);
225 mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)line);
226 mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)block);
Damien George08335002014-01-18 23:24:36 +0000227}
228
Damien George136b1492014-01-19 12:38:49 +0000229void 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 +0000230 // make sure self_in is an exception instance
231 assert(mp_obj_get_type(self_in)->make_new == mp_obj_exception_make_new);
Damien George08335002014-01-18 23:24:36 +0000232 mp_obj_exception_t *self = self_in;
Damien Georgec5966122014-02-15 16:10:44 +0000233
Damien George136b1492014-01-19 12:38:49 +0000234 if (self->traceback == MP_OBJ_NULL) {
235 *n = 0;
236 *values = NULL;
237 } else {
238 uint n2;
239 mp_obj_list_get(self->traceback, &n2, (mp_obj_t**)values);
240 *n = n2;
241 }
Damien George08335002014-01-18 23:24:36 +0000242}