blob: 7f5f9c73d2478b721214f5a470dc7f214b68b22a [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
Damien Georgec5966122014-02-15 16:10:44 +0000123const mp_obj_type_t mp_type_BaseException = {
124 { &mp_type_type },
125 .name = MP_QSTR_BaseException,
126 .print = mp_obj_exception_print,
127 .make_new = mp_obj_exception_make_new,
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +0200128 .load_attr = exception_load_attr,
Damiend99b0522013-12-21 18:17:45 +0000129};
130
Damien George22a08652014-02-15 21:05:25 +0000131#define MP_DEFINE_EXCEPTION_BASE(base_name) \
Damien George07ddab52014-03-29 13:15:08 +0000132STATIC 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 +0000133
134#define MP_DEFINE_EXCEPTION(exc_name, base_name) \
Damien Georgec5966122014-02-15 16:10:44 +0000135const mp_obj_type_t mp_type_ ## exc_name = { \
136 { &mp_type_type }, \
137 .name = MP_QSTR_ ## exc_name, \
138 .print = mp_obj_exception_print, \
139 .make_new = mp_obj_exception_make_new, \
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +0200140 .load_attr = exception_load_attr, \
Damien George22a08652014-02-15 21:05:25 +0000141 .bases_tuple = (mp_obj_t)&mp_type_ ## base_name ## _base_tuple, \
Damien Georgec5966122014-02-15 16:10:44 +0000142};
143
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000144// List of all exceptions, arranged as in the table at:
145// http://docs.python.org/3.3/library/exceptions.html
Damien George22a08652014-02-15 21:05:25 +0000146MP_DEFINE_EXCEPTION_BASE(BaseException)
Damien Georgeffb5cfc2014-03-25 14:29:40 +0000147//MP_DEFINE_EXCEPTION(SystemExit, BaseException)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000148//MP_DEFINE_EXCEPTION(KeyboardInterrupt, BaseException)
149MP_DEFINE_EXCEPTION(GeneratorExit, BaseException)
150MP_DEFINE_EXCEPTION(Exception, BaseException)
151 MP_DEFINE_EXCEPTION_BASE(Exception)
152 MP_DEFINE_EXCEPTION(StopIteration, Exception)
153 MP_DEFINE_EXCEPTION(ArithmeticError, Exception)
154 MP_DEFINE_EXCEPTION_BASE(ArithmeticError)
Damien Georgec63f9842014-03-27 23:49:06 +0000155 //MP_DEFINE_EXCEPTION(FloatingPointError, ArithmeticError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000156 MP_DEFINE_EXCEPTION(OverflowError, ArithmeticError)
157 MP_DEFINE_EXCEPTION(ZeroDivisionError, ArithmeticError)
158 MP_DEFINE_EXCEPTION(AssertionError, Exception)
159 MP_DEFINE_EXCEPTION(AttributeError, Exception)
Damien Georgec63f9842014-03-27 23:49:06 +0000160 //MP_DEFINE_EXCEPTION(BufferError, Exception)
Damien Georgeffb5cfc2014-03-25 14:29:40 +0000161 //MP_DEFINE_EXCEPTION(EnvironmentError, Exception)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000162 MP_DEFINE_EXCEPTION(EOFError, Exception)
163 MP_DEFINE_EXCEPTION(ImportError, Exception)
164 MP_DEFINE_EXCEPTION(IOError, Exception)
165 MP_DEFINE_EXCEPTION(LookupError, Exception)
166 MP_DEFINE_EXCEPTION_BASE(LookupError)
167 MP_DEFINE_EXCEPTION(IndexError, LookupError)
168 MP_DEFINE_EXCEPTION(KeyError, LookupError)
169 MP_DEFINE_EXCEPTION(MemoryError, Exception)
170 MP_DEFINE_EXCEPTION(NameError, Exception)
Damien Georgec63f9842014-03-27 23:49:06 +0000171 /*
Antonin ENFRUNda1fffa2014-05-12 00:21:50 +0200172 MP_DEFINE_EXCEPTION_BASE(NameError)
173 MP_DEFINE_EXCEPTION(UnboundLocalError, NameError)
174 */
175 MP_DEFINE_EXCEPTION(OSError, Exception)
176 /*
177 MP_DEFINE_EXCEPTION_BASE(OSError)
Damien Georgec63f9842014-03-27 23:49:06 +0000178 MP_DEFINE_EXCEPTION(BlockingIOError, OSError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000179 MP_DEFINE_EXCEPTION(ChildProcessError, OSError)
180 MP_DEFINE_EXCEPTION(ConnectionError, OSError)
181 MP_DEFINE_EXCEPTION(BrokenPipeError, ConnectionError)
182 MP_DEFINE_EXCEPTION(ConnectionAbortedError, ConnectionError)
183 MP_DEFINE_EXCEPTION(ConnectionRefusedError, ConnectionError)
184 MP_DEFINE_EXCEPTION(ConnectionResetError, ConnectionError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000185 MP_DEFINE_EXCEPTION(InterruptedError, OSError)
186 MP_DEFINE_EXCEPTION(IsADirectoryError, OSError)
187 MP_DEFINE_EXCEPTION(NotADirectoryError, OSError)
188 MP_DEFINE_EXCEPTION(PermissionError, OSError)
189 MP_DEFINE_EXCEPTION(ProcessLookupError, OSError)
Damien Georgec63f9842014-03-27 23:49:06 +0000190 MP_DEFINE_EXCEPTION(TimeoutError, OSError)
Damien Georgec9109722014-03-22 23:40:02 +0000191 MP_DEFINE_EXCEPTION(FileExistsError, OSError)
192 MP_DEFINE_EXCEPTION(FileNotFoundError, OSError)
Damien Georgec63f9842014-03-27 23:49:06 +0000193 MP_DEFINE_EXCEPTION(ReferenceError, Exception)
194 */
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000195 MP_DEFINE_EXCEPTION(RuntimeError, Exception)
196 MP_DEFINE_EXCEPTION_BASE(RuntimeError)
197 MP_DEFINE_EXCEPTION(NotImplementedError, RuntimeError)
198 MP_DEFINE_EXCEPTION(SyntaxError, Exception)
199 MP_DEFINE_EXCEPTION_BASE(SyntaxError)
200 MP_DEFINE_EXCEPTION(IndentationError, SyntaxError)
Damien Georgec63f9842014-03-27 23:49:06 +0000201 /*
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000202 MP_DEFINE_EXCEPTION_BASE(IndentationError)
Damien Georgec63f9842014-03-27 23:49:06 +0000203 MP_DEFINE_EXCEPTION(TabError, IndentationError)
204 */
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000205 MP_DEFINE_EXCEPTION(SystemError, Exception)
206 MP_DEFINE_EXCEPTION(TypeError, Exception)
207 MP_DEFINE_EXCEPTION(ValueError, Exception)
208 //TODO: Implement UnicodeErrors which take arguments
Damien Georgec9109722014-03-22 23:40:02 +0000209 /*
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000210 MP_DEFINE_EXCEPTION(Warning, Exception)
211 MP_DEFINE_EXCEPTION_BASE(Warning)
212 MP_DEFINE_EXCEPTION(DeprecationWarning, Warning)
213 MP_DEFINE_EXCEPTION(PendingDeprecationWarning, Warning)
214 MP_DEFINE_EXCEPTION(RuntimeWarning, Warning)
215 MP_DEFINE_EXCEPTION(SyntaxWarning, Warning)
216 MP_DEFINE_EXCEPTION(UserWarning, Warning)
217 MP_DEFINE_EXCEPTION(FutureWarning, Warning)
218 MP_DEFINE_EXCEPTION(ImportWarning, Warning)
219 MP_DEFINE_EXCEPTION(UnicodeWarning, Warning)
220 MP_DEFINE_EXCEPTION(BytesWarning, Warning)
221 MP_DEFINE_EXCEPTION(ResourceWarning, Warning)
Damien Georgec9109722014-03-22 23:40:02 +0000222 */
Damien Georgec5966122014-02-15 16:10:44 +0000223
224mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type) {
Paul Sokolovskya96d3d02014-03-31 01:10:10 +0300225 return mp_obj_new_exception_args(exc_type, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000226}
227
Paul Sokolovskydec31bb2014-04-22 00:01:13 +0300228// "Optimized" version for common(?) case of having 1 exception arg
229mp_obj_t mp_obj_new_exception_arg1(const mp_obj_type_t *exc_type, mp_obj_t arg) {
230 return mp_obj_new_exception_args(exc_type, 1, &arg);
231}
232
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200233mp_obj_t mp_obj_new_exception_args(const mp_obj_type_t *exc_type, uint n_args, const mp_obj_t *args) {
234 assert(exc_type->make_new == mp_obj_exception_make_new);
235 return exc_type->make_new((mp_obj_t)exc_type, n_args, 0, args);
236}
237
Damien Georgec5966122014-02-15 16:10:44 +0000238mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, const char *msg) {
239 return mp_obj_new_exception_msg_varg(exc_type, msg);
Damiend99b0522013-12-21 18:17:45 +0000240}
241
Damien Georgec5966122014-02-15 16:10:44 +0000242mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char *fmt, ...) {
243 // check that the given type is an exception type
244 assert(exc_type->make_new == mp_obj_exception_make_new);
245
Damien George6c73ca12014-01-08 18:11:23 +0000246 // make exception object
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300247 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 +0100248 if (o == NULL) {
249 // Couldn't allocate heap memory; use local data instead.
250 // Unfortunately, we won't be able to format the string...
251 o = &mp_emergency_exception_obj;
252 o->base.type = exc_type;
253 o->traceback = MP_OBJ_NULL;
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300254 o->args = mp_const_empty_tuple;
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200255 } else {
Damien Georgef0954e32014-04-10 14:38:25 +0100256 o->base.type = exc_type;
257 o->traceback = MP_OBJ_NULL;
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300258 o->args = mp_obj_new_tuple(1, NULL);
Damien Georgef0954e32014-04-10 14:38:25 +0100259
260 if (fmt == NULL) {
261 // no message
262 assert(0);
263 } else {
264 // render exception message and store as .args[0]
265 // TODO: optimize bufferbloat
266 vstr_t *vstr = vstr_new();
267 va_list ap;
268 va_start(ap, fmt);
269 vstr_vprintf(vstr, fmt, ap);
270 va_end(ap);
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300271 o->args->items[0] = mp_obj_new_str((byte*)vstr->buf, vstr->len, false);
Damien Georgef0954e32014-04-10 14:38:25 +0100272 vstr_free(vstr);
273 }
Damien George6c73ca12014-01-08 18:11:23 +0000274 }
Damien George6c73ca12014-01-08 18:11:23 +0000275
276 return o;
277}
278
Damien Georgec5966122014-02-15 16:10:44 +0000279// return true if the given object is an exception type
Damien Georgec5966122014-02-15 16:10:44 +0000280bool mp_obj_is_exception_type(mp_obj_t self_in) {
281 if (MP_OBJ_IS_TYPE(self_in, &mp_type_type)) {
Damien George9e6e9352014-03-26 18:37:06 +0000282 // optimisation when self_in is a builtin exception
Damien Georgec5966122014-02-15 16:10:44 +0000283 mp_obj_type_t *self = self_in;
Damien George9e6e9352014-03-26 18:37:06 +0000284 if (self->make_new == mp_obj_exception_make_new) {
285 return true;
286 }
Damien Georgec5966122014-02-15 16:10:44 +0000287 }
Damien George9e6e9352014-03-26 18:37:06 +0000288 return mp_obj_is_subclass_fast(self_in, &mp_type_BaseException);
Damien Georgec5966122014-02-15 16:10:44 +0000289}
290
291// return true if the given object is an instance of an exception type
Damien Georgec5966122014-02-15 16:10:44 +0000292bool mp_obj_is_exception_instance(mp_obj_t self_in) {
Damien George9e6e9352014-03-26 18:37:06 +0000293 return mp_obj_is_exception_type(mp_obj_get_type(self_in));
Damien Georgec5966122014-02-15 16:10:44 +0000294}
295
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200296// return true if exception (type or instance) is a subclass of given
297// exception type.
298bool mp_obj_exception_match(mp_obj_t exc, const mp_obj_type_t *exc_type) {
299 // TODO: move implementation from RT_BINARY_OP_EXCEPTION_MATCH here.
Damien Georged17926d2014-03-30 13:35:08 +0100300 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 +0200301}
302
Paul Sokolovsky91e556a2014-05-02 02:27:00 +0300303// traceback handling functions
Damien Georgec5966122014-02-15 16:10:44 +0000304
Paul Sokolovsky91e556a2014-05-02 02:27:00 +0300305#define GET_NATIVE_EXCEPTION(self, self_in) \
306 /* make sure self_in is an exception instance */ \
307 assert(mp_obj_is_exception_instance(self_in)); \
308 mp_obj_exception_t *self; \
309 if (mp_obj_is_native_exception_instance(self_in)) { \
310 self = self_in; \
311 } else { \
312 self = ((mp_obj_instance_t*)self_in)->subobj[0]; \
Damien George9e6e9352014-03-26 18:37:06 +0000313 }
Paul Sokolovsky91e556a2014-05-02 02:27:00 +0300314
315void mp_obj_exception_clear_traceback(mp_obj_t self_in) {
316 GET_NATIVE_EXCEPTION(self, self_in);
317 // just set the traceback to the null object
318 // we don't want to call any memory management functions here
319 self->traceback = MP_OBJ_NULL;
Damienb86e3f92013-12-29 17:17:43 +0000320}
Damien George08335002014-01-18 23:24:36 +0000321
Damien George136b1492014-01-19 12:38:49 +0000322void 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 +0300323 GET_NATIVE_EXCEPTION(self, self_in);
Damien Georgec5966122014-02-15 16:10:44 +0000324
Paul Sokolovsky91e556a2014-05-02 02:27:00 +0300325 // for traceback, we are just using the list object for convenience, it's not really a list of Python objects
326 if (self->traceback == MP_OBJ_NULL) {
327 self->traceback = mp_obj_new_list(0, NULL);
Damien George08335002014-01-18 23:24:36 +0000328 }
Paul Sokolovsky91e556a2014-05-02 02:27:00 +0300329 mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)file);
330 mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)line);
331 mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)block);
Damien George08335002014-01-18 23:24:36 +0000332}
333
Damien George136b1492014-01-19 12:38:49 +0000334void 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 +0300335 GET_NATIVE_EXCEPTION(self, self_in);
Damien Georgec5966122014-02-15 16:10:44 +0000336
Damien George136b1492014-01-19 12:38:49 +0000337 if (self->traceback == MP_OBJ_NULL) {
338 *n = 0;
339 *values = NULL;
340 } else {
341 uint n2;
342 mp_obj_list_get(self->traceback, &n2, (mp_obj_t**)values);
343 *n = n2;
344 }
Damien George08335002014-01-18 23:24:36 +0000345}