blob: f96523ed2393da35578f5b459b7fad5a1895fd80 [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 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
Damien Georgec5966122014-02-15 16:10:44 +000063const mp_obj_type_t mp_type_BaseException = {
64 { &mp_type_type },
65 .name = MP_QSTR_BaseException,
66 .print = mp_obj_exception_print,
67 .make_new = mp_obj_exception_make_new,
Damiend99b0522013-12-21 18:17:45 +000068};
69
Damien George22a08652014-02-15 21:05:25 +000070#define MP_DEFINE_EXCEPTION_BASE(base_name) \
71STATIC const mp_obj_tuple_t mp_type_ ## base_name ## _base_tuple = {{&tuple_type}, 1, {(mp_obj_t)&mp_type_ ## base_name}};\
72
73#define MP_DEFINE_EXCEPTION(exc_name, base_name) \
Damien Georgec5966122014-02-15 16:10:44 +000074const mp_obj_type_t mp_type_ ## exc_name = { \
75 { &mp_type_type }, \
76 .name = MP_QSTR_ ## exc_name, \
77 .print = mp_obj_exception_print, \
78 .make_new = mp_obj_exception_make_new, \
Damien George22a08652014-02-15 21:05:25 +000079 .bases_tuple = (mp_obj_t)&mp_type_ ## base_name ## _base_tuple, \
Damien Georgec5966122014-02-15 16:10:44 +000080};
81
Rachel Dowdall721c55d2014-03-22 15:28:16 +000082// List of all exceptions, arranged as in the table at:
83// http://docs.python.org/3.3/library/exceptions.html
Damien George22a08652014-02-15 21:05:25 +000084MP_DEFINE_EXCEPTION_BASE(BaseException)
Rachel Dowdall721c55d2014-03-22 15:28:16 +000085MP_DEFINE_EXCEPTION(SystemExit, BaseException)
86//MP_DEFINE_EXCEPTION(KeyboardInterrupt, BaseException)
87MP_DEFINE_EXCEPTION(GeneratorExit, BaseException)
88MP_DEFINE_EXCEPTION(Exception, BaseException)
89 MP_DEFINE_EXCEPTION_BASE(Exception)
90 MP_DEFINE_EXCEPTION(StopIteration, Exception)
91 MP_DEFINE_EXCEPTION(ArithmeticError, Exception)
92 MP_DEFINE_EXCEPTION_BASE(ArithmeticError)
93 MP_DEFINE_EXCEPTION(FloatingPointError, ArithmeticError)
94 MP_DEFINE_EXCEPTION(OverflowError, ArithmeticError)
95 MP_DEFINE_EXCEPTION(ZeroDivisionError, ArithmeticError)
96 MP_DEFINE_EXCEPTION(AssertionError, Exception)
97 MP_DEFINE_EXCEPTION(AttributeError, Exception)
98 MP_DEFINE_EXCEPTION(BufferError, Exception)
99 MP_DEFINE_EXCEPTION(EnvironmentError, Exception)
100 MP_DEFINE_EXCEPTION(EOFError, Exception)
101 MP_DEFINE_EXCEPTION(ImportError, Exception)
102 MP_DEFINE_EXCEPTION(IOError, Exception)
103 MP_DEFINE_EXCEPTION(LookupError, Exception)
104 MP_DEFINE_EXCEPTION_BASE(LookupError)
105 MP_DEFINE_EXCEPTION(IndexError, LookupError)
106 MP_DEFINE_EXCEPTION(KeyError, LookupError)
107 MP_DEFINE_EXCEPTION(MemoryError, Exception)
108 MP_DEFINE_EXCEPTION(NameError, Exception)
109 MP_DEFINE_EXCEPTION_BASE(NameError)
110 MP_DEFINE_EXCEPTION(UnboundLocalError, NameError)
111 MP_DEFINE_EXCEPTION(OSError, Exception)
Damien Georgec9109722014-03-22 23:40:02 +0000112 MP_DEFINE_EXCEPTION_BASE(OSError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000113 // Probably don't need these
Damien Georgec9109722014-03-22 23:40:02 +0000114 /*MP_DEFINE_EXCEPTION(BlockingIOError, OSError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000115 MP_DEFINE_EXCEPTION(ChildProcessError, OSError)
116 MP_DEFINE_EXCEPTION(ConnectionError, OSError)
117 MP_DEFINE_EXCEPTION(BrokenPipeError, ConnectionError)
118 MP_DEFINE_EXCEPTION(ConnectionAbortedError, ConnectionError)
119 MP_DEFINE_EXCEPTION(ConnectionRefusedError, ConnectionError)
120 MP_DEFINE_EXCEPTION(ConnectionResetError, ConnectionError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000121 MP_DEFINE_EXCEPTION(InterruptedError, OSError)
122 MP_DEFINE_EXCEPTION(IsADirectoryError, OSError)
123 MP_DEFINE_EXCEPTION(NotADirectoryError, OSError)
124 MP_DEFINE_EXCEPTION(PermissionError, OSError)
125 MP_DEFINE_EXCEPTION(ProcessLookupError, OSError)
126 MP_DEFINE_EXCEPTION(TimeoutError, OSError)*/
Damien Georgec9109722014-03-22 23:40:02 +0000127 MP_DEFINE_EXCEPTION(FileExistsError, OSError)
128 MP_DEFINE_EXCEPTION(FileNotFoundError, OSError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000129 MP_DEFINE_EXCEPTION(ReferenceError, Exception)
130 MP_DEFINE_EXCEPTION(RuntimeError, Exception)
131 MP_DEFINE_EXCEPTION_BASE(RuntimeError)
132 MP_DEFINE_EXCEPTION(NotImplementedError, RuntimeError)
133 MP_DEFINE_EXCEPTION(SyntaxError, Exception)
134 MP_DEFINE_EXCEPTION_BASE(SyntaxError)
135 MP_DEFINE_EXCEPTION(IndentationError, SyntaxError)
136 MP_DEFINE_EXCEPTION_BASE(IndentationError)
137 MP_DEFINE_EXCEPTION(TabError, IndentationError)
138 MP_DEFINE_EXCEPTION(SystemError, Exception)
139 MP_DEFINE_EXCEPTION(TypeError, Exception)
140 MP_DEFINE_EXCEPTION(ValueError, Exception)
141 //TODO: Implement UnicodeErrors which take arguments
Damien Georgec9109722014-03-22 23:40:02 +0000142 /*
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000143 MP_DEFINE_EXCEPTION(Warning, Exception)
144 MP_DEFINE_EXCEPTION_BASE(Warning)
145 MP_DEFINE_EXCEPTION(DeprecationWarning, Warning)
146 MP_DEFINE_EXCEPTION(PendingDeprecationWarning, Warning)
147 MP_DEFINE_EXCEPTION(RuntimeWarning, Warning)
148 MP_DEFINE_EXCEPTION(SyntaxWarning, Warning)
149 MP_DEFINE_EXCEPTION(UserWarning, Warning)
150 MP_DEFINE_EXCEPTION(FutureWarning, Warning)
151 MP_DEFINE_EXCEPTION(ImportWarning, Warning)
152 MP_DEFINE_EXCEPTION(UnicodeWarning, Warning)
153 MP_DEFINE_EXCEPTION(BytesWarning, Warning)
154 MP_DEFINE_EXCEPTION(ResourceWarning, Warning)
Damien Georgec9109722014-03-22 23:40:02 +0000155 */
Damien Georgec5966122014-02-15 16:10:44 +0000156
157mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type) {
158 return mp_obj_new_exception_msg_varg(exc_type, NULL);
Damiend99b0522013-12-21 18:17:45 +0000159}
160
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200161mp_obj_t mp_obj_new_exception_args(const mp_obj_type_t *exc_type, uint n_args, const mp_obj_t *args) {
162 assert(exc_type->make_new == mp_obj_exception_make_new);
163 return exc_type->make_new((mp_obj_t)exc_type, n_args, 0, args);
164}
165
Damien Georgec5966122014-02-15 16:10:44 +0000166mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, const char *msg) {
167 return mp_obj_new_exception_msg_varg(exc_type, msg);
Damiend99b0522013-12-21 18:17:45 +0000168}
169
Damien Georgec5966122014-02-15 16:10:44 +0000170mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char *fmt, ...) {
171 // check that the given type is an exception type
172 assert(exc_type->make_new == mp_obj_exception_make_new);
173
Damien George6c73ca12014-01-08 18:11:23 +0000174 // make exception object
Damien Georgec5966122014-02-15 16:10:44 +0000175 mp_obj_exception_t *o = m_new_obj_var(mp_obj_exception_t, mp_obj_t, 0);
176 o->base.type = exc_type;
Damien George136b1492014-01-19 12:38:49 +0000177 o->traceback = MP_OBJ_NULL;
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200178 o->args.len = 0;
Damien Georgec5966122014-02-15 16:10:44 +0000179
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200180 if (fmt == NULL) {
Damien Georgec5966122014-02-15 16:10:44 +0000181 // no message
Damien George1ba1fac2014-01-29 18:57:20 +0000182 o->msg = NULL;
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200183 } else {
184 // render exception message
Damien George1ba1fac2014-01-29 18:57:20 +0000185 o->msg = vstr_new();
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200186 va_list ap;
187 va_start(ap, fmt);
Damien George1ba1fac2014-01-29 18:57:20 +0000188 vstr_vprintf(o->msg, fmt, ap);
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200189 va_end(ap);
Damien George6c73ca12014-01-08 18:11:23 +0000190 }
Damien George6c73ca12014-01-08 18:11:23 +0000191
192 return o;
193}
194
Damien Georgec5966122014-02-15 16:10:44 +0000195// return true if the given object is an exception type
196// TODO make this work for user defined exceptions
197bool mp_obj_is_exception_type(mp_obj_t self_in) {
198 if (MP_OBJ_IS_TYPE(self_in, &mp_type_type)) {
199 mp_obj_type_t *self = self_in;
200 return self->make_new == mp_obj_exception_make_new;
201 } else {
202 return false;
203 }
204}
205
206// return true if the given object is an instance of an exception type
207// TODO make this work for user defined exceptions
208bool mp_obj_is_exception_instance(mp_obj_t self_in) {
209 return mp_obj_get_type(self_in)->make_new == mp_obj_exception_make_new;
210}
211
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200212// return true if exception (type or instance) is a subclass of given
213// exception type.
214bool mp_obj_exception_match(mp_obj_t exc, const mp_obj_type_t *exc_type) {
215 // TODO: move implementation from RT_BINARY_OP_EXCEPTION_MATCH here.
216 return rt_binary_op(RT_BINARY_OP_EXCEPTION_MATCH, exc, (mp_obj_t)exc_type) == mp_const_true;
217}
218
Damien Georgec5966122014-02-15 16:10:44 +0000219void mp_obj_exception_clear_traceback(mp_obj_t self_in) {
220 // make sure self_in is an exception instance
221 assert(mp_obj_get_type(self_in)->make_new == mp_obj_exception_make_new);
Damienb86e3f92013-12-29 17:17:43 +0000222 mp_obj_exception_t *self = self_in;
Damien Georgec5966122014-02-15 16:10:44 +0000223
224 // just set the traceback to the null object
225 // we don't want to call any memory management functions here
226 self->traceback = MP_OBJ_NULL;
Damienb86e3f92013-12-29 17:17:43 +0000227}
Damien George08335002014-01-18 23:24:36 +0000228
Damien George136b1492014-01-19 12:38:49 +0000229void 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 +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 // for traceback, we are just using the list object for convenience, it's not really a list of Python objects
235 if (self->traceback == MP_OBJ_NULL) {
Damien Georgec8f78bc2014-02-15 22:55:00 +0000236 self->traceback = mp_obj_new_list(0, NULL);
Damien George08335002014-01-18 23:24:36 +0000237 }
Damien George136b1492014-01-19 12:38:49 +0000238 mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)file);
239 mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)line);
240 mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)block);
Damien George08335002014-01-18 23:24:36 +0000241}
242
Damien George136b1492014-01-19 12:38:49 +0000243void 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 +0000244 // make sure self_in is an exception instance
245 assert(mp_obj_get_type(self_in)->make_new == mp_obj_exception_make_new);
Damien George08335002014-01-18 23:24:36 +0000246 mp_obj_exception_t *self = self_in;
Damien Georgec5966122014-02-15 16:10:44 +0000247
Damien George136b1492014-01-19 12:38:49 +0000248 if (self->traceback == MP_OBJ_NULL) {
249 *n = 0;
250 *values = NULL;
251 } else {
252 uint n2;
253 mp_obj_list_get(self->traceback, &n2, (mp_obj_t**)values);
254 *n = n2;
255 }
Damien George08335002014-01-18 23:24:36 +0000256}