blob: ad66bb50febfa8e8087c5e950b44aafdfa028f52 [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"
Dave Hylands2fe841d2014-06-30 22:49:21 -070040#include "gc.h"
Damiend99b0522013-12-21 18:17:45 +000041
Damien George07ddab52014-03-29 13:15:08 +000042typedef struct _mp_obj_exception_t {
Damiend99b0522013-12-21 18:17:45 +000043 mp_obj_base_t base;
Damien George136b1492014-01-19 12:38:49 +000044 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 +030045 mp_obj_tuple_t *args;
Damiend99b0522013-12-21 18:17:45 +000046} mp_obj_exception_t;
47
Damien George6902eed2014-04-04 10:52:59 +000048// Instance of MemoryError exception - needed by mp_malloc_fail
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +030049const 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 +000050
Damien Georgef0954e32014-04-10 14:38:25 +010051// Local non-heap memory for allocating an exception when we run out of RAM
52STATIC mp_obj_exception_t mp_emergency_exception_obj;
53
Paul Sokolovskyc4d589e2014-03-28 02:37:28 +020054// Instance of GeneratorExit exception - needed by generator.close()
55// This would belong to objgenerator.c, but to keep mp_obj_exception_t
56// definition module-private so far, have it here.
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +030057const 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 +020058
Damien Georgec5966122014-02-15 16:10:44 +000059STATIC 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 +000060 mp_obj_exception_t *o = o_in;
Paul Sokolovskyd8351ca2014-05-02 01:51:25 +030061 mp_print_kind_t k = kind & ~PRINT_EXC_SUBCLASS;
62 bool is_subclass = kind & PRINT_EXC_SUBCLASS;
63 if (!is_subclass && (k == PRINT_REPR || k == PRINT_EXC)) {
Paul Sokolovskya96d3d02014-03-31 01:10:10 +030064 print(env, "%s", qstr_str(o->base.type->name));
Damiend99b0522013-12-21 18:17:45 +000065 }
Paul Sokolovskyd8351ca2014-05-02 01:51:25 +030066
67 if (k == PRINT_EXC) {
68 print(env, ": ");
69 }
70
71 if (k == PRINT_STR || k == PRINT_EXC) {
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +030072 if (o->args == NULL || o->args->len == 0) {
Paul Sokolovskya96d3d02014-03-31 01:10:10 +030073 print(env, "");
74 return;
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +030075 } else if (o->args->len == 1) {
76 mp_obj_print_helper(print, env, o->args->items[0], PRINT_STR);
Paul Sokolovskya96d3d02014-03-31 01:10:10 +030077 return;
78 }
79 }
Damien George2323ef92014-05-11 18:00:45 +010080 mp_obj_tuple_print(print, env, o->args, kind);
Damiend99b0522013-12-21 18:17:45 +000081}
82
Paul Sokolovskyd8351ca2014-05-02 01:51:25 +030083mp_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 +000084 mp_obj_type_t *type = type_in;
Damien George20006db2014-01-18 14:10:48 +000085
86 if (n_kw != 0) {
Damien Georgeea13f402014-04-05 18:32:08 +010087 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 +000088 }
89
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +030090 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 +010091 if (o == NULL) {
92 // Couldn't allocate heap memory; use local data instead.
93 o = &mp_emergency_exception_obj;
94 // We can't store any args.
95 n_args = 0;
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +030096 o->args = mp_const_empty_tuple;
97 } else {
98 o->args = mp_obj_new_tuple(n_args, args);
Damien Georgef0954e32014-04-10 14:38:25 +010099 }
Damien Georgec5966122014-02-15 16:10:44 +0000100 o->base.type = type;
Paul Sokolovsky60a0d3f2014-02-14 21:01:12 +0200101 o->traceback = MP_OBJ_NULL;
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200102 return o;
103}
104
Paul Sokolovskyaf1ae302014-03-26 19:17:20 +0200105// Get exception "value" - that is, first argument, or None
106mp_obj_t mp_obj_exception_get_value(mp_obj_t self_in) {
107 mp_obj_exception_t *self = self_in;
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300108 if (self->args->len == 0) {
Paul Sokolovskyaf1ae302014-03-26 19:17:20 +0200109 return mp_const_none;
110 } else {
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300111 return self->args->items[0];
Paul Sokolovskyaf1ae302014-03-26 19:17:20 +0200112 }
113}
114
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +0200115STATIC void exception_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
116 mp_obj_exception_t *self = self_in;
117 if (attr == MP_QSTR_args) {
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300118 dest[0] = self->args;
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +0200119 } else if (self->base.type == &mp_type_StopIteration && attr == MP_QSTR_value) {
Paul Sokolovskyaf1ae302014-03-26 19:17:20 +0200120 dest[0] = mp_obj_exception_get_value(self);
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +0200121 }
122}
123
Paul Sokolovsky52386ca2014-05-18 20:44:04 +0300124STATIC mp_obj_t exc___init__(uint n_args, const mp_obj_t *args) {
125 mp_obj_exception_t *self = args[0];
126 mp_obj_t argst = mp_obj_new_tuple(n_args - 1, args + 1);
127 self->args = argst;
128 return mp_const_none;
129}
130STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(exc___init___obj, 1, MP_OBJ_FUN_ARGS_MAX, exc___init__);
131
132STATIC const mp_map_elem_t exc_locals_dict_table[] = {
133 { MP_OBJ_NEW_QSTR(MP_QSTR___init__), (mp_obj_t)&exc___init___obj },
134};
135
136STATIC MP_DEFINE_CONST_DICT(exc_locals_dict, exc_locals_dict_table);
137
Damien Georgec5966122014-02-15 16:10:44 +0000138const mp_obj_type_t mp_type_BaseException = {
139 { &mp_type_type },
140 .name = MP_QSTR_BaseException,
141 .print = mp_obj_exception_print,
142 .make_new = mp_obj_exception_make_new,
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +0200143 .load_attr = exception_load_attr,
Paul Sokolovsky52386ca2014-05-18 20:44:04 +0300144 .locals_dict = (mp_obj_t)&exc_locals_dict,
Damiend99b0522013-12-21 18:17:45 +0000145};
146
Damien George22a08652014-02-15 21:05:25 +0000147#define MP_DEFINE_EXCEPTION_BASE(base_name) \
Damien George07ddab52014-03-29 13:15:08 +0000148STATIC 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 +0000149
150#define MP_DEFINE_EXCEPTION(exc_name, base_name) \
Damien Georgec5966122014-02-15 16:10:44 +0000151const mp_obj_type_t mp_type_ ## exc_name = { \
152 { &mp_type_type }, \
153 .name = MP_QSTR_ ## exc_name, \
154 .print = mp_obj_exception_print, \
155 .make_new = mp_obj_exception_make_new, \
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +0200156 .load_attr = exception_load_attr, \
Damien George22a08652014-02-15 21:05:25 +0000157 .bases_tuple = (mp_obj_t)&mp_type_ ## base_name ## _base_tuple, \
Damien Georgec5966122014-02-15 16:10:44 +0000158};
159
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000160// List of all exceptions, arranged as in the table at:
Chris Angelicodaf973a2014-06-06 03:51:03 +1000161// http://docs.python.org/3/library/exceptions.html
Damien George22a08652014-02-15 21:05:25 +0000162MP_DEFINE_EXCEPTION_BASE(BaseException)
Damien George7a4ddd22014-05-24 23:32:19 +0100163MP_DEFINE_EXCEPTION(SystemExit, BaseException)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000164//MP_DEFINE_EXCEPTION(KeyboardInterrupt, BaseException)
165MP_DEFINE_EXCEPTION(GeneratorExit, BaseException)
166MP_DEFINE_EXCEPTION(Exception, BaseException)
167 MP_DEFINE_EXCEPTION_BASE(Exception)
168 MP_DEFINE_EXCEPTION(StopIteration, Exception)
169 MP_DEFINE_EXCEPTION(ArithmeticError, Exception)
170 MP_DEFINE_EXCEPTION_BASE(ArithmeticError)
Damien Georgec63f9842014-03-27 23:49:06 +0000171 //MP_DEFINE_EXCEPTION(FloatingPointError, ArithmeticError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000172 MP_DEFINE_EXCEPTION(OverflowError, ArithmeticError)
173 MP_DEFINE_EXCEPTION(ZeroDivisionError, ArithmeticError)
174 MP_DEFINE_EXCEPTION(AssertionError, Exception)
175 MP_DEFINE_EXCEPTION(AttributeError, Exception)
Damien Georgec63f9842014-03-27 23:49:06 +0000176 //MP_DEFINE_EXCEPTION(BufferError, Exception)
Damien Georgeffb5cfc2014-03-25 14:29:40 +0000177 //MP_DEFINE_EXCEPTION(EnvironmentError, Exception)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000178 MP_DEFINE_EXCEPTION(EOFError, Exception)
179 MP_DEFINE_EXCEPTION(ImportError, Exception)
180 MP_DEFINE_EXCEPTION(IOError, Exception)
181 MP_DEFINE_EXCEPTION(LookupError, Exception)
182 MP_DEFINE_EXCEPTION_BASE(LookupError)
183 MP_DEFINE_EXCEPTION(IndexError, LookupError)
184 MP_DEFINE_EXCEPTION(KeyError, LookupError)
185 MP_DEFINE_EXCEPTION(MemoryError, Exception)
186 MP_DEFINE_EXCEPTION(NameError, Exception)
Damien Georgec63f9842014-03-27 23:49:06 +0000187 /*
Antonin ENFRUNda1fffa2014-05-12 00:21:50 +0200188 MP_DEFINE_EXCEPTION_BASE(NameError)
189 MP_DEFINE_EXCEPTION(UnboundLocalError, NameError)
190 */
191 MP_DEFINE_EXCEPTION(OSError, Exception)
192 /*
193 MP_DEFINE_EXCEPTION_BASE(OSError)
Damien Georgec63f9842014-03-27 23:49:06 +0000194 MP_DEFINE_EXCEPTION(BlockingIOError, OSError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000195 MP_DEFINE_EXCEPTION(ChildProcessError, OSError)
196 MP_DEFINE_EXCEPTION(ConnectionError, OSError)
197 MP_DEFINE_EXCEPTION(BrokenPipeError, ConnectionError)
198 MP_DEFINE_EXCEPTION(ConnectionAbortedError, ConnectionError)
199 MP_DEFINE_EXCEPTION(ConnectionRefusedError, ConnectionError)
200 MP_DEFINE_EXCEPTION(ConnectionResetError, ConnectionError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000201 MP_DEFINE_EXCEPTION(InterruptedError, OSError)
202 MP_DEFINE_EXCEPTION(IsADirectoryError, OSError)
203 MP_DEFINE_EXCEPTION(NotADirectoryError, OSError)
204 MP_DEFINE_EXCEPTION(PermissionError, OSError)
205 MP_DEFINE_EXCEPTION(ProcessLookupError, OSError)
Damien Georgec63f9842014-03-27 23:49:06 +0000206 MP_DEFINE_EXCEPTION(TimeoutError, OSError)
Damien Georgec9109722014-03-22 23:40:02 +0000207 MP_DEFINE_EXCEPTION(FileExistsError, OSError)
208 MP_DEFINE_EXCEPTION(FileNotFoundError, OSError)
Damien Georgec63f9842014-03-27 23:49:06 +0000209 MP_DEFINE_EXCEPTION(ReferenceError, Exception)
210 */
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000211 MP_DEFINE_EXCEPTION(RuntimeError, Exception)
212 MP_DEFINE_EXCEPTION_BASE(RuntimeError)
213 MP_DEFINE_EXCEPTION(NotImplementedError, RuntimeError)
214 MP_DEFINE_EXCEPTION(SyntaxError, Exception)
215 MP_DEFINE_EXCEPTION_BASE(SyntaxError)
216 MP_DEFINE_EXCEPTION(IndentationError, SyntaxError)
Damien Georgec63f9842014-03-27 23:49:06 +0000217 /*
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000218 MP_DEFINE_EXCEPTION_BASE(IndentationError)
Damien Georgec63f9842014-03-27 23:49:06 +0000219 MP_DEFINE_EXCEPTION(TabError, IndentationError)
220 */
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000221 MP_DEFINE_EXCEPTION(SystemError, Exception)
222 MP_DEFINE_EXCEPTION(TypeError, Exception)
223 MP_DEFINE_EXCEPTION(ValueError, Exception)
224 //TODO: Implement UnicodeErrors which take arguments
Damien Georgec9109722014-03-22 23:40:02 +0000225 /*
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000226 MP_DEFINE_EXCEPTION(Warning, Exception)
227 MP_DEFINE_EXCEPTION_BASE(Warning)
228 MP_DEFINE_EXCEPTION(DeprecationWarning, Warning)
229 MP_DEFINE_EXCEPTION(PendingDeprecationWarning, Warning)
230 MP_DEFINE_EXCEPTION(RuntimeWarning, Warning)
231 MP_DEFINE_EXCEPTION(SyntaxWarning, Warning)
232 MP_DEFINE_EXCEPTION(UserWarning, Warning)
233 MP_DEFINE_EXCEPTION(FutureWarning, Warning)
234 MP_DEFINE_EXCEPTION(ImportWarning, Warning)
235 MP_DEFINE_EXCEPTION(UnicodeWarning, Warning)
236 MP_DEFINE_EXCEPTION(BytesWarning, Warning)
237 MP_DEFINE_EXCEPTION(ResourceWarning, Warning)
Damien Georgec9109722014-03-22 23:40:02 +0000238 */
Damien Georgec5966122014-02-15 16:10:44 +0000239
240mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type) {
Paul Sokolovskya96d3d02014-03-31 01:10:10 +0300241 return mp_obj_new_exception_args(exc_type, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000242}
243
Paul Sokolovskydec31bb2014-04-22 00:01:13 +0300244// "Optimized" version for common(?) case of having 1 exception arg
245mp_obj_t mp_obj_new_exception_arg1(const mp_obj_type_t *exc_type, mp_obj_t arg) {
246 return mp_obj_new_exception_args(exc_type, 1, &arg);
247}
248
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200249mp_obj_t mp_obj_new_exception_args(const mp_obj_type_t *exc_type, uint n_args, const mp_obj_t *args) {
250 assert(exc_type->make_new == mp_obj_exception_make_new);
251 return exc_type->make_new((mp_obj_t)exc_type, n_args, 0, args);
252}
253
Damien Georgec5966122014-02-15 16:10:44 +0000254mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, const char *msg) {
255 return mp_obj_new_exception_msg_varg(exc_type, msg);
Damiend99b0522013-12-21 18:17:45 +0000256}
257
Damien Georgec5966122014-02-15 16:10:44 +0000258mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char *fmt, ...) {
259 // check that the given type is an exception type
260 assert(exc_type->make_new == mp_obj_exception_make_new);
261
Damien George6c73ca12014-01-08 18:11:23 +0000262 // make exception object
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300263 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 +0100264 if (o == NULL) {
265 // Couldn't allocate heap memory; use local data instead.
266 // Unfortunately, we won't be able to format the string...
267 o = &mp_emergency_exception_obj;
268 o->base.type = exc_type;
269 o->traceback = MP_OBJ_NULL;
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300270 o->args = mp_const_empty_tuple;
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200271 } else {
Damien Georgef0954e32014-04-10 14:38:25 +0100272 o->base.type = exc_type;
273 o->traceback = MP_OBJ_NULL;
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300274 o->args = mp_obj_new_tuple(1, NULL);
Damien Georgef0954e32014-04-10 14:38:25 +0100275
276 if (fmt == NULL) {
277 // no message
278 assert(0);
279 } else {
280 // render exception message and store as .args[0]
281 // TODO: optimize bufferbloat
282 vstr_t *vstr = vstr_new();
283 va_list ap;
284 va_start(ap, fmt);
285 vstr_vprintf(vstr, fmt, ap);
286 va_end(ap);
Damien George2617eeb2014-05-25 22:27:57 +0100287 o->args->items[0] = mp_obj_new_str(vstr->buf, vstr->len, false);
Damien Georgef0954e32014-04-10 14:38:25 +0100288 vstr_free(vstr);
289 }
Damien George6c73ca12014-01-08 18:11:23 +0000290 }
Damien George6c73ca12014-01-08 18:11:23 +0000291
292 return o;
293}
294
Damien Georgec5966122014-02-15 16:10:44 +0000295// return true if the given object is an exception type
Damien Georgec5966122014-02-15 16:10:44 +0000296bool mp_obj_is_exception_type(mp_obj_t self_in) {
297 if (MP_OBJ_IS_TYPE(self_in, &mp_type_type)) {
Damien George9e6e9352014-03-26 18:37:06 +0000298 // optimisation when self_in is a builtin exception
Damien Georgec5966122014-02-15 16:10:44 +0000299 mp_obj_type_t *self = self_in;
Damien George9e6e9352014-03-26 18:37:06 +0000300 if (self->make_new == mp_obj_exception_make_new) {
301 return true;
302 }
Damien Georgec5966122014-02-15 16:10:44 +0000303 }
Damien George9e6e9352014-03-26 18:37:06 +0000304 return mp_obj_is_subclass_fast(self_in, &mp_type_BaseException);
Damien Georgec5966122014-02-15 16:10:44 +0000305}
306
307// return true if the given object is an instance of an exception type
Damien Georgec5966122014-02-15 16:10:44 +0000308bool mp_obj_is_exception_instance(mp_obj_t self_in) {
Damien George9e6e9352014-03-26 18:37:06 +0000309 return mp_obj_is_exception_type(mp_obj_get_type(self_in));
Damien Georgec5966122014-02-15 16:10:44 +0000310}
311
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200312// return true if exception (type or instance) is a subclass of given
313// exception type.
314bool mp_obj_exception_match(mp_obj_t exc, const mp_obj_type_t *exc_type) {
315 // TODO: move implementation from RT_BINARY_OP_EXCEPTION_MATCH here.
Damien Georged17926d2014-03-30 13:35:08 +0100316 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 +0200317}
318
Paul Sokolovsky91e556a2014-05-02 02:27:00 +0300319// traceback handling functions
Damien Georgec5966122014-02-15 16:10:44 +0000320
Paul Sokolovsky91e556a2014-05-02 02:27:00 +0300321#define GET_NATIVE_EXCEPTION(self, self_in) \
322 /* make sure self_in is an exception instance */ \
323 assert(mp_obj_is_exception_instance(self_in)); \
324 mp_obj_exception_t *self; \
325 if (mp_obj_is_native_exception_instance(self_in)) { \
326 self = self_in; \
327 } else { \
328 self = ((mp_obj_instance_t*)self_in)->subobj[0]; \
Damien George9e6e9352014-03-26 18:37:06 +0000329 }
Paul Sokolovsky91e556a2014-05-02 02:27:00 +0300330
331void mp_obj_exception_clear_traceback(mp_obj_t self_in) {
332 GET_NATIVE_EXCEPTION(self, self_in);
333 // just set the traceback to the null object
334 // we don't want to call any memory management functions here
335 self->traceback = MP_OBJ_NULL;
Damienb86e3f92013-12-29 17:17:43 +0000336}
Damien George08335002014-01-18 23:24:36 +0000337
Damien George136b1492014-01-19 12:38:49 +0000338void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, machine_uint_t line, qstr block) {
Damien Georgef0b29722014-07-01 14:28:09 +0100339 #if MICROPY_ENABLE_GC
Dave Hylands2fe841d2014-06-30 22:49:21 -0700340 if (gc_is_locked()) {
341 // We can't allocate memory, so don't bother to try
342 return;
343 }
Damien Georgef0b29722014-07-01 14:28:09 +0100344 #endif
345
Paul Sokolovsky91e556a2014-05-02 02:27:00 +0300346 GET_NATIVE_EXCEPTION(self, self_in);
Damien Georgec5966122014-02-15 16:10:44 +0000347
Paul Sokolovsky91e556a2014-05-02 02:27:00 +0300348 // for traceback, we are just using the list object for convenience, it's not really a list of Python objects
349 if (self->traceback == MP_OBJ_NULL) {
350 self->traceback = mp_obj_new_list(0, NULL);
Damien George08335002014-01-18 23:24:36 +0000351 }
Paul Sokolovsky91e556a2014-05-02 02:27:00 +0300352 mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)file);
353 mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)line);
354 mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)block);
Damien George08335002014-01-18 23:24:36 +0000355}
356
Damien George136b1492014-01-19 12:38:49 +0000357void 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 +0300358 GET_NATIVE_EXCEPTION(self, self_in);
Damien Georgec5966122014-02-15 16:10:44 +0000359
Damien George136b1492014-01-19 12:38:49 +0000360 if (self->traceback == MP_OBJ_NULL) {
361 *n = 0;
362 *values = NULL;
363 } else {
364 uint n2;
365 mp_obj_list_get(self->traceback, &n2, (mp_obj_t**)values);
366 *n = n2;
367 }
Damien George08335002014-01-18 23:24:36 +0000368}