blob: bf83818b4a29bb91ffd6d5480647539ebf35b90f [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Damiend99b0522013-12-21 18:17:45 +000027#include <string.h>
Damien George6c73ca12014-01-08 18:11:23 +000028#include <stdarg.h>
Damiend99b0522013-12-21 18:17:45 +000029#include <assert.h>
30
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030031#include "mpconfig.h"
Damiend99b0522013-12-21 18:17:45 +000032#include "nlr.h"
33#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000034#include "qstr.h"
Damiend99b0522013-12-21 18:17:45 +000035#include "obj.h"
Paul Sokolovskyddf21782014-01-12 23:30:20 +020036#include "objtuple.h"
Paul Sokolovsky91e556a2014-05-02 02:27:00 +030037#include "objtype.h"
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +020038#include "runtime.h"
39#include "runtime0.h"
Damiend99b0522013-12-21 18:17:45 +000040
Damien George07ddab52014-03-29 13:15:08 +000041typedef struct _mp_obj_exception_t {
Damiend99b0522013-12-21 18:17:45 +000042 mp_obj_base_t base;
Damien George136b1492014-01-19 12:38:49 +000043 mp_obj_t traceback; // a list object, holding (file,line,block) as numbers (not Python objects); a hack for now
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +030044 mp_obj_tuple_t *args;
Damiend99b0522013-12-21 18:17:45 +000045} mp_obj_exception_t;
46
Damien George6902eed2014-04-04 10:52:59 +000047// Instance of MemoryError exception - needed by mp_malloc_fail
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +030048const mp_obj_exception_t mp_const_MemoryError_obj = {{&mp_type_MemoryError}, MP_OBJ_NULL, mp_const_empty_tuple};
Damien George6902eed2014-04-04 10:52:59 +000049
Damien Georgef0954e32014-04-10 14:38:25 +010050// Local non-heap memory for allocating an exception when we run out of RAM
51STATIC mp_obj_exception_t mp_emergency_exception_obj;
52
Paul Sokolovskyc4d589e2014-03-28 02:37:28 +020053// Instance of GeneratorExit exception - needed by generator.close()
54// This would belong to objgenerator.c, but to keep mp_obj_exception_t
55// definition module-private so far, have it here.
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +030056const mp_obj_exception_t mp_const_GeneratorExit_obj = {{&mp_type_GeneratorExit}, MP_OBJ_NULL, mp_const_empty_tuple};
Paul Sokolovskyc4d589e2014-03-28 02:37:28 +020057
Damien Georgec5966122014-02-15 16:10:44 +000058STATIC 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 +000059 mp_obj_exception_t *o = o_in;
Paul Sokolovskyd8351ca2014-05-02 01:51:25 +030060 mp_print_kind_t k = kind & ~PRINT_EXC_SUBCLASS;
61 bool is_subclass = kind & PRINT_EXC_SUBCLASS;
62 if (!is_subclass && (k == PRINT_REPR || k == PRINT_EXC)) {
Paul Sokolovskya96d3d02014-03-31 01:10:10 +030063 print(env, "%s", qstr_str(o->base.type->name));
Damiend99b0522013-12-21 18:17:45 +000064 }
Paul Sokolovskyd8351ca2014-05-02 01:51:25 +030065
66 if (k == PRINT_EXC) {
67 print(env, ": ");
68 }
69
70 if (k == PRINT_STR || k == PRINT_EXC) {
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +030071 if (o->args == NULL || o->args->len == 0) {
Paul Sokolovskya96d3d02014-03-31 01:10:10 +030072 print(env, "");
73 return;
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +030074 } else if (o->args->len == 1) {
75 mp_obj_print_helper(print, env, o->args->items[0], PRINT_STR);
Paul Sokolovskya96d3d02014-03-31 01:10:10 +030076 return;
77 }
78 }
Damien George2323ef92014-05-11 18:00:45 +010079 mp_obj_tuple_print(print, env, o->args, kind);
Damiend99b0522013-12-21 18:17:45 +000080}
81
Paul Sokolovskyd8351ca2014-05-02 01:51:25 +030082mp_obj_t mp_obj_exception_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
Damien Georgec5966122014-02-15 16:10:44 +000083 mp_obj_type_t *type = type_in;
Damien George20006db2014-01-18 14:10:48 +000084
85 if (n_kw != 0) {
Damien Georgeea13f402014-04-05 18:32:08 +010086 nlr_raise(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 +000087 }
88
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +030089 mp_obj_exception_t *o = m_new_obj_var_maybe(mp_obj_exception_t, mp_obj_t, 0);
Damien Georgef0954e32014-04-10 14:38:25 +010090 if (o == NULL) {
91 // Couldn't allocate heap memory; use local data instead.
92 o = &mp_emergency_exception_obj;
93 // We can't store any args.
94 n_args = 0;
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +030095 o->args = mp_const_empty_tuple;
96 } else {
97 o->args = mp_obj_new_tuple(n_args, args);
Damien Georgef0954e32014-04-10 14:38:25 +010098 }
Damien Georgec5966122014-02-15 16:10:44 +000099 o->base.type = type;
Paul Sokolovsky60a0d3f2014-02-14 21:01:12 +0200100 o->traceback = MP_OBJ_NULL;
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200101 return o;
102}
103
Paul Sokolovskyaf1ae302014-03-26 19:17:20 +0200104// Get exception "value" - that is, first argument, or None
105mp_obj_t mp_obj_exception_get_value(mp_obj_t self_in) {
106 mp_obj_exception_t *self = self_in;
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300107 if (self->args->len == 0) {
Paul Sokolovskyaf1ae302014-03-26 19:17:20 +0200108 return mp_const_none;
109 } else {
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300110 return self->args->items[0];
Paul Sokolovskyaf1ae302014-03-26 19:17:20 +0200111 }
112}
113
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +0200114STATIC void exception_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
115 mp_obj_exception_t *self = self_in;
116 if (attr == MP_QSTR_args) {
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300117 dest[0] = self->args;
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +0200118 } else if (self->base.type == &mp_type_StopIteration && attr == MP_QSTR_value) {
Paul Sokolovskyaf1ae302014-03-26 19:17:20 +0200119 dest[0] = mp_obj_exception_get_value(self);
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +0200120 }
121}
122
Paul Sokolovsky52386ca2014-05-18 20:44:04 +0300123STATIC mp_obj_t exc___init__(uint n_args, const mp_obj_t *args) {
124 mp_obj_exception_t *self = args[0];
125 mp_obj_t argst = mp_obj_new_tuple(n_args - 1, args + 1);
126 self->args = argst;
127 return mp_const_none;
128}
129STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(exc___init___obj, 1, MP_OBJ_FUN_ARGS_MAX, exc___init__);
130
131STATIC const mp_map_elem_t exc_locals_dict_table[] = {
132 { MP_OBJ_NEW_QSTR(MP_QSTR___init__), (mp_obj_t)&exc___init___obj },
133};
134
135STATIC MP_DEFINE_CONST_DICT(exc_locals_dict, exc_locals_dict_table);
136
Damien Georgec5966122014-02-15 16:10:44 +0000137const mp_obj_type_t mp_type_BaseException = {
138 { &mp_type_type },
139 .name = MP_QSTR_BaseException,
140 .print = mp_obj_exception_print,
141 .make_new = mp_obj_exception_make_new,
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +0200142 .load_attr = exception_load_attr,
Paul Sokolovsky52386ca2014-05-18 20:44:04 +0300143 .locals_dict = (mp_obj_t)&exc_locals_dict,
Damiend99b0522013-12-21 18:17:45 +0000144};
145
Damien George22a08652014-02-15 21:05:25 +0000146#define MP_DEFINE_EXCEPTION_BASE(base_name) \
Damien George07ddab52014-03-29 13:15:08 +0000147STATIC 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 +0000148
149#define MP_DEFINE_EXCEPTION(exc_name, base_name) \
Damien Georgec5966122014-02-15 16:10:44 +0000150const mp_obj_type_t mp_type_ ## exc_name = { \
151 { &mp_type_type }, \
152 .name = MP_QSTR_ ## exc_name, \
153 .print = mp_obj_exception_print, \
154 .make_new = mp_obj_exception_make_new, \
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +0200155 .load_attr = exception_load_attr, \
Damien George22a08652014-02-15 21:05:25 +0000156 .bases_tuple = (mp_obj_t)&mp_type_ ## base_name ## _base_tuple, \
Damien Georgec5966122014-02-15 16:10:44 +0000157};
158
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000159// List of all exceptions, arranged as in the table at:
160// http://docs.python.org/3.3/library/exceptions.html
Damien George22a08652014-02-15 21:05:25 +0000161MP_DEFINE_EXCEPTION_BASE(BaseException)
Damien Georgeffb5cfc2014-03-25 14:29:40 +0000162//MP_DEFINE_EXCEPTION(SystemExit, BaseException)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000163//MP_DEFINE_EXCEPTION(KeyboardInterrupt, BaseException)
164MP_DEFINE_EXCEPTION(GeneratorExit, BaseException)
165MP_DEFINE_EXCEPTION(Exception, BaseException)
166 MP_DEFINE_EXCEPTION_BASE(Exception)
167 MP_DEFINE_EXCEPTION(StopIteration, Exception)
168 MP_DEFINE_EXCEPTION(ArithmeticError, Exception)
169 MP_DEFINE_EXCEPTION_BASE(ArithmeticError)
Damien Georgec63f9842014-03-27 23:49:06 +0000170 //MP_DEFINE_EXCEPTION(FloatingPointError, ArithmeticError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000171 MP_DEFINE_EXCEPTION(OverflowError, ArithmeticError)
172 MP_DEFINE_EXCEPTION(ZeroDivisionError, ArithmeticError)
173 MP_DEFINE_EXCEPTION(AssertionError, Exception)
174 MP_DEFINE_EXCEPTION(AttributeError, Exception)
Damien Georgec63f9842014-03-27 23:49:06 +0000175 //MP_DEFINE_EXCEPTION(BufferError, Exception)
Damien Georgeffb5cfc2014-03-25 14:29:40 +0000176 //MP_DEFINE_EXCEPTION(EnvironmentError, Exception)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000177 MP_DEFINE_EXCEPTION(EOFError, Exception)
178 MP_DEFINE_EXCEPTION(ImportError, Exception)
179 MP_DEFINE_EXCEPTION(IOError, Exception)
180 MP_DEFINE_EXCEPTION(LookupError, Exception)
181 MP_DEFINE_EXCEPTION_BASE(LookupError)
182 MP_DEFINE_EXCEPTION(IndexError, LookupError)
183 MP_DEFINE_EXCEPTION(KeyError, LookupError)
184 MP_DEFINE_EXCEPTION(MemoryError, Exception)
185 MP_DEFINE_EXCEPTION(NameError, Exception)
186 MP_DEFINE_EXCEPTION_BASE(NameError)
Damien Georgec63f9842014-03-27 23:49:06 +0000187 //MP_DEFINE_EXCEPTION(UnboundLocalError, NameError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000188 MP_DEFINE_EXCEPTION(OSError, Exception)
Damien Georgec9109722014-03-22 23:40:02 +0000189 MP_DEFINE_EXCEPTION_BASE(OSError)
Damien Georgec63f9842014-03-27 23:49:06 +0000190 /*
191 MP_DEFINE_EXCEPTION(BlockingIOError, OSError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000192 MP_DEFINE_EXCEPTION(ChildProcessError, OSError)
193 MP_DEFINE_EXCEPTION(ConnectionError, OSError)
194 MP_DEFINE_EXCEPTION(BrokenPipeError, ConnectionError)
195 MP_DEFINE_EXCEPTION(ConnectionAbortedError, ConnectionError)
196 MP_DEFINE_EXCEPTION(ConnectionRefusedError, ConnectionError)
197 MP_DEFINE_EXCEPTION(ConnectionResetError, ConnectionError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000198 MP_DEFINE_EXCEPTION(InterruptedError, OSError)
199 MP_DEFINE_EXCEPTION(IsADirectoryError, OSError)
200 MP_DEFINE_EXCEPTION(NotADirectoryError, OSError)
201 MP_DEFINE_EXCEPTION(PermissionError, OSError)
202 MP_DEFINE_EXCEPTION(ProcessLookupError, OSError)
Damien Georgec63f9842014-03-27 23:49:06 +0000203 MP_DEFINE_EXCEPTION(TimeoutError, OSError)
Damien Georgec9109722014-03-22 23:40:02 +0000204 MP_DEFINE_EXCEPTION(FileExistsError, OSError)
205 MP_DEFINE_EXCEPTION(FileNotFoundError, OSError)
Damien Georgec63f9842014-03-27 23:49:06 +0000206 MP_DEFINE_EXCEPTION(ReferenceError, Exception)
207 */
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000208 MP_DEFINE_EXCEPTION(RuntimeError, Exception)
209 MP_DEFINE_EXCEPTION_BASE(RuntimeError)
210 MP_DEFINE_EXCEPTION(NotImplementedError, RuntimeError)
211 MP_DEFINE_EXCEPTION(SyntaxError, Exception)
212 MP_DEFINE_EXCEPTION_BASE(SyntaxError)
213 MP_DEFINE_EXCEPTION(IndentationError, SyntaxError)
Damien Georgec63f9842014-03-27 23:49:06 +0000214 /*
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000215 MP_DEFINE_EXCEPTION_BASE(IndentationError)
Damien Georgec63f9842014-03-27 23:49:06 +0000216 MP_DEFINE_EXCEPTION(TabError, IndentationError)
217 */
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000218 MP_DEFINE_EXCEPTION(SystemError, Exception)
219 MP_DEFINE_EXCEPTION(TypeError, Exception)
220 MP_DEFINE_EXCEPTION(ValueError, Exception)
221 //TODO: Implement UnicodeErrors which take arguments
Damien Georgec9109722014-03-22 23:40:02 +0000222 /*
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000223 MP_DEFINE_EXCEPTION(Warning, Exception)
224 MP_DEFINE_EXCEPTION_BASE(Warning)
225 MP_DEFINE_EXCEPTION(DeprecationWarning, Warning)
226 MP_DEFINE_EXCEPTION(PendingDeprecationWarning, Warning)
227 MP_DEFINE_EXCEPTION(RuntimeWarning, Warning)
228 MP_DEFINE_EXCEPTION(SyntaxWarning, Warning)
229 MP_DEFINE_EXCEPTION(UserWarning, Warning)
230 MP_DEFINE_EXCEPTION(FutureWarning, Warning)
231 MP_DEFINE_EXCEPTION(ImportWarning, Warning)
232 MP_DEFINE_EXCEPTION(UnicodeWarning, Warning)
233 MP_DEFINE_EXCEPTION(BytesWarning, Warning)
234 MP_DEFINE_EXCEPTION(ResourceWarning, Warning)
Damien Georgec9109722014-03-22 23:40:02 +0000235 */
Damien Georgec5966122014-02-15 16:10:44 +0000236
237mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type) {
Paul Sokolovskya96d3d02014-03-31 01:10:10 +0300238 return mp_obj_new_exception_args(exc_type, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000239}
240
Paul Sokolovskydec31bb2014-04-22 00:01:13 +0300241// "Optimized" version for common(?) case of having 1 exception arg
242mp_obj_t mp_obj_new_exception_arg1(const mp_obj_type_t *exc_type, mp_obj_t arg) {
243 return mp_obj_new_exception_args(exc_type, 1, &arg);
244}
245
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200246mp_obj_t mp_obj_new_exception_args(const mp_obj_type_t *exc_type, uint n_args, const mp_obj_t *args) {
247 assert(exc_type->make_new == mp_obj_exception_make_new);
248 return exc_type->make_new((mp_obj_t)exc_type, n_args, 0, args);
249}
250
Damien Georgec5966122014-02-15 16:10:44 +0000251mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, const char *msg) {
252 return mp_obj_new_exception_msg_varg(exc_type, msg);
Damiend99b0522013-12-21 18:17:45 +0000253}
254
Damien Georgec5966122014-02-15 16:10:44 +0000255mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char *fmt, ...) {
256 // check that the given type is an exception type
257 assert(exc_type->make_new == mp_obj_exception_make_new);
258
Damien George6c73ca12014-01-08 18:11:23 +0000259 // make exception object
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300260 mp_obj_exception_t *o = m_new_obj_var_maybe(mp_obj_exception_t, mp_obj_t, 0);
Damien Georgef0954e32014-04-10 14:38:25 +0100261 if (o == NULL) {
262 // Couldn't allocate heap memory; use local data instead.
263 // Unfortunately, we won't be able to format the string...
264 o = &mp_emergency_exception_obj;
265 o->base.type = exc_type;
266 o->traceback = MP_OBJ_NULL;
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300267 o->args = mp_const_empty_tuple;
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200268 } else {
Damien Georgef0954e32014-04-10 14:38:25 +0100269 o->base.type = exc_type;
270 o->traceback = MP_OBJ_NULL;
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300271 o->args = mp_obj_new_tuple(1, NULL);
Damien Georgef0954e32014-04-10 14:38:25 +0100272
273 if (fmt == NULL) {
274 // no message
275 assert(0);
276 } else {
277 // render exception message and store as .args[0]
278 // TODO: optimize bufferbloat
279 vstr_t *vstr = vstr_new();
280 va_list ap;
281 va_start(ap, fmt);
282 vstr_vprintf(vstr, fmt, ap);
283 va_end(ap);
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300284 o->args->items[0] = mp_obj_new_str((byte*)vstr->buf, vstr->len, false);
Damien Georgef0954e32014-04-10 14:38:25 +0100285 vstr_free(vstr);
286 }
Damien George6c73ca12014-01-08 18:11:23 +0000287 }
Damien George6c73ca12014-01-08 18:11:23 +0000288
289 return o;
290}
291
Damien Georgec5966122014-02-15 16:10:44 +0000292// return true if the given object is an exception type
Damien Georgec5966122014-02-15 16:10:44 +0000293bool mp_obj_is_exception_type(mp_obj_t self_in) {
294 if (MP_OBJ_IS_TYPE(self_in, &mp_type_type)) {
Damien George9e6e9352014-03-26 18:37:06 +0000295 // optimisation when self_in is a builtin exception
Damien Georgec5966122014-02-15 16:10:44 +0000296 mp_obj_type_t *self = self_in;
Damien George9e6e9352014-03-26 18:37:06 +0000297 if (self->make_new == mp_obj_exception_make_new) {
298 return true;
299 }
Damien Georgec5966122014-02-15 16:10:44 +0000300 }
Damien George9e6e9352014-03-26 18:37:06 +0000301 return mp_obj_is_subclass_fast(self_in, &mp_type_BaseException);
Damien Georgec5966122014-02-15 16:10:44 +0000302}
303
304// return true if the given object is an instance of an exception type
Damien Georgec5966122014-02-15 16:10:44 +0000305bool mp_obj_is_exception_instance(mp_obj_t self_in) {
Damien George9e6e9352014-03-26 18:37:06 +0000306 return mp_obj_is_exception_type(mp_obj_get_type(self_in));
Damien Georgec5966122014-02-15 16:10:44 +0000307}
308
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200309// return true if exception (type or instance) is a subclass of given
310// exception type.
311bool mp_obj_exception_match(mp_obj_t exc, const mp_obj_type_t *exc_type) {
312 // TODO: move implementation from RT_BINARY_OP_EXCEPTION_MATCH here.
Damien Georged17926d2014-03-30 13:35:08 +0100313 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 +0200314}
315
Paul Sokolovsky91e556a2014-05-02 02:27:00 +0300316// traceback handling functions
Damien Georgec5966122014-02-15 16:10:44 +0000317
Paul Sokolovsky91e556a2014-05-02 02:27:00 +0300318#define GET_NATIVE_EXCEPTION(self, self_in) \
319 /* make sure self_in is an exception instance */ \
320 assert(mp_obj_is_exception_instance(self_in)); \
321 mp_obj_exception_t *self; \
322 if (mp_obj_is_native_exception_instance(self_in)) { \
323 self = self_in; \
324 } else { \
325 self = ((mp_obj_instance_t*)self_in)->subobj[0]; \
Damien George9e6e9352014-03-26 18:37:06 +0000326 }
Paul Sokolovsky91e556a2014-05-02 02:27:00 +0300327
328void mp_obj_exception_clear_traceback(mp_obj_t self_in) {
329 GET_NATIVE_EXCEPTION(self, self_in);
330 // just set the traceback to the null object
331 // we don't want to call any memory management functions here
332 self->traceback = MP_OBJ_NULL;
Damienb86e3f92013-12-29 17:17:43 +0000333}
Damien George08335002014-01-18 23:24:36 +0000334
Damien George136b1492014-01-19 12:38:49 +0000335void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, machine_uint_t line, qstr block) {
Paul Sokolovsky91e556a2014-05-02 02:27:00 +0300336 GET_NATIVE_EXCEPTION(self, self_in);
Damien Georgec5966122014-02-15 16:10:44 +0000337
Paul Sokolovsky91e556a2014-05-02 02:27:00 +0300338 // for traceback, we are just using the list object for convenience, it's not really a list of Python objects
339 if (self->traceback == MP_OBJ_NULL) {
340 self->traceback = mp_obj_new_list(0, NULL);
Damien George08335002014-01-18 23:24:36 +0000341 }
Paul Sokolovsky91e556a2014-05-02 02:27:00 +0300342 mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)file);
343 mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)line);
344 mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)block);
Damien George08335002014-01-18 23:24:36 +0000345}
346
Damien George136b1492014-01-19 12:38:49 +0000347void mp_obj_exception_get_traceback(mp_obj_t self_in, machine_uint_t *n, machine_uint_t **values) {
Paul Sokolovsky91e556a2014-05-02 02:27:00 +0300348 GET_NATIVE_EXCEPTION(self, self_in);
Damien Georgec5966122014-02-15 16:10:44 +0000349
Damien George136b1492014-01-19 12:38:49 +0000350 if (self->traceback == MP_OBJ_NULL) {
351 *n = 0;
352 *values = NULL;
353 } else {
354 uint n2;
355 mp_obj_list_get(self->traceback, &n2, (mp_obj_t**)values);
356 *n = n2;
357 }
Damien George08335002014-01-18 23:24:36 +0000358}