blob: 8a008d1fff0cf6093d3bf0675854a0c14864951e [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>
Dave Hylands5b7fd202014-07-01 23:46:53 -070030#include <stdio.h>
Damiend99b0522013-12-21 18:17:45 +000031
Damien Georgeb4b10fd2015-01-01 23:30:53 +000032#include "py/mpstate.h"
Damien George51dfcb42015-01-01 20:27:54 +000033#include "py/objlist.h"
34#include "py/objstr.h"
35#include "py/objtuple.h"
36#include "py/objtype.h"
Damien George50149a52015-01-20 14:11:27 +000037#include "py/runtime.h"
Damien George51dfcb42015-01-01 20:27:54 +000038#include "py/gc.h"
Damiend99b0522013-12-21 18:17:45 +000039
Damien George6902eed2014-04-04 10:52:59 +000040// Instance of MemoryError exception - needed by mp_malloc_fail
Damien George4852e092015-02-27 00:36:39 +000041const mp_obj_exception_t mp_const_MemoryError_obj = {{&mp_type_MemoryError}, 0, 0, MP_OBJ_NULL, mp_const_empty_tuple};
Damien George6902eed2014-04-04 10:52:59 +000042
Dave Hylands5b7fd202014-07-01 23:46:53 -070043// Optionally allocated buffer for storing the first argument of an exception
44// allocated when the heap is locked.
45#if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF
46# if MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE > 0
Dave Hylands5b7fd202014-07-01 23:46:53 -070047#define mp_emergency_exception_buf_size MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE
48
49void mp_init_emergency_exception_buf(void) {
50 // Nothing to do since the buffer was declared statically. We put this
51 // definition here so that the calling code can call this function
52 // regardless of how its configured (makes the calling code a bit cleaner).
53}
54
55#else
Damien Georgeb4b10fd2015-01-01 23:30:53 +000056#define mp_emergency_exception_buf_size MP_STATE_VM(mp_emergency_exception_buf_size)
Dave Hylands5b7fd202014-07-01 23:46:53 -070057
58void mp_init_emergency_exception_buf(void) {
59 mp_emergency_exception_buf_size = 0;
Damien Georgeb4b10fd2015-01-01 23:30:53 +000060 MP_STATE_VM(mp_emergency_exception_buf) = NULL;
Dave Hylands5b7fd202014-07-01 23:46:53 -070061}
62
63mp_obj_t mp_alloc_emergency_exception_buf(mp_obj_t size_in) {
64 mp_int_t size = mp_obj_get_int(size_in);
65 void *buf = NULL;
66 if (size > 0) {
Damien George4d77e1a2015-02-27 09:34:51 +000067 buf = m_new(byte, size);
Dave Hylands5b7fd202014-07-01 23:46:53 -070068 }
69
70 int old_size = mp_emergency_exception_buf_size;
Damien Georgeb4b10fd2015-01-01 23:30:53 +000071 void *old_buf = MP_STATE_VM(mp_emergency_exception_buf);
Dave Hylands5b7fd202014-07-01 23:46:53 -070072
73 // Update the 2 variables atomically so that an interrupt can't occur
74 // between the assignments.
Damien George4859edb2014-10-15 17:33:24 +000075 mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
Dave Hylands5b7fd202014-07-01 23:46:53 -070076 mp_emergency_exception_buf_size = size;
Damien Georgeb4b10fd2015-01-01 23:30:53 +000077 MP_STATE_VM(mp_emergency_exception_buf) = buf;
Damien George4859edb2014-10-15 17:33:24 +000078 MICROPY_END_ATOMIC_SECTION(atomic_state);
Dave Hylands5b7fd202014-07-01 23:46:53 -070079
80 if (old_buf != NULL) {
Damien George4d77e1a2015-02-27 09:34:51 +000081 m_del(byte, old_buf, old_size);
Dave Hylands5b7fd202014-07-01 23:46:53 -070082 }
83 return mp_const_none;
84}
85#endif
86#endif // MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF
87
Paul Sokolovskyc4d589e2014-03-28 02:37:28 +020088// Instance of GeneratorExit exception - needed by generator.close()
89// This would belong to objgenerator.c, but to keep mp_obj_exception_t
90// definition module-private so far, have it here.
Damien George4852e092015-02-27 00:36:39 +000091const mp_obj_exception_t mp_const_GeneratorExit_obj = {{&mp_type_GeneratorExit}, 0, 0, MP_OBJ_NULL, mp_const_empty_tuple};
Paul Sokolovskyc4d589e2014-03-28 02:37:28 +020092
Damien George7f9d1d62015-04-09 23:56:15 +010093STATIC void mp_obj_exception_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +000094 mp_obj_exception_t *o = o_in;
Paul Sokolovskyd8351ca2014-05-02 01:51:25 +030095 mp_print_kind_t k = kind & ~PRINT_EXC_SUBCLASS;
96 bool is_subclass = kind & PRINT_EXC_SUBCLASS;
97 if (!is_subclass && (k == PRINT_REPR || k == PRINT_EXC)) {
Damien George7f9d1d62015-04-09 23:56:15 +010098 mp_printf(print, "%s", qstr_str(o->base.type->name));
Damiend99b0522013-12-21 18:17:45 +000099 }
Paul Sokolovskyd8351ca2014-05-02 01:51:25 +0300100
101 if (k == PRINT_EXC) {
Damien George7f9d1d62015-04-09 23:56:15 +0100102 mp_print_str(print, ": ");
Paul Sokolovskyd8351ca2014-05-02 01:51:25 +0300103 }
104
105 if (k == PRINT_STR || k == PRINT_EXC) {
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300106 if (o->args == NULL || o->args->len == 0) {
Damien George7f9d1d62015-04-09 23:56:15 +0100107 mp_print_str(print, "");
Paul Sokolovskya96d3d02014-03-31 01:10:10 +0300108 return;
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300109 } else if (o->args->len == 1) {
Damien George7f9d1d62015-04-09 23:56:15 +0100110 mp_obj_print_helper(print, o->args->items[0], PRINT_STR);
Paul Sokolovskya96d3d02014-03-31 01:10:10 +0300111 return;
112 }
113 }
Damien George7f9d1d62015-04-09 23:56:15 +0100114 mp_obj_tuple_print(print, o->args, kind);
Damiend99b0522013-12-21 18:17:45 +0000115}
116
Damien Georgeecc88e92014-08-30 00:35:11 +0100117mp_obj_t mp_obj_exception_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
Damien George50149a52015-01-20 14:11:27 +0000118 mp_arg_check_num(n_args, n_kw, 0, MP_OBJ_FUN_ARGS_MAX, false);
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300119 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 +0100120 if (o == NULL) {
121 // Couldn't allocate heap memory; use local data instead.
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000122 o = &MP_STATE_VM(mp_emergency_exception_obj);
Damien Georgef0954e32014-04-10 14:38:25 +0100123 // We can't store any args.
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300124 o->args = mp_const_empty_tuple;
125 } else {
126 o->args = mp_obj_new_tuple(n_args, args);
Damien Georgef0954e32014-04-10 14:38:25 +0100127 }
Damien George50149a52015-01-20 14:11:27 +0000128 o->base.type = type_in;
Damien George4852e092015-02-27 00:36:39 +0000129 o->traceback_data = NULL;
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200130 return o;
131}
132
Paul Sokolovskyaf1ae302014-03-26 19:17:20 +0200133// Get exception "value" - that is, first argument, or None
134mp_obj_t mp_obj_exception_get_value(mp_obj_t self_in) {
135 mp_obj_exception_t *self = self_in;
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300136 if (self->args->len == 0) {
Paul Sokolovskyaf1ae302014-03-26 19:17:20 +0200137 return mp_const_none;
138 } else {
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300139 return self->args->items[0];
Paul Sokolovskyaf1ae302014-03-26 19:17:20 +0200140 }
141}
142
Damien Georgeb1bbe962015-04-01 14:10:50 +0000143STATIC void exception_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
144 if (dest[0] != MP_OBJ_NULL) {
145 // not load attribute
146 return;
147 }
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +0200148 mp_obj_exception_t *self = self_in;
149 if (attr == MP_QSTR_args) {
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300150 dest[0] = self->args;
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +0200151 } else if (self->base.type == &mp_type_StopIteration && attr == MP_QSTR_value) {
Paul Sokolovskyaf1ae302014-03-26 19:17:20 +0200152 dest[0] = mp_obj_exception_get_value(self);
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +0200153 }
154}
155
Damien Georgeecc88e92014-08-30 00:35:11 +0100156STATIC mp_obj_t exc___init__(mp_uint_t n_args, const mp_obj_t *args) {
Paul Sokolovsky52386ca2014-05-18 20:44:04 +0300157 mp_obj_exception_t *self = args[0];
158 mp_obj_t argst = mp_obj_new_tuple(n_args - 1, args + 1);
159 self->args = argst;
160 return mp_const_none;
161}
162STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(exc___init___obj, 1, MP_OBJ_FUN_ARGS_MAX, exc___init__);
163
164STATIC const mp_map_elem_t exc_locals_dict_table[] = {
165 { MP_OBJ_NEW_QSTR(MP_QSTR___init__), (mp_obj_t)&exc___init___obj },
166};
167
168STATIC MP_DEFINE_CONST_DICT(exc_locals_dict, exc_locals_dict_table);
169
Damien Georgec5966122014-02-15 16:10:44 +0000170const mp_obj_type_t mp_type_BaseException = {
171 { &mp_type_type },
172 .name = MP_QSTR_BaseException,
173 .print = mp_obj_exception_print,
174 .make_new = mp_obj_exception_make_new,
Damien Georgeb1bbe962015-04-01 14:10:50 +0000175 .attr = exception_attr,
Paul Sokolovsky52386ca2014-05-18 20:44:04 +0300176 .locals_dict = (mp_obj_t)&exc_locals_dict,
Damiend99b0522013-12-21 18:17:45 +0000177};
178
Damien George22a08652014-02-15 21:05:25 +0000179#define MP_DEFINE_EXCEPTION_BASE(base_name) \
Damien George07ddab52014-03-29 13:15:08 +0000180STATIC 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 +0000181
182#define MP_DEFINE_EXCEPTION(exc_name, base_name) \
Damien Georgec5966122014-02-15 16:10:44 +0000183const mp_obj_type_t mp_type_ ## exc_name = { \
184 { &mp_type_type }, \
185 .name = MP_QSTR_ ## exc_name, \
186 .print = mp_obj_exception_print, \
187 .make_new = mp_obj_exception_make_new, \
Damien Georgeb1bbe962015-04-01 14:10:50 +0000188 .attr = exception_attr, \
Damien George22a08652014-02-15 21:05:25 +0000189 .bases_tuple = (mp_obj_t)&mp_type_ ## base_name ## _base_tuple, \
Damien Georgec5966122014-02-15 16:10:44 +0000190};
191
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000192// List of all exceptions, arranged as in the table at:
Chris Angelicodaf973a2014-06-06 03:51:03 +1000193// http://docs.python.org/3/library/exceptions.html
Damien George22a08652014-02-15 21:05:25 +0000194MP_DEFINE_EXCEPTION_BASE(BaseException)
Damien George7a4ddd22014-05-24 23:32:19 +0100195MP_DEFINE_EXCEPTION(SystemExit, BaseException)
Damien George124df6f2014-10-25 18:19:55 +0100196MP_DEFINE_EXCEPTION(KeyboardInterrupt, BaseException)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000197MP_DEFINE_EXCEPTION(GeneratorExit, BaseException)
198MP_DEFINE_EXCEPTION(Exception, BaseException)
199 MP_DEFINE_EXCEPTION_BASE(Exception)
200 MP_DEFINE_EXCEPTION(StopIteration, Exception)
201 MP_DEFINE_EXCEPTION(ArithmeticError, Exception)
202 MP_DEFINE_EXCEPTION_BASE(ArithmeticError)
Damien Georgec63f9842014-03-27 23:49:06 +0000203 //MP_DEFINE_EXCEPTION(FloatingPointError, ArithmeticError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000204 MP_DEFINE_EXCEPTION(OverflowError, ArithmeticError)
205 MP_DEFINE_EXCEPTION(ZeroDivisionError, ArithmeticError)
206 MP_DEFINE_EXCEPTION(AssertionError, Exception)
207 MP_DEFINE_EXCEPTION(AttributeError, Exception)
Damien Georgec63f9842014-03-27 23:49:06 +0000208 //MP_DEFINE_EXCEPTION(BufferError, Exception)
Damien George8b03d942014-09-30 13:59:30 +0000209 //MP_DEFINE_EXCEPTION(EnvironmentError, Exception) use OSError instead
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000210 MP_DEFINE_EXCEPTION(EOFError, Exception)
211 MP_DEFINE_EXCEPTION(ImportError, Exception)
Damien George8b03d942014-09-30 13:59:30 +0000212 //MP_DEFINE_EXCEPTION(IOError, Exception) use OSError instead
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000213 MP_DEFINE_EXCEPTION(LookupError, Exception)
214 MP_DEFINE_EXCEPTION_BASE(LookupError)
215 MP_DEFINE_EXCEPTION(IndexError, LookupError)
216 MP_DEFINE_EXCEPTION(KeyError, LookupError)
217 MP_DEFINE_EXCEPTION(MemoryError, Exception)
218 MP_DEFINE_EXCEPTION(NameError, Exception)
Damien Georgec63f9842014-03-27 23:49:06 +0000219 /*
Antonin ENFRUNda1fffa2014-05-12 00:21:50 +0200220 MP_DEFINE_EXCEPTION_BASE(NameError)
221 MP_DEFINE_EXCEPTION(UnboundLocalError, NameError)
222 */
223 MP_DEFINE_EXCEPTION(OSError, Exception)
224 /*
225 MP_DEFINE_EXCEPTION_BASE(OSError)
Damien Georgec63f9842014-03-27 23:49:06 +0000226 MP_DEFINE_EXCEPTION(BlockingIOError, OSError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000227 MP_DEFINE_EXCEPTION(ChildProcessError, OSError)
228 MP_DEFINE_EXCEPTION(ConnectionError, OSError)
229 MP_DEFINE_EXCEPTION(BrokenPipeError, ConnectionError)
230 MP_DEFINE_EXCEPTION(ConnectionAbortedError, ConnectionError)
231 MP_DEFINE_EXCEPTION(ConnectionRefusedError, ConnectionError)
232 MP_DEFINE_EXCEPTION(ConnectionResetError, ConnectionError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000233 MP_DEFINE_EXCEPTION(InterruptedError, OSError)
234 MP_DEFINE_EXCEPTION(IsADirectoryError, OSError)
235 MP_DEFINE_EXCEPTION(NotADirectoryError, OSError)
236 MP_DEFINE_EXCEPTION(PermissionError, OSError)
237 MP_DEFINE_EXCEPTION(ProcessLookupError, OSError)
Damien Georgec63f9842014-03-27 23:49:06 +0000238 MP_DEFINE_EXCEPTION(TimeoutError, OSError)
Damien Georgec9109722014-03-22 23:40:02 +0000239 MP_DEFINE_EXCEPTION(FileExistsError, OSError)
240 MP_DEFINE_EXCEPTION(FileNotFoundError, OSError)
Damien Georgec63f9842014-03-27 23:49:06 +0000241 MP_DEFINE_EXCEPTION(ReferenceError, Exception)
242 */
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000243 MP_DEFINE_EXCEPTION(RuntimeError, Exception)
244 MP_DEFINE_EXCEPTION_BASE(RuntimeError)
245 MP_DEFINE_EXCEPTION(NotImplementedError, RuntimeError)
246 MP_DEFINE_EXCEPTION(SyntaxError, Exception)
247 MP_DEFINE_EXCEPTION_BASE(SyntaxError)
248 MP_DEFINE_EXCEPTION(IndentationError, SyntaxError)
Damien Georgec63f9842014-03-27 23:49:06 +0000249 /*
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000250 MP_DEFINE_EXCEPTION_BASE(IndentationError)
Damien Georgec63f9842014-03-27 23:49:06 +0000251 MP_DEFINE_EXCEPTION(TabError, IndentationError)
252 */
Damien Georgee7a47822014-10-22 19:42:55 +0100253 //MP_DEFINE_EXCEPTION(SystemError, Exception)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000254 MP_DEFINE_EXCEPTION(TypeError, Exception)
255 MP_DEFINE_EXCEPTION(ValueError, Exception)
Paul Sokolovsky71ebd4b2015-02-23 23:18:36 +0200256#if MICROPY_PY_BUILTINS_STR_UNICODE
257 MP_DEFINE_EXCEPTION_BASE(ValueError)
258 MP_DEFINE_EXCEPTION(UnicodeError, ValueError)
259 //TODO: Implement more UnicodeError subclasses which take arguments
260#endif
Damien Georgec9109722014-03-22 23:40:02 +0000261 /*
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000262 MP_DEFINE_EXCEPTION(Warning, Exception)
263 MP_DEFINE_EXCEPTION_BASE(Warning)
264 MP_DEFINE_EXCEPTION(DeprecationWarning, Warning)
265 MP_DEFINE_EXCEPTION(PendingDeprecationWarning, Warning)
266 MP_DEFINE_EXCEPTION(RuntimeWarning, Warning)
267 MP_DEFINE_EXCEPTION(SyntaxWarning, Warning)
268 MP_DEFINE_EXCEPTION(UserWarning, Warning)
269 MP_DEFINE_EXCEPTION(FutureWarning, Warning)
270 MP_DEFINE_EXCEPTION(ImportWarning, Warning)
271 MP_DEFINE_EXCEPTION(UnicodeWarning, Warning)
272 MP_DEFINE_EXCEPTION(BytesWarning, Warning)
273 MP_DEFINE_EXCEPTION(ResourceWarning, Warning)
Damien Georgec9109722014-03-22 23:40:02 +0000274 */
Damien Georgec5966122014-02-15 16:10:44 +0000275
276mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type) {
Paul Sokolovskya96d3d02014-03-31 01:10:10 +0300277 return mp_obj_new_exception_args(exc_type, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000278}
279
Paul Sokolovskydec31bb2014-04-22 00:01:13 +0300280// "Optimized" version for common(?) case of having 1 exception arg
281mp_obj_t mp_obj_new_exception_arg1(const mp_obj_type_t *exc_type, mp_obj_t arg) {
282 return mp_obj_new_exception_args(exc_type, 1, &arg);
283}
284
Damien Georged182b982014-08-30 14:19:41 +0100285mp_obj_t mp_obj_new_exception_args(const mp_obj_type_t *exc_type, mp_uint_t n_args, const mp_obj_t *args) {
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200286 assert(exc_type->make_new == mp_obj_exception_make_new);
287 return exc_type->make_new((mp_obj_t)exc_type, n_args, 0, args);
288}
289
Damien Georgec5966122014-02-15 16:10:44 +0000290mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, const char *msg) {
291 return mp_obj_new_exception_msg_varg(exc_type, msg);
Damiend99b0522013-12-21 18:17:45 +0000292}
293
Damien Georgec5966122014-02-15 16:10:44 +0000294mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char *fmt, ...) {
295 // check that the given type is an exception type
296 assert(exc_type->make_new == mp_obj_exception_make_new);
297
Damien George6c73ca12014-01-08 18:11:23 +0000298 // make exception object
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300299 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 +0100300 if (o == NULL) {
301 // Couldn't allocate heap memory; use local data instead.
302 // Unfortunately, we won't be able to format the string...
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000303 o = &MP_STATE_VM(mp_emergency_exception_obj);
Damien Georgef0954e32014-04-10 14:38:25 +0100304 o->base.type = exc_type;
Damien George4852e092015-02-27 00:36:39 +0000305 o->traceback_data = NULL;
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300306 o->args = mp_const_empty_tuple;
Dave Hylands5b7fd202014-07-01 23:46:53 -0700307
308#if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF
309 // If the user has provided a buffer, then we try to create a tuple
310 // of length 1, which has a string object and the string data.
311
312 if (mp_emergency_exception_buf_size > (sizeof(mp_obj_tuple_t) + sizeof(mp_obj_str_t) + sizeof(mp_obj_t))) {
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000313 mp_obj_tuple_t *tuple = (mp_obj_tuple_t *)MP_STATE_VM(mp_emergency_exception_buf);
Dave Hylands5b7fd202014-07-01 23:46:53 -0700314 mp_obj_str_t *str = (mp_obj_str_t *)&tuple->items[1];
315
316 tuple->base.type = &mp_type_tuple;
317 tuple->len = 1;
318 tuple->items[0] = str;
319
320 byte *str_data = (byte *)&str[1];
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000321 uint max_len = MP_STATE_VM(mp_emergency_exception_buf) + mp_emergency_exception_buf_size
Dave Hylands5b7fd202014-07-01 23:46:53 -0700322 - str_data;
323
324 va_list ap;
325 va_start(ap, fmt);
326 str->len = vsnprintf((char *)str_data, max_len, fmt, ap);
327 va_end(ap);
328
329 str->base.type = &mp_type_str;
330 str->hash = qstr_compute_hash(str_data, str->len);
331 str->data = str_data;
332
333 o->args = tuple;
334
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000335 uint offset = &str_data[str->len] - MP_STATE_VM(mp_emergency_exception_buf);
Dave Hylands5b7fd202014-07-01 23:46:53 -0700336 offset += sizeof(void *) - 1;
337 offset &= ~(sizeof(void *) - 1);
338
Damien George4852e092015-02-27 00:36:39 +0000339 if ((mp_emergency_exception_buf_size - offset) > (sizeof(mp_uint_t) * 3)) {
Dave Hylands5b7fd202014-07-01 23:46:53 -0700340 // We have room to store some traceback.
Damien George4852e092015-02-27 00:36:39 +0000341 o->traceback_data = (mp_uint_t*)((byte *)MP_STATE_VM(mp_emergency_exception_buf) + offset);
342 o->traceback_alloc = (MP_STATE_VM(mp_emergency_exception_buf) + mp_emergency_exception_buf_size - (byte *)o->traceback_data) / sizeof(o->traceback_data[0]);
343 o->traceback_len = 0;
Dave Hylands5b7fd202014-07-01 23:46:53 -0700344 }
345 }
346#endif // MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200347 } else {
Damien Georgef0954e32014-04-10 14:38:25 +0100348 o->base.type = exc_type;
Damien George4852e092015-02-27 00:36:39 +0000349 o->traceback_data = NULL;
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300350 o->args = mp_obj_new_tuple(1, NULL);
Damien Georgef0954e32014-04-10 14:38:25 +0100351
352 if (fmt == NULL) {
353 // no message
354 assert(0);
355 } else {
Paul Sokolovsky29c4f922015-02-15 22:19:30 +0300356 if (strchr(fmt, '%') == NULL) {
357 // no formatting substitutions, avoid allocating vstr.
358 o->args->items[0] = mp_obj_new_str(fmt, strlen(fmt), false);
359 } else {
360 // render exception message and store as .args[0]
361 va_list ap;
362 vstr_t vstr;
363 vstr_init(&vstr, 16);
364 va_start(ap, fmt);
365 vstr_vprintf(&vstr, fmt, ap);
366 va_end(ap);
367 o->args->items[0] = mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
368 }
Damien Georgef0954e32014-04-10 14:38:25 +0100369 }
Damien George6c73ca12014-01-08 18:11:23 +0000370 }
Damien George6c73ca12014-01-08 18:11:23 +0000371
372 return o;
373}
374
Damien Georgec5966122014-02-15 16:10:44 +0000375// return true if the given object is an exception type
Damien Georgec5966122014-02-15 16:10:44 +0000376bool mp_obj_is_exception_type(mp_obj_t self_in) {
377 if (MP_OBJ_IS_TYPE(self_in, &mp_type_type)) {
Damien George9e6e9352014-03-26 18:37:06 +0000378 // optimisation when self_in is a builtin exception
Damien Georgec5966122014-02-15 16:10:44 +0000379 mp_obj_type_t *self = self_in;
Damien George9e6e9352014-03-26 18:37:06 +0000380 if (self->make_new == mp_obj_exception_make_new) {
381 return true;
382 }
Damien Georgec5966122014-02-15 16:10:44 +0000383 }
Damien George9e6e9352014-03-26 18:37:06 +0000384 return mp_obj_is_subclass_fast(self_in, &mp_type_BaseException);
Damien Georgec5966122014-02-15 16:10:44 +0000385}
386
387// return true if the given object is an instance of an exception type
Damien Georgec5966122014-02-15 16:10:44 +0000388bool mp_obj_is_exception_instance(mp_obj_t self_in) {
Damien George9e6e9352014-03-26 18:37:06 +0000389 return mp_obj_is_exception_type(mp_obj_get_type(self_in));
Damien Georgec5966122014-02-15 16:10:44 +0000390}
391
Damien George4bcd04b2014-09-24 14:05:40 +0100392// Return true if exception (type or instance) is a subclass of given
393// exception type. Assumes exc_type is a subclass of BaseException, as
394// defined by mp_obj_is_exception_type(exc_type).
395bool mp_obj_exception_match(mp_obj_t exc, mp_const_obj_t exc_type) {
396 // if exc is an instance of an exception, then extract and use its type
397 if (mp_obj_is_exception_instance(exc)) {
398 exc = mp_obj_get_type(exc);
399 }
400 return mp_obj_is_subclass_fast(exc, exc_type);
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200401}
402
Paul Sokolovsky91e556a2014-05-02 02:27:00 +0300403// traceback handling functions
Damien Georgec5966122014-02-15 16:10:44 +0000404
Paul Sokolovsky91e556a2014-05-02 02:27:00 +0300405#define GET_NATIVE_EXCEPTION(self, self_in) \
406 /* make sure self_in is an exception instance */ \
407 assert(mp_obj_is_exception_instance(self_in)); \
408 mp_obj_exception_t *self; \
409 if (mp_obj_is_native_exception_instance(self_in)) { \
410 self = self_in; \
411 } else { \
412 self = ((mp_obj_instance_t*)self_in)->subobj[0]; \
Damien George9e6e9352014-03-26 18:37:06 +0000413 }
Paul Sokolovsky91e556a2014-05-02 02:27:00 +0300414
415void mp_obj_exception_clear_traceback(mp_obj_t self_in) {
416 GET_NATIVE_EXCEPTION(self, self_in);
417 // just set the traceback to the null object
418 // we don't want to call any memory management functions here
Damien George4852e092015-02-27 00:36:39 +0000419 self->traceback_data = NULL;
Damienb86e3f92013-12-29 17:17:43 +0000420}
Damien George08335002014-01-18 23:24:36 +0000421
Damien George40f3c022014-07-03 13:25:24 +0100422void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, mp_uint_t line, qstr block) {
Dave Hylands5b7fd202014-07-01 23:46:53 -0700423 GET_NATIVE_EXCEPTION(self, self_in);
424
Damien George4852e092015-02-27 00:36:39 +0000425 // append this traceback info to traceback data
426 // if memory allocation fails (eg because gc is locked), just return
Damien Georgef0b29722014-07-01 14:28:09 +0100427
Damien George4852e092015-02-27 00:36:39 +0000428 if (self->traceback_data == NULL) {
429 self->traceback_data = m_new_maybe(mp_uint_t, 3);
430 if (self->traceback_data == NULL) {
Paul Sokolovskyfa3b8952015-02-15 22:24:03 +0300431 return;
432 }
Damien George4852e092015-02-27 00:36:39 +0000433 self->traceback_alloc = 3;
434 self->traceback_len = 0;
435 } else if (self->traceback_len + 3 > self->traceback_alloc) {
436 // be conservative with growing traceback data
437 mp_uint_t *tb_data = m_renew_maybe(mp_uint_t, self->traceback_data, self->traceback_alloc, self->traceback_alloc + 3);
438 if (tb_data == NULL) {
439 return;
440 }
441 self->traceback_data = tb_data;
442 self->traceback_alloc += 3;
Damien George08335002014-01-18 23:24:36 +0000443 }
Damien George4852e092015-02-27 00:36:39 +0000444
445 mp_uint_t *tb_data = &self->traceback_data[self->traceback_len];
446 self->traceback_len += 3;
447 tb_data[0] = (mp_uint_t)file;
448 tb_data[1] = (mp_uint_t)line;
449 tb_data[2] = (mp_uint_t)block;
Damien George08335002014-01-18 23:24:36 +0000450}
451
Damien George40f3c022014-07-03 13:25:24 +0100452void mp_obj_exception_get_traceback(mp_obj_t self_in, mp_uint_t *n, mp_uint_t **values) {
Paul Sokolovsky91e556a2014-05-02 02:27:00 +0300453 GET_NATIVE_EXCEPTION(self, self_in);
Damien Georgec5966122014-02-15 16:10:44 +0000454
Damien George4852e092015-02-27 00:36:39 +0000455 if (self->traceback_data == NULL) {
Damien George136b1492014-01-19 12:38:49 +0000456 *n = 0;
457 *values = NULL;
458 } else {
Damien George4852e092015-02-27 00:36:39 +0000459 *n = self->traceback_len;
460 *values = self->traceback_data;
Damien George136b1492014-01-19 12:38:49 +0000461 }
Damien George08335002014-01-18 23:24:36 +0000462}