blob: 9f421373bbd702f3324e4b36854d9228c4185af4 [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:
Chris Angelicodaf973a2014-06-06 03:51:03 +1000160// http://docs.python.org/3/library/exceptions.html
Damien George22a08652014-02-15 21:05:25 +0000161MP_DEFINE_EXCEPTION_BASE(BaseException)
Damien George7a4ddd22014-05-24 23:32:19 +0100162MP_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)
Damien Georgec63f9842014-03-27 23:49:06 +0000186 /*
Antonin ENFRUNda1fffa2014-05-12 00:21:50 +0200187 MP_DEFINE_EXCEPTION_BASE(NameError)
188 MP_DEFINE_EXCEPTION(UnboundLocalError, NameError)
189 */
190 MP_DEFINE_EXCEPTION(OSError, Exception)
191 /*
192 MP_DEFINE_EXCEPTION_BASE(OSError)
Damien Georgec63f9842014-03-27 23:49:06 +0000193 MP_DEFINE_EXCEPTION(BlockingIOError, OSError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000194 MP_DEFINE_EXCEPTION(ChildProcessError, OSError)
195 MP_DEFINE_EXCEPTION(ConnectionError, OSError)
196 MP_DEFINE_EXCEPTION(BrokenPipeError, ConnectionError)
197 MP_DEFINE_EXCEPTION(ConnectionAbortedError, ConnectionError)
198 MP_DEFINE_EXCEPTION(ConnectionRefusedError, ConnectionError)
199 MP_DEFINE_EXCEPTION(ConnectionResetError, ConnectionError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000200 MP_DEFINE_EXCEPTION(InterruptedError, OSError)
201 MP_DEFINE_EXCEPTION(IsADirectoryError, OSError)
202 MP_DEFINE_EXCEPTION(NotADirectoryError, OSError)
203 MP_DEFINE_EXCEPTION(PermissionError, OSError)
204 MP_DEFINE_EXCEPTION(ProcessLookupError, OSError)
Damien Georgec63f9842014-03-27 23:49:06 +0000205 MP_DEFINE_EXCEPTION(TimeoutError, OSError)
Damien Georgec9109722014-03-22 23:40:02 +0000206 MP_DEFINE_EXCEPTION(FileExistsError, OSError)
207 MP_DEFINE_EXCEPTION(FileNotFoundError, OSError)
Damien Georgec63f9842014-03-27 23:49:06 +0000208 MP_DEFINE_EXCEPTION(ReferenceError, Exception)
209 */
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000210 MP_DEFINE_EXCEPTION(RuntimeError, Exception)
211 MP_DEFINE_EXCEPTION_BASE(RuntimeError)
212 MP_DEFINE_EXCEPTION(NotImplementedError, RuntimeError)
213 MP_DEFINE_EXCEPTION(SyntaxError, Exception)
214 MP_DEFINE_EXCEPTION_BASE(SyntaxError)
215 MP_DEFINE_EXCEPTION(IndentationError, SyntaxError)
Damien Georgec63f9842014-03-27 23:49:06 +0000216 /*
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000217 MP_DEFINE_EXCEPTION_BASE(IndentationError)
Damien Georgec63f9842014-03-27 23:49:06 +0000218 MP_DEFINE_EXCEPTION(TabError, IndentationError)
219 */
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000220 MP_DEFINE_EXCEPTION(SystemError, Exception)
221 MP_DEFINE_EXCEPTION(TypeError, Exception)
222 MP_DEFINE_EXCEPTION(ValueError, Exception)
223 //TODO: Implement UnicodeErrors which take arguments
Damien Georgec9109722014-03-22 23:40:02 +0000224 /*
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000225 MP_DEFINE_EXCEPTION(Warning, Exception)
226 MP_DEFINE_EXCEPTION_BASE(Warning)
227 MP_DEFINE_EXCEPTION(DeprecationWarning, Warning)
228 MP_DEFINE_EXCEPTION(PendingDeprecationWarning, Warning)
229 MP_DEFINE_EXCEPTION(RuntimeWarning, Warning)
230 MP_DEFINE_EXCEPTION(SyntaxWarning, Warning)
231 MP_DEFINE_EXCEPTION(UserWarning, Warning)
232 MP_DEFINE_EXCEPTION(FutureWarning, Warning)
233 MP_DEFINE_EXCEPTION(ImportWarning, Warning)
234 MP_DEFINE_EXCEPTION(UnicodeWarning, Warning)
235 MP_DEFINE_EXCEPTION(BytesWarning, Warning)
236 MP_DEFINE_EXCEPTION(ResourceWarning, Warning)
Damien Georgec9109722014-03-22 23:40:02 +0000237 */
Damien Georgec5966122014-02-15 16:10:44 +0000238
239mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type) {
Paul Sokolovskya96d3d02014-03-31 01:10:10 +0300240 return mp_obj_new_exception_args(exc_type, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000241}
242
Paul Sokolovskydec31bb2014-04-22 00:01:13 +0300243// "Optimized" version for common(?) case of having 1 exception arg
244mp_obj_t mp_obj_new_exception_arg1(const mp_obj_type_t *exc_type, mp_obj_t arg) {
245 return mp_obj_new_exception_args(exc_type, 1, &arg);
246}
247
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200248mp_obj_t mp_obj_new_exception_args(const mp_obj_type_t *exc_type, uint n_args, const mp_obj_t *args) {
249 assert(exc_type->make_new == mp_obj_exception_make_new);
250 return exc_type->make_new((mp_obj_t)exc_type, n_args, 0, args);
251}
252
Damien Georgec5966122014-02-15 16:10:44 +0000253mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, const char *msg) {
254 return mp_obj_new_exception_msg_varg(exc_type, msg);
Damiend99b0522013-12-21 18:17:45 +0000255}
256
Damien Georgec5966122014-02-15 16:10:44 +0000257mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char *fmt, ...) {
258 // check that the given type is an exception type
259 assert(exc_type->make_new == mp_obj_exception_make_new);
260
Damien George6c73ca12014-01-08 18:11:23 +0000261 // make exception object
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300262 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 +0100263 if (o == NULL) {
264 // Couldn't allocate heap memory; use local data instead.
265 // Unfortunately, we won't be able to format the string...
266 o = &mp_emergency_exception_obj;
267 o->base.type = exc_type;
268 o->traceback = MP_OBJ_NULL;
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300269 o->args = mp_const_empty_tuple;
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200270 } else {
Damien Georgef0954e32014-04-10 14:38:25 +0100271 o->base.type = exc_type;
272 o->traceback = MP_OBJ_NULL;
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300273 o->args = mp_obj_new_tuple(1, NULL);
Damien Georgef0954e32014-04-10 14:38:25 +0100274
275 if (fmt == NULL) {
276 // no message
277 assert(0);
278 } else {
279 // render exception message and store as .args[0]
280 // TODO: optimize bufferbloat
281 vstr_t *vstr = vstr_new();
282 va_list ap;
283 va_start(ap, fmt);
284 vstr_vprintf(vstr, fmt, ap);
285 va_end(ap);
Damien George2617eeb2014-05-25 22:27:57 +0100286 o->args->items[0] = mp_obj_new_str(vstr->buf, vstr->len, false);
Damien Georgef0954e32014-04-10 14:38:25 +0100287 vstr_free(vstr);
288 }
Damien George6c73ca12014-01-08 18:11:23 +0000289 }
Damien George6c73ca12014-01-08 18:11:23 +0000290
291 return o;
292}
293
Damien Georgec5966122014-02-15 16:10:44 +0000294// return true if the given object is an exception type
Damien Georgec5966122014-02-15 16:10:44 +0000295bool mp_obj_is_exception_type(mp_obj_t self_in) {
296 if (MP_OBJ_IS_TYPE(self_in, &mp_type_type)) {
Damien George9e6e9352014-03-26 18:37:06 +0000297 // optimisation when self_in is a builtin exception
Damien Georgec5966122014-02-15 16:10:44 +0000298 mp_obj_type_t *self = self_in;
Damien George9e6e9352014-03-26 18:37:06 +0000299 if (self->make_new == mp_obj_exception_make_new) {
300 return true;
301 }
Damien Georgec5966122014-02-15 16:10:44 +0000302 }
Damien George9e6e9352014-03-26 18:37:06 +0000303 return mp_obj_is_subclass_fast(self_in, &mp_type_BaseException);
Damien Georgec5966122014-02-15 16:10:44 +0000304}
305
306// return true if the given object is an instance of an exception type
Damien Georgec5966122014-02-15 16:10:44 +0000307bool mp_obj_is_exception_instance(mp_obj_t self_in) {
Damien George9e6e9352014-03-26 18:37:06 +0000308 return mp_obj_is_exception_type(mp_obj_get_type(self_in));
Damien Georgec5966122014-02-15 16:10:44 +0000309}
310
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200311// return true if exception (type or instance) is a subclass of given
312// exception type.
313bool mp_obj_exception_match(mp_obj_t exc, const mp_obj_type_t *exc_type) {
314 // TODO: move implementation from RT_BINARY_OP_EXCEPTION_MATCH here.
Damien Georged17926d2014-03-30 13:35:08 +0100315 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 +0200316}
317
Paul Sokolovsky91e556a2014-05-02 02:27:00 +0300318// traceback handling functions
Damien Georgec5966122014-02-15 16:10:44 +0000319
Paul Sokolovsky91e556a2014-05-02 02:27:00 +0300320#define GET_NATIVE_EXCEPTION(self, self_in) \
321 /* make sure self_in is an exception instance */ \
322 assert(mp_obj_is_exception_instance(self_in)); \
323 mp_obj_exception_t *self; \
324 if (mp_obj_is_native_exception_instance(self_in)) { \
325 self = self_in; \
326 } else { \
327 self = ((mp_obj_instance_t*)self_in)->subobj[0]; \
Damien George9e6e9352014-03-26 18:37:06 +0000328 }
Paul Sokolovsky91e556a2014-05-02 02:27:00 +0300329
330void mp_obj_exception_clear_traceback(mp_obj_t self_in) {
331 GET_NATIVE_EXCEPTION(self, self_in);
332 // just set the traceback to the null object
333 // we don't want to call any memory management functions here
334 self->traceback = MP_OBJ_NULL;
Damienb86e3f92013-12-29 17:17:43 +0000335}
Damien George08335002014-01-18 23:24:36 +0000336
Damien George136b1492014-01-19 12:38:49 +0000337void 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 +0300338 GET_NATIVE_EXCEPTION(self, self_in);
Damien Georgec5966122014-02-15 16:10:44 +0000339
Paul Sokolovsky91e556a2014-05-02 02:27:00 +0300340 // for traceback, we are just using the list object for convenience, it's not really a list of Python objects
341 if (self->traceback == MP_OBJ_NULL) {
342 self->traceback = mp_obj_new_list(0, NULL);
Damien George08335002014-01-18 23:24:36 +0000343 }
Paul Sokolovsky91e556a2014-05-02 02:27:00 +0300344 mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)file);
345 mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)line);
346 mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)block);
Damien George08335002014-01-18 23:24:36 +0000347}
348
Damien George136b1492014-01-19 12:38:49 +0000349void 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 +0300350 GET_NATIVE_EXCEPTION(self, self_in);
Damien Georgec5966122014-02-15 16:10:44 +0000351
Damien George136b1492014-01-19 12:38:49 +0000352 if (self->traceback == MP_OBJ_NULL) {
353 *n = 0;
354 *values = NULL;
355 } else {
356 uint n2;
357 mp_obj_list_get(self->traceback, &n2, (mp_obj_t**)values);
358 *n = n2;
359 }
Damien George08335002014-01-18 23:24:36 +0000360}