blob: 4b8d6ea08b58ae9a19ba6cd283eeac036af995bd [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>
Damien George109c1de2014-10-31 21:30:46 +000031#include <stdint.h>
Damiend99b0522013-12-21 18:17:45 +000032
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030033#include "mpconfig.h"
Damiend99b0522013-12-21 18:17:45 +000034#include "nlr.h"
35#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000036#include "qstr.h"
Damiend99b0522013-12-21 18:17:45 +000037#include "obj.h"
Dave Hylands5b7fd202014-07-01 23:46:53 -070038#include "objlist.h"
39#include "objstr.h"
Paul Sokolovskyddf21782014-01-12 23:30:20 +020040#include "objtuple.h"
Paul Sokolovsky91e556a2014-05-02 02:27:00 +030041#include "objtype.h"
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +020042#include "runtime.h"
43#include "runtime0.h"
Dave Hylands2fe841d2014-06-30 22:49:21 -070044#include "gc.h"
Damiend99b0522013-12-21 18:17:45 +000045
Damien George07ddab52014-03-29 13:15:08 +000046typedef struct _mp_obj_exception_t {
Damiend99b0522013-12-21 18:17:45 +000047 mp_obj_base_t base;
Damien George136b1492014-01-19 12:38:49 +000048 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 +030049 mp_obj_tuple_t *args;
Damiend99b0522013-12-21 18:17:45 +000050} mp_obj_exception_t;
51
Damien George6902eed2014-04-04 10:52:59 +000052// Instance of MemoryError exception - needed by mp_malloc_fail
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +030053const 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 +000054
Damien Georgef0954e32014-04-10 14:38:25 +010055// Local non-heap memory for allocating an exception when we run out of RAM
56STATIC mp_obj_exception_t mp_emergency_exception_obj;
57
Dave Hylands5b7fd202014-07-01 23:46:53 -070058// Optionally allocated buffer for storing the first argument of an exception
59// allocated when the heap is locked.
60#if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF
61# if MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE > 0
62STATIC byte mp_emergency_exception_buf[MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE];
63#define mp_emergency_exception_buf_size MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE
64
65void mp_init_emergency_exception_buf(void) {
66 // Nothing to do since the buffer was declared statically. We put this
67 // definition here so that the calling code can call this function
68 // regardless of how its configured (makes the calling code a bit cleaner).
69}
70
71#else
72STATIC mp_int_t mp_emergency_exception_buf_size = 0;
73STATIC byte *mp_emergency_exception_buf = NULL;
74
75void mp_init_emergency_exception_buf(void) {
76 mp_emergency_exception_buf_size = 0;
77 mp_emergency_exception_buf = NULL;
78}
79
80mp_obj_t mp_alloc_emergency_exception_buf(mp_obj_t size_in) {
81 mp_int_t size = mp_obj_get_int(size_in);
82 void *buf = NULL;
83 if (size > 0) {
84 buf = m_malloc(size);
85 }
86
87 int old_size = mp_emergency_exception_buf_size;
88 void *old_buf = mp_emergency_exception_buf;
89
90 // Update the 2 variables atomically so that an interrupt can't occur
91 // between the assignments.
Damien George4859edb2014-10-15 17:33:24 +000092 mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
Dave Hylands5b7fd202014-07-01 23:46:53 -070093 mp_emergency_exception_buf_size = size;
94 mp_emergency_exception_buf = buf;
Damien George4859edb2014-10-15 17:33:24 +000095 MICROPY_END_ATOMIC_SECTION(atomic_state);
Dave Hylands5b7fd202014-07-01 23:46:53 -070096
97 if (old_buf != NULL) {
98 m_free(old_buf, old_size);
99 }
100 return mp_const_none;
101}
102#endif
103#endif // MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF
104
Paul Sokolovskyc4d589e2014-03-28 02:37:28 +0200105// Instance of GeneratorExit exception - needed by generator.close()
106// This would belong to objgenerator.c, but to keep mp_obj_exception_t
107// definition module-private so far, have it here.
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300108const 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 +0200109
Damien Georgec5966122014-02-15 16:10:44 +0000110STATIC 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 +0000111 mp_obj_exception_t *o = o_in;
Paul Sokolovskyd8351ca2014-05-02 01:51:25 +0300112 mp_print_kind_t k = kind & ~PRINT_EXC_SUBCLASS;
113 bool is_subclass = kind & PRINT_EXC_SUBCLASS;
114 if (!is_subclass && (k == PRINT_REPR || k == PRINT_EXC)) {
Paul Sokolovskya96d3d02014-03-31 01:10:10 +0300115 print(env, "%s", qstr_str(o->base.type->name));
Damiend99b0522013-12-21 18:17:45 +0000116 }
Paul Sokolovskyd8351ca2014-05-02 01:51:25 +0300117
118 if (k == PRINT_EXC) {
119 print(env, ": ");
120 }
121
122 if (k == PRINT_STR || k == PRINT_EXC) {
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300123 if (o->args == NULL || o->args->len == 0) {
Paul Sokolovskya96d3d02014-03-31 01:10:10 +0300124 print(env, "");
125 return;
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300126 } else if (o->args->len == 1) {
127 mp_obj_print_helper(print, env, o->args->items[0], PRINT_STR);
Paul Sokolovskya96d3d02014-03-31 01:10:10 +0300128 return;
129 }
130 }
Damien George2323ef92014-05-11 18:00:45 +0100131 mp_obj_tuple_print(print, env, o->args, kind);
Damiend99b0522013-12-21 18:17:45 +0000132}
133
Damien Georgeecc88e92014-08-30 00:35:11 +0100134mp_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 Georgec5966122014-02-15 16:10:44 +0000135 mp_obj_type_t *type = type_in;
Damien George20006db2014-01-18 14:10:48 +0000136
137 if (n_kw != 0) {
Damien Georgeea13f402014-04-05 18:32:08 +0100138 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 +0000139 }
140
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300141 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 +0100142 if (o == NULL) {
143 // Couldn't allocate heap memory; use local data instead.
144 o = &mp_emergency_exception_obj;
145 // We can't store any args.
146 n_args = 0;
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300147 o->args = mp_const_empty_tuple;
148 } else {
149 o->args = mp_obj_new_tuple(n_args, args);
Damien Georgef0954e32014-04-10 14:38:25 +0100150 }
Damien Georgec5966122014-02-15 16:10:44 +0000151 o->base.type = type;
Paul Sokolovsky60a0d3f2014-02-14 21:01:12 +0200152 o->traceback = MP_OBJ_NULL;
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200153 return o;
154}
155
Paul Sokolovskyaf1ae302014-03-26 19:17:20 +0200156// Get exception "value" - that is, first argument, or None
157mp_obj_t mp_obj_exception_get_value(mp_obj_t self_in) {
158 mp_obj_exception_t *self = self_in;
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300159 if (self->args->len == 0) {
Paul Sokolovskyaf1ae302014-03-26 19:17:20 +0200160 return mp_const_none;
161 } else {
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300162 return self->args->items[0];
Paul Sokolovskyaf1ae302014-03-26 19:17:20 +0200163 }
164}
165
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +0200166STATIC void exception_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
167 mp_obj_exception_t *self = self_in;
168 if (attr == MP_QSTR_args) {
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300169 dest[0] = self->args;
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +0200170 } else if (self->base.type == &mp_type_StopIteration && attr == MP_QSTR_value) {
Paul Sokolovskyaf1ae302014-03-26 19:17:20 +0200171 dest[0] = mp_obj_exception_get_value(self);
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +0200172 }
173}
174
Damien Georgeecc88e92014-08-30 00:35:11 +0100175STATIC mp_obj_t exc___init__(mp_uint_t n_args, const mp_obj_t *args) {
Paul Sokolovsky52386ca2014-05-18 20:44:04 +0300176 mp_obj_exception_t *self = args[0];
177 mp_obj_t argst = mp_obj_new_tuple(n_args - 1, args + 1);
178 self->args = argst;
179 return mp_const_none;
180}
181STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(exc___init___obj, 1, MP_OBJ_FUN_ARGS_MAX, exc___init__);
182
183STATIC const mp_map_elem_t exc_locals_dict_table[] = {
184 { MP_OBJ_NEW_QSTR(MP_QSTR___init__), (mp_obj_t)&exc___init___obj },
185};
186
187STATIC MP_DEFINE_CONST_DICT(exc_locals_dict, exc_locals_dict_table);
188
Damien Georgec5966122014-02-15 16:10:44 +0000189const mp_obj_type_t mp_type_BaseException = {
190 { &mp_type_type },
191 .name = MP_QSTR_BaseException,
192 .print = mp_obj_exception_print,
193 .make_new = mp_obj_exception_make_new,
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +0200194 .load_attr = exception_load_attr,
Paul Sokolovsky52386ca2014-05-18 20:44:04 +0300195 .locals_dict = (mp_obj_t)&exc_locals_dict,
Damiend99b0522013-12-21 18:17:45 +0000196};
197
Damien George22a08652014-02-15 21:05:25 +0000198#define MP_DEFINE_EXCEPTION_BASE(base_name) \
Damien George07ddab52014-03-29 13:15:08 +0000199STATIC 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 +0000200
201#define MP_DEFINE_EXCEPTION(exc_name, base_name) \
Damien Georgec5966122014-02-15 16:10:44 +0000202const mp_obj_type_t mp_type_ ## exc_name = { \
203 { &mp_type_type }, \
204 .name = MP_QSTR_ ## exc_name, \
205 .print = mp_obj_exception_print, \
206 .make_new = mp_obj_exception_make_new, \
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +0200207 .load_attr = exception_load_attr, \
Damien George22a08652014-02-15 21:05:25 +0000208 .bases_tuple = (mp_obj_t)&mp_type_ ## base_name ## _base_tuple, \
Damien Georgec5966122014-02-15 16:10:44 +0000209};
210
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000211// List of all exceptions, arranged as in the table at:
Chris Angelicodaf973a2014-06-06 03:51:03 +1000212// http://docs.python.org/3/library/exceptions.html
Damien George22a08652014-02-15 21:05:25 +0000213MP_DEFINE_EXCEPTION_BASE(BaseException)
Damien George7a4ddd22014-05-24 23:32:19 +0100214MP_DEFINE_EXCEPTION(SystemExit, BaseException)
Damien George124df6f2014-10-25 18:19:55 +0100215MP_DEFINE_EXCEPTION(KeyboardInterrupt, BaseException)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000216MP_DEFINE_EXCEPTION(GeneratorExit, BaseException)
217MP_DEFINE_EXCEPTION(Exception, BaseException)
218 MP_DEFINE_EXCEPTION_BASE(Exception)
219 MP_DEFINE_EXCEPTION(StopIteration, Exception)
220 MP_DEFINE_EXCEPTION(ArithmeticError, Exception)
221 MP_DEFINE_EXCEPTION_BASE(ArithmeticError)
Damien Georgec63f9842014-03-27 23:49:06 +0000222 //MP_DEFINE_EXCEPTION(FloatingPointError, ArithmeticError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000223 MP_DEFINE_EXCEPTION(OverflowError, ArithmeticError)
224 MP_DEFINE_EXCEPTION(ZeroDivisionError, ArithmeticError)
225 MP_DEFINE_EXCEPTION(AssertionError, Exception)
226 MP_DEFINE_EXCEPTION(AttributeError, Exception)
Damien Georgec63f9842014-03-27 23:49:06 +0000227 //MP_DEFINE_EXCEPTION(BufferError, Exception)
Damien George8b03d942014-09-30 13:59:30 +0000228 //MP_DEFINE_EXCEPTION(EnvironmentError, Exception) use OSError instead
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000229 MP_DEFINE_EXCEPTION(EOFError, Exception)
230 MP_DEFINE_EXCEPTION(ImportError, Exception)
Damien George8b03d942014-09-30 13:59:30 +0000231 //MP_DEFINE_EXCEPTION(IOError, Exception) use OSError instead
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000232 MP_DEFINE_EXCEPTION(LookupError, Exception)
233 MP_DEFINE_EXCEPTION_BASE(LookupError)
234 MP_DEFINE_EXCEPTION(IndexError, LookupError)
235 MP_DEFINE_EXCEPTION(KeyError, LookupError)
236 MP_DEFINE_EXCEPTION(MemoryError, Exception)
237 MP_DEFINE_EXCEPTION(NameError, Exception)
Damien Georgec63f9842014-03-27 23:49:06 +0000238 /*
Antonin ENFRUNda1fffa2014-05-12 00:21:50 +0200239 MP_DEFINE_EXCEPTION_BASE(NameError)
240 MP_DEFINE_EXCEPTION(UnboundLocalError, NameError)
241 */
242 MP_DEFINE_EXCEPTION(OSError, Exception)
243 /*
244 MP_DEFINE_EXCEPTION_BASE(OSError)
Damien Georgec63f9842014-03-27 23:49:06 +0000245 MP_DEFINE_EXCEPTION(BlockingIOError, OSError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000246 MP_DEFINE_EXCEPTION(ChildProcessError, OSError)
247 MP_DEFINE_EXCEPTION(ConnectionError, OSError)
248 MP_DEFINE_EXCEPTION(BrokenPipeError, ConnectionError)
249 MP_DEFINE_EXCEPTION(ConnectionAbortedError, ConnectionError)
250 MP_DEFINE_EXCEPTION(ConnectionRefusedError, ConnectionError)
251 MP_DEFINE_EXCEPTION(ConnectionResetError, ConnectionError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000252 MP_DEFINE_EXCEPTION(InterruptedError, OSError)
253 MP_DEFINE_EXCEPTION(IsADirectoryError, OSError)
254 MP_DEFINE_EXCEPTION(NotADirectoryError, OSError)
255 MP_DEFINE_EXCEPTION(PermissionError, OSError)
256 MP_DEFINE_EXCEPTION(ProcessLookupError, OSError)
Damien Georgec63f9842014-03-27 23:49:06 +0000257 MP_DEFINE_EXCEPTION(TimeoutError, OSError)
Damien Georgec9109722014-03-22 23:40:02 +0000258 MP_DEFINE_EXCEPTION(FileExistsError, OSError)
259 MP_DEFINE_EXCEPTION(FileNotFoundError, OSError)
Damien Georgec63f9842014-03-27 23:49:06 +0000260 MP_DEFINE_EXCEPTION(ReferenceError, Exception)
261 */
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000262 MP_DEFINE_EXCEPTION(RuntimeError, Exception)
263 MP_DEFINE_EXCEPTION_BASE(RuntimeError)
264 MP_DEFINE_EXCEPTION(NotImplementedError, RuntimeError)
265 MP_DEFINE_EXCEPTION(SyntaxError, Exception)
266 MP_DEFINE_EXCEPTION_BASE(SyntaxError)
267 MP_DEFINE_EXCEPTION(IndentationError, SyntaxError)
Damien Georgec63f9842014-03-27 23:49:06 +0000268 /*
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000269 MP_DEFINE_EXCEPTION_BASE(IndentationError)
Damien Georgec63f9842014-03-27 23:49:06 +0000270 MP_DEFINE_EXCEPTION(TabError, IndentationError)
271 */
Damien Georgee7a47822014-10-22 19:42:55 +0100272 //MP_DEFINE_EXCEPTION(SystemError, Exception)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000273 MP_DEFINE_EXCEPTION(TypeError, Exception)
274 MP_DEFINE_EXCEPTION(ValueError, Exception)
275 //TODO: Implement UnicodeErrors which take arguments
Damien Georgec9109722014-03-22 23:40:02 +0000276 /*
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000277 MP_DEFINE_EXCEPTION(Warning, Exception)
278 MP_DEFINE_EXCEPTION_BASE(Warning)
279 MP_DEFINE_EXCEPTION(DeprecationWarning, Warning)
280 MP_DEFINE_EXCEPTION(PendingDeprecationWarning, Warning)
281 MP_DEFINE_EXCEPTION(RuntimeWarning, Warning)
282 MP_DEFINE_EXCEPTION(SyntaxWarning, Warning)
283 MP_DEFINE_EXCEPTION(UserWarning, Warning)
284 MP_DEFINE_EXCEPTION(FutureWarning, Warning)
285 MP_DEFINE_EXCEPTION(ImportWarning, Warning)
286 MP_DEFINE_EXCEPTION(UnicodeWarning, Warning)
287 MP_DEFINE_EXCEPTION(BytesWarning, Warning)
288 MP_DEFINE_EXCEPTION(ResourceWarning, Warning)
Damien Georgec9109722014-03-22 23:40:02 +0000289 */
Damien Georgec5966122014-02-15 16:10:44 +0000290
291mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type) {
Paul Sokolovskya96d3d02014-03-31 01:10:10 +0300292 return mp_obj_new_exception_args(exc_type, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000293}
294
Paul Sokolovskydec31bb2014-04-22 00:01:13 +0300295// "Optimized" version for common(?) case of having 1 exception arg
296mp_obj_t mp_obj_new_exception_arg1(const mp_obj_type_t *exc_type, mp_obj_t arg) {
297 return mp_obj_new_exception_args(exc_type, 1, &arg);
298}
299
Damien Georged182b982014-08-30 14:19:41 +0100300mp_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 +0200301 assert(exc_type->make_new == mp_obj_exception_make_new);
302 return exc_type->make_new((mp_obj_t)exc_type, n_args, 0, args);
303}
304
Damien Georgec5966122014-02-15 16:10:44 +0000305mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, const char *msg) {
306 return mp_obj_new_exception_msg_varg(exc_type, msg);
Damiend99b0522013-12-21 18:17:45 +0000307}
308
Damien Georgec5966122014-02-15 16:10:44 +0000309mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char *fmt, ...) {
310 // check that the given type is an exception type
311 assert(exc_type->make_new == mp_obj_exception_make_new);
312
Damien George6c73ca12014-01-08 18:11:23 +0000313 // make exception object
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300314 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 +0100315 if (o == NULL) {
316 // Couldn't allocate heap memory; use local data instead.
317 // Unfortunately, we won't be able to format the string...
318 o = &mp_emergency_exception_obj;
319 o->base.type = exc_type;
320 o->traceback = MP_OBJ_NULL;
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300321 o->args = mp_const_empty_tuple;
Dave Hylands5b7fd202014-07-01 23:46:53 -0700322
323#if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF
324 // If the user has provided a buffer, then we try to create a tuple
325 // of length 1, which has a string object and the string data.
326
327 if (mp_emergency_exception_buf_size > (sizeof(mp_obj_tuple_t) + sizeof(mp_obj_str_t) + sizeof(mp_obj_t))) {
328 mp_obj_tuple_t *tuple = (mp_obj_tuple_t *)mp_emergency_exception_buf;
329 mp_obj_str_t *str = (mp_obj_str_t *)&tuple->items[1];
330
331 tuple->base.type = &mp_type_tuple;
332 tuple->len = 1;
333 tuple->items[0] = str;
334
335 byte *str_data = (byte *)&str[1];
336 uint max_len = mp_emergency_exception_buf + mp_emergency_exception_buf_size
337 - str_data;
338
339 va_list ap;
340 va_start(ap, fmt);
341 str->len = vsnprintf((char *)str_data, max_len, fmt, ap);
342 va_end(ap);
343
344 str->base.type = &mp_type_str;
345 str->hash = qstr_compute_hash(str_data, str->len);
346 str->data = str_data;
347
348 o->args = tuple;
349
350 uint offset = &str_data[str->len] - mp_emergency_exception_buf;
351 offset += sizeof(void *) - 1;
352 offset &= ~(sizeof(void *) - 1);
353
354 if ((mp_emergency_exception_buf_size - offset) > (sizeof(mp_obj_list_t) + sizeof(mp_obj_t) * 3)) {
355 // We have room to store some traceback.
356 mp_obj_list_t *list = (mp_obj_list_t *)((byte *)mp_emergency_exception_buf + offset);
357 list->base.type = &mp_type_list;
358 list->items = (mp_obj_t)&list[1];
359 list->alloc = (mp_emergency_exception_buf + mp_emergency_exception_buf_size - (byte *)list->items) / sizeof(list->items[0]);
360 list->len = 0;
361
362 o->traceback = list;
363 }
364 }
365#endif // MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200366 } else {
Damien Georgef0954e32014-04-10 14:38:25 +0100367 o->base.type = exc_type;
368 o->traceback = MP_OBJ_NULL;
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300369 o->args = mp_obj_new_tuple(1, NULL);
Damien Georgef0954e32014-04-10 14:38:25 +0100370
371 if (fmt == NULL) {
372 // no message
373 assert(0);
374 } else {
375 // render exception message and store as .args[0]
376 // TODO: optimize bufferbloat
377 vstr_t *vstr = vstr_new();
378 va_list ap;
379 va_start(ap, fmt);
380 vstr_vprintf(vstr, fmt, ap);
381 va_end(ap);
Damien George2617eeb2014-05-25 22:27:57 +0100382 o->args->items[0] = mp_obj_new_str(vstr->buf, vstr->len, false);
Damien Georgef0954e32014-04-10 14:38:25 +0100383 vstr_free(vstr);
384 }
Damien George6c73ca12014-01-08 18:11:23 +0000385 }
Damien George6c73ca12014-01-08 18:11:23 +0000386
387 return o;
388}
389
Damien Georgec5966122014-02-15 16:10:44 +0000390// return true if the given object is an exception type
Damien Georgec5966122014-02-15 16:10:44 +0000391bool mp_obj_is_exception_type(mp_obj_t self_in) {
392 if (MP_OBJ_IS_TYPE(self_in, &mp_type_type)) {
Damien George9e6e9352014-03-26 18:37:06 +0000393 // optimisation when self_in is a builtin exception
Damien Georgec5966122014-02-15 16:10:44 +0000394 mp_obj_type_t *self = self_in;
Damien George9e6e9352014-03-26 18:37:06 +0000395 if (self->make_new == mp_obj_exception_make_new) {
396 return true;
397 }
Damien Georgec5966122014-02-15 16:10:44 +0000398 }
Damien George9e6e9352014-03-26 18:37:06 +0000399 return mp_obj_is_subclass_fast(self_in, &mp_type_BaseException);
Damien Georgec5966122014-02-15 16:10:44 +0000400}
401
402// return true if the given object is an instance of an exception type
Damien Georgec5966122014-02-15 16:10:44 +0000403bool mp_obj_is_exception_instance(mp_obj_t self_in) {
Damien George9e6e9352014-03-26 18:37:06 +0000404 return mp_obj_is_exception_type(mp_obj_get_type(self_in));
Damien Georgec5966122014-02-15 16:10:44 +0000405}
406
Damien George4bcd04b2014-09-24 14:05:40 +0100407// Return true if exception (type or instance) is a subclass of given
408// exception type. Assumes exc_type is a subclass of BaseException, as
409// defined by mp_obj_is_exception_type(exc_type).
410bool mp_obj_exception_match(mp_obj_t exc, mp_const_obj_t exc_type) {
411 // if exc is an instance of an exception, then extract and use its type
412 if (mp_obj_is_exception_instance(exc)) {
413 exc = mp_obj_get_type(exc);
414 }
415 return mp_obj_is_subclass_fast(exc, exc_type);
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200416}
417
Paul Sokolovsky91e556a2014-05-02 02:27:00 +0300418// traceback handling functions
Damien Georgec5966122014-02-15 16:10:44 +0000419
Paul Sokolovsky91e556a2014-05-02 02:27:00 +0300420#define GET_NATIVE_EXCEPTION(self, self_in) \
421 /* make sure self_in is an exception instance */ \
422 assert(mp_obj_is_exception_instance(self_in)); \
423 mp_obj_exception_t *self; \
424 if (mp_obj_is_native_exception_instance(self_in)) { \
425 self = self_in; \
426 } else { \
427 self = ((mp_obj_instance_t*)self_in)->subobj[0]; \
Damien George9e6e9352014-03-26 18:37:06 +0000428 }
Paul Sokolovsky91e556a2014-05-02 02:27:00 +0300429
430void mp_obj_exception_clear_traceback(mp_obj_t self_in) {
431 GET_NATIVE_EXCEPTION(self, self_in);
432 // just set the traceback to the null object
433 // we don't want to call any memory management functions here
434 self->traceback = MP_OBJ_NULL;
Damienb86e3f92013-12-29 17:17:43 +0000435}
Damien George08335002014-01-18 23:24:36 +0000436
Damien George40f3c022014-07-03 13:25:24 +0100437void 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 -0700438 GET_NATIVE_EXCEPTION(self, self_in);
439
Damien Georgef0b29722014-07-01 14:28:09 +0100440 #if MICROPY_ENABLE_GC
Dave Hylands2fe841d2014-06-30 22:49:21 -0700441 if (gc_is_locked()) {
Dave Hylands5b7fd202014-07-01 23:46:53 -0700442 if (self->traceback == MP_OBJ_NULL) {
443 // We can't allocate any memory, and no memory has been
444 // pre-allocated, so there is nothing else we can do.
445 return;
446 }
447 mp_obj_list_t *list = self->traceback;
448 if (list->alloc <= (list->len + 3)) {
449 // There is some preallocated memory, but not enough to store an
450 // entire record.
451 return;
452 }
Dave Hylands2fe841d2014-06-30 22:49:21 -0700453 }
Damien Georgef0b29722014-07-01 14:28:09 +0100454 #endif
455
Paul Sokolovsky91e556a2014-05-02 02:27:00 +0300456 // for traceback, we are just using the list object for convenience, it's not really a list of Python objects
457 if (self->traceback == MP_OBJ_NULL) {
458 self->traceback = mp_obj_new_list(0, NULL);
Damien George08335002014-01-18 23:24:36 +0000459 }
Damien George40f3c022014-07-03 13:25:24 +0100460 mp_obj_list_append(self->traceback, (mp_obj_t)(mp_uint_t)file);
461 mp_obj_list_append(self->traceback, (mp_obj_t)(mp_uint_t)line);
462 mp_obj_list_append(self->traceback, (mp_obj_t)(mp_uint_t)block);
Damien George08335002014-01-18 23:24:36 +0000463}
464
Damien George40f3c022014-07-03 13:25:24 +0100465void 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 +0300466 GET_NATIVE_EXCEPTION(self, self_in);
Damien Georgec5966122014-02-15 16:10:44 +0000467
Damien George136b1492014-01-19 12:38:49 +0000468 if (self->traceback == MP_OBJ_NULL) {
469 *n = 0;
470 *values = NULL;
471 } else {
Damien George9c4cbe22014-08-30 14:04:14 +0100472 mp_uint_t n2;
Damien George136b1492014-01-19 12:38:49 +0000473 mp_obj_list_get(self->traceback, &n2, (mp_obj_t**)values);
474 *n = n2;
475 }
Damien George08335002014-01-18 23:24:36 +0000476}