blob: dcc7800dcabdf9a35b45947a05d8730039dfc0bf [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"
Damien Georged45e5f82016-05-12 13:20:40 +010039#include "py/mperrno.h"
Damiend99b0522013-12-21 18:17:45 +000040
Damien George6902eed2014-04-04 10:52:59 +000041// Instance of MemoryError exception - needed by mp_malloc_fail
Damien George999cedb2015-11-27 17:01:44 +000042const mp_obj_exception_t mp_const_MemoryError_obj = {{&mp_type_MemoryError}, 0, 0, NULL, (mp_obj_tuple_t*)&mp_const_empty_tuple_obj};
Damien George6902eed2014-04-04 10:52:59 +000043
Dave Hylands5b7fd202014-07-01 23:46:53 -070044// Optionally allocated buffer for storing the first argument of an exception
45// allocated when the heap is locked.
46#if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF
47# if MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE > 0
Dave Hylands5b7fd202014-07-01 23:46:53 -070048#define mp_emergency_exception_buf_size MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE
49
50void mp_init_emergency_exception_buf(void) {
51 // Nothing to do since the buffer was declared statically. We put this
52 // definition here so that the calling code can call this function
53 // regardless of how its configured (makes the calling code a bit cleaner).
54}
55
56#else
Damien Georgeb4b10fd2015-01-01 23:30:53 +000057#define mp_emergency_exception_buf_size MP_STATE_VM(mp_emergency_exception_buf_size)
Dave Hylands5b7fd202014-07-01 23:46:53 -070058
59void mp_init_emergency_exception_buf(void) {
60 mp_emergency_exception_buf_size = 0;
Damien Georgeb4b10fd2015-01-01 23:30:53 +000061 MP_STATE_VM(mp_emergency_exception_buf) = NULL;
Dave Hylands5b7fd202014-07-01 23:46:53 -070062}
63
64mp_obj_t mp_alloc_emergency_exception_buf(mp_obj_t size_in) {
65 mp_int_t size = mp_obj_get_int(size_in);
66 void *buf = NULL;
67 if (size > 0) {
Damien George4d77e1a2015-02-27 09:34:51 +000068 buf = m_new(byte, size);
Dave Hylands5b7fd202014-07-01 23:46:53 -070069 }
70
71 int old_size = mp_emergency_exception_buf_size;
Damien Georgeb4b10fd2015-01-01 23:30:53 +000072 void *old_buf = MP_STATE_VM(mp_emergency_exception_buf);
Dave Hylands5b7fd202014-07-01 23:46:53 -070073
74 // Update the 2 variables atomically so that an interrupt can't occur
75 // between the assignments.
Damien George4859edb2014-10-15 17:33:24 +000076 mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
Dave Hylands5b7fd202014-07-01 23:46:53 -070077 mp_emergency_exception_buf_size = size;
Damien Georgeb4b10fd2015-01-01 23:30:53 +000078 MP_STATE_VM(mp_emergency_exception_buf) = buf;
Damien George4859edb2014-10-15 17:33:24 +000079 MICROPY_END_ATOMIC_SECTION(atomic_state);
Dave Hylands5b7fd202014-07-01 23:46:53 -070080
81 if (old_buf != NULL) {
Damien George4d77e1a2015-02-27 09:34:51 +000082 m_del(byte, old_buf, old_size);
Dave Hylands5b7fd202014-07-01 23:46:53 -070083 }
84 return mp_const_none;
85}
86#endif
87#endif // MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF
88
Paul Sokolovskyc4d589e2014-03-28 02:37:28 +020089// Instance of GeneratorExit exception - needed by generator.close()
90// This would belong to objgenerator.c, but to keep mp_obj_exception_t
91// definition module-private so far, have it here.
Damien George999cedb2015-11-27 17:01:44 +000092const mp_obj_exception_t mp_const_GeneratorExit_obj = {{&mp_type_GeneratorExit}, 0, 0, NULL, (mp_obj_tuple_t*)&mp_const_empty_tuple_obj};
Paul Sokolovskyc4d589e2014-03-28 02:37:28 +020093
Damien George7f9d1d62015-04-09 23:56:15 +010094STATIC void mp_obj_exception_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
Damien George999cedb2015-11-27 17:01:44 +000095 mp_obj_exception_t *o = MP_OBJ_TO_PTR(o_in);
Paul Sokolovskyd8351ca2014-05-02 01:51:25 +030096 mp_print_kind_t k = kind & ~PRINT_EXC_SUBCLASS;
97 bool is_subclass = kind & PRINT_EXC_SUBCLASS;
98 if (!is_subclass && (k == PRINT_REPR || k == PRINT_EXC)) {
Damien George044c4732015-04-11 13:03:37 +010099 mp_print_str(print, qstr_str(o->base.type->name));
Damiend99b0522013-12-21 18:17:45 +0000100 }
Paul Sokolovskyd8351ca2014-05-02 01:51:25 +0300101
102 if (k == PRINT_EXC) {
Damien George7f9d1d62015-04-09 23:56:15 +0100103 mp_print_str(print, ": ");
Paul Sokolovskyd8351ca2014-05-02 01:51:25 +0300104 }
105
106 if (k == PRINT_STR || k == PRINT_EXC) {
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300107 if (o->args == NULL || o->args->len == 0) {
Damien George7f9d1d62015-04-09 23:56:15 +0100108 mp_print_str(print, "");
Paul Sokolovskya96d3d02014-03-31 01:10:10 +0300109 return;
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300110 } else if (o->args->len == 1) {
Damien George9a924992016-05-12 14:27:52 +0100111 #if MICROPY_PY_UERRNO
112 // try to provide a nice OSError error message
113 if (o->base.type == &mp_type_OSError && MP_OBJ_IS_SMALL_INT(o->args->items[0])) {
114 qstr qst = mp_errno_to_str(o->args->items[0]);
115 if (qst != MP_QSTR_NULL) {
116 mp_printf(print, "[Errno %d] %q", MP_OBJ_SMALL_INT_VALUE(o->args->items[0]), qst);
117 return;
118 }
119 }
120 #endif
Damien George7f9d1d62015-04-09 23:56:15 +0100121 mp_obj_print_helper(print, o->args->items[0], PRINT_STR);
Paul Sokolovskya96d3d02014-03-31 01:10:10 +0300122 return;
123 }
124 }
Damien George999cedb2015-11-27 17:01:44 +0000125 mp_obj_tuple_print(print, MP_OBJ_FROM_PTR(o->args), kind);
Damiend99b0522013-12-21 18:17:45 +0000126}
127
Damien George5b3f0b72016-01-03 15:55:55 +0000128mp_obj_t mp_obj_exception_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
Damien George50149a52015-01-20 14:11:27 +0000129 mp_arg_check_num(n_args, n_kw, 0, MP_OBJ_FUN_ARGS_MAX, false);
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300130 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 +0100131 if (o == NULL) {
132 // Couldn't allocate heap memory; use local data instead.
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000133 o = &MP_STATE_VM(mp_emergency_exception_obj);
Damien Georgef0954e32014-04-10 14:38:25 +0100134 // We can't store any args.
Damien George999cedb2015-11-27 17:01:44 +0000135 o->args = (mp_obj_tuple_t*)&mp_const_empty_tuple_obj;
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300136 } else {
Damien George999cedb2015-11-27 17:01:44 +0000137 o->args = MP_OBJ_TO_PTR(mp_obj_new_tuple(n_args, args));
Damien Georgef0954e32014-04-10 14:38:25 +0100138 }
Damien George5b3f0b72016-01-03 15:55:55 +0000139 o->base.type = type;
Damien George4852e092015-02-27 00:36:39 +0000140 o->traceback_data = NULL;
Damien George999cedb2015-11-27 17:01:44 +0000141 return MP_OBJ_FROM_PTR(o);
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200142}
143
Paul Sokolovskyaf1ae302014-03-26 19:17:20 +0200144// Get exception "value" - that is, first argument, or None
145mp_obj_t mp_obj_exception_get_value(mp_obj_t self_in) {
Damien George999cedb2015-11-27 17:01:44 +0000146 mp_obj_exception_t *self = MP_OBJ_TO_PTR(self_in);
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300147 if (self->args->len == 0) {
Paul Sokolovskyaf1ae302014-03-26 19:17:20 +0200148 return mp_const_none;
149 } else {
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300150 return self->args->items[0];
Paul Sokolovskyaf1ae302014-03-26 19:17:20 +0200151 }
152}
153
Damien Georgeb1bbe962015-04-01 14:10:50 +0000154STATIC void exception_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
Paul Sokolovskyc3d96d32016-11-14 02:23:30 +0300155 mp_obj_exception_t *self = MP_OBJ_TO_PTR(self_in);
Damien Georgeb1bbe962015-04-01 14:10:50 +0000156 if (dest[0] != MP_OBJ_NULL) {
Paul Sokolovskyc3d96d32016-11-14 02:23:30 +0300157 // store/delete attribute
158 if (attr == MP_QSTR___traceback__ && dest[1] == mp_const_none) {
159 // We allow 'exc.__traceback__ = None' assignment as low-level
160 // optimization of pre-allocating exception instance and raising
161 // it repeatedly - this avoids memory allocation during raise.
162 // However, uPy will keep adding traceback entries to such
163 // exception instance, so before throwing it, traceback should
164 // be cleared like above.
165 self->traceback_len = 0;
166 dest[0] = MP_OBJ_NULL; // indicate success
167 }
Damien Georgeb1bbe962015-04-01 14:10:50 +0000168 return;
169 }
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +0200170 if (attr == MP_QSTR_args) {
Damien George999cedb2015-11-27 17:01:44 +0000171 dest[0] = MP_OBJ_FROM_PTR(self->args);
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +0200172 } else if (self->base.type == &mp_type_StopIteration && attr == MP_QSTR_value) {
Damien George999cedb2015-11-27 17:01:44 +0000173 dest[0] = mp_obj_exception_get_value(self_in);
Paul Sokolovsky9512e9e2014-03-25 01:29:09 +0200174 }
175}
176
Damien George4b72b3a2016-01-03 14:21:40 +0000177STATIC mp_obj_t exc___init__(size_t n_args, const mp_obj_t *args) {
Damien George999cedb2015-11-27 17:01:44 +0000178 mp_obj_exception_t *self = MP_OBJ_TO_PTR(args[0]);
Paul Sokolovsky52386ca2014-05-18 20:44:04 +0300179 mp_obj_t argst = mp_obj_new_tuple(n_args - 1, args + 1);
Damien George999cedb2015-11-27 17:01:44 +0000180 self->args = MP_OBJ_TO_PTR(argst);
Paul Sokolovsky52386ca2014-05-18 20:44:04 +0300181 return mp_const_none;
182}
183STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(exc___init___obj, 1, MP_OBJ_FUN_ARGS_MAX, exc___init__);
184
Damien Georgecbf76742015-11-27 13:38:15 +0000185STATIC const mp_rom_map_elem_t exc_locals_dict_table[] = {
186 { MP_ROM_QSTR(MP_QSTR___init__), MP_ROM_PTR(&exc___init___obj) },
Paul Sokolovsky52386ca2014-05-18 20:44:04 +0300187};
188
189STATIC MP_DEFINE_CONST_DICT(exc_locals_dict, exc_locals_dict_table);
190
Damien Georgec5966122014-02-15 16:10:44 +0000191const mp_obj_type_t mp_type_BaseException = {
192 { &mp_type_type },
193 .name = MP_QSTR_BaseException,
194 .print = mp_obj_exception_print,
195 .make_new = mp_obj_exception_make_new,
Damien Georgeb1bbe962015-04-01 14:10:50 +0000196 .attr = exception_attr,
Damien George999cedb2015-11-27 17:01:44 +0000197 .locals_dict = (mp_obj_dict_t*)&exc_locals_dict,
Damiend99b0522013-12-21 18:17:45 +0000198};
199
Damien George22a08652014-02-15 21:05:25 +0000200#define MP_DEFINE_EXCEPTION_BASE(base_name) \
Damien Georgecbf76742015-11-27 13:38:15 +0000201STATIC const mp_rom_obj_tuple_t mp_type_ ## base_name ## _base_tuple = {{&mp_type_tuple}, 1, {MP_ROM_PTR(&mp_type_ ## base_name)}};\
Damien George22a08652014-02-15 21:05:25 +0000202
203#define MP_DEFINE_EXCEPTION(exc_name, base_name) \
Damien Georgec5966122014-02-15 16:10:44 +0000204const mp_obj_type_t mp_type_ ## exc_name = { \
205 { &mp_type_type }, \
206 .name = MP_QSTR_ ## exc_name, \
207 .print = mp_obj_exception_print, \
208 .make_new = mp_obj_exception_make_new, \
Damien Georgeb1bbe962015-04-01 14:10:50 +0000209 .attr = exception_attr, \
Damien George999cedb2015-11-27 17:01:44 +0000210 .bases_tuple = (mp_obj_tuple_t*)(mp_rom_obj_tuple_t*)&mp_type_ ## base_name ## _base_tuple, \
Damien Georgec5966122014-02-15 16:10:44 +0000211};
212
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000213// List of all exceptions, arranged as in the table at:
Chris Angelicodaf973a2014-06-06 03:51:03 +1000214// http://docs.python.org/3/library/exceptions.html
Damien George22a08652014-02-15 21:05:25 +0000215MP_DEFINE_EXCEPTION_BASE(BaseException)
Damien George7a4ddd22014-05-24 23:32:19 +0100216MP_DEFINE_EXCEPTION(SystemExit, BaseException)
Damien George124df6f2014-10-25 18:19:55 +0100217MP_DEFINE_EXCEPTION(KeyboardInterrupt, BaseException)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000218MP_DEFINE_EXCEPTION(GeneratorExit, BaseException)
219MP_DEFINE_EXCEPTION(Exception, BaseException)
220 MP_DEFINE_EXCEPTION_BASE(Exception)
pohmelie81ebba72016-01-27 23:23:11 +0300221 #if MICROPY_PY_ASYNC_AWAIT
222 MP_DEFINE_EXCEPTION(StopAsyncIteration, Exception)
223 #endif
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000224 MP_DEFINE_EXCEPTION(StopIteration, Exception)
225 MP_DEFINE_EXCEPTION(ArithmeticError, Exception)
226 MP_DEFINE_EXCEPTION_BASE(ArithmeticError)
Damien Georgec63f9842014-03-27 23:49:06 +0000227 //MP_DEFINE_EXCEPTION(FloatingPointError, ArithmeticError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000228 MP_DEFINE_EXCEPTION(OverflowError, ArithmeticError)
229 MP_DEFINE_EXCEPTION(ZeroDivisionError, ArithmeticError)
230 MP_DEFINE_EXCEPTION(AssertionError, Exception)
231 MP_DEFINE_EXCEPTION(AttributeError, Exception)
Damien Georgec63f9842014-03-27 23:49:06 +0000232 //MP_DEFINE_EXCEPTION(BufferError, Exception)
Damien George8b03d942014-09-30 13:59:30 +0000233 //MP_DEFINE_EXCEPTION(EnvironmentError, Exception) use OSError instead
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000234 MP_DEFINE_EXCEPTION(EOFError, Exception)
235 MP_DEFINE_EXCEPTION(ImportError, Exception)
Damien George8b03d942014-09-30 13:59:30 +0000236 //MP_DEFINE_EXCEPTION(IOError, Exception) use OSError instead
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000237 MP_DEFINE_EXCEPTION(LookupError, Exception)
238 MP_DEFINE_EXCEPTION_BASE(LookupError)
239 MP_DEFINE_EXCEPTION(IndexError, LookupError)
240 MP_DEFINE_EXCEPTION(KeyError, LookupError)
241 MP_DEFINE_EXCEPTION(MemoryError, Exception)
242 MP_DEFINE_EXCEPTION(NameError, Exception)
Damien Georgec63f9842014-03-27 23:49:06 +0000243 /*
Antonin ENFRUNda1fffa2014-05-12 00:21:50 +0200244 MP_DEFINE_EXCEPTION_BASE(NameError)
245 MP_DEFINE_EXCEPTION(UnboundLocalError, NameError)
246 */
247 MP_DEFINE_EXCEPTION(OSError, Exception)
Daniel Campora077812b2015-06-29 22:45:39 +0200248#if MICROPY_PY_BUILTINS_TIMEOUTERROR
Antonin ENFRUNda1fffa2014-05-12 00:21:50 +0200249 MP_DEFINE_EXCEPTION_BASE(OSError)
Daniel Campora077812b2015-06-29 22:45:39 +0200250 MP_DEFINE_EXCEPTION(TimeoutError, OSError)
251#endif
252 /*
Damien Georgec63f9842014-03-27 23:49:06 +0000253 MP_DEFINE_EXCEPTION(BlockingIOError, OSError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000254 MP_DEFINE_EXCEPTION(ChildProcessError, OSError)
255 MP_DEFINE_EXCEPTION(ConnectionError, OSError)
256 MP_DEFINE_EXCEPTION(BrokenPipeError, ConnectionError)
257 MP_DEFINE_EXCEPTION(ConnectionAbortedError, ConnectionError)
258 MP_DEFINE_EXCEPTION(ConnectionRefusedError, ConnectionError)
259 MP_DEFINE_EXCEPTION(ConnectionResetError, ConnectionError)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000260 MP_DEFINE_EXCEPTION(InterruptedError, OSError)
261 MP_DEFINE_EXCEPTION(IsADirectoryError, OSError)
262 MP_DEFINE_EXCEPTION(NotADirectoryError, OSError)
263 MP_DEFINE_EXCEPTION(PermissionError, OSError)
264 MP_DEFINE_EXCEPTION(ProcessLookupError, OSError)
Damien Georgec9109722014-03-22 23:40:02 +0000265 MP_DEFINE_EXCEPTION(FileExistsError, OSError)
266 MP_DEFINE_EXCEPTION(FileNotFoundError, OSError)
Damien Georgec63f9842014-03-27 23:49:06 +0000267 MP_DEFINE_EXCEPTION(ReferenceError, Exception)
268 */
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000269 MP_DEFINE_EXCEPTION(RuntimeError, Exception)
270 MP_DEFINE_EXCEPTION_BASE(RuntimeError)
271 MP_DEFINE_EXCEPTION(NotImplementedError, RuntimeError)
272 MP_DEFINE_EXCEPTION(SyntaxError, Exception)
273 MP_DEFINE_EXCEPTION_BASE(SyntaxError)
274 MP_DEFINE_EXCEPTION(IndentationError, SyntaxError)
Damien Georgec63f9842014-03-27 23:49:06 +0000275 /*
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000276 MP_DEFINE_EXCEPTION_BASE(IndentationError)
Damien Georgec63f9842014-03-27 23:49:06 +0000277 MP_DEFINE_EXCEPTION(TabError, IndentationError)
278 */
Damien Georgee7a47822014-10-22 19:42:55 +0100279 //MP_DEFINE_EXCEPTION(SystemError, Exception)
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000280 MP_DEFINE_EXCEPTION(TypeError, Exception)
Damien Georgec8b60f02015-04-20 13:29:31 +0000281#if MICROPY_EMIT_NATIVE
282 MP_DEFINE_EXCEPTION_BASE(TypeError)
283 MP_DEFINE_EXCEPTION(ViperTypeError, TypeError)
284#endif
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000285 MP_DEFINE_EXCEPTION(ValueError, Exception)
Paul Sokolovsky71ebd4b2015-02-23 23:18:36 +0200286#if MICROPY_PY_BUILTINS_STR_UNICODE
287 MP_DEFINE_EXCEPTION_BASE(ValueError)
288 MP_DEFINE_EXCEPTION(UnicodeError, ValueError)
289 //TODO: Implement more UnicodeError subclasses which take arguments
290#endif
Damien Georgec9109722014-03-22 23:40:02 +0000291 /*
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000292 MP_DEFINE_EXCEPTION(Warning, Exception)
293 MP_DEFINE_EXCEPTION_BASE(Warning)
294 MP_DEFINE_EXCEPTION(DeprecationWarning, Warning)
295 MP_DEFINE_EXCEPTION(PendingDeprecationWarning, Warning)
296 MP_DEFINE_EXCEPTION(RuntimeWarning, Warning)
297 MP_DEFINE_EXCEPTION(SyntaxWarning, Warning)
298 MP_DEFINE_EXCEPTION(UserWarning, Warning)
299 MP_DEFINE_EXCEPTION(FutureWarning, Warning)
300 MP_DEFINE_EXCEPTION(ImportWarning, Warning)
301 MP_DEFINE_EXCEPTION(UnicodeWarning, Warning)
302 MP_DEFINE_EXCEPTION(BytesWarning, Warning)
303 MP_DEFINE_EXCEPTION(ResourceWarning, Warning)
Damien Georgec9109722014-03-22 23:40:02 +0000304 */
Damien Georgec5966122014-02-15 16:10:44 +0000305
306mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type) {
Paul Sokolovskya96d3d02014-03-31 01:10:10 +0300307 return mp_obj_new_exception_args(exc_type, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000308}
309
Paul Sokolovskydec31bb2014-04-22 00:01:13 +0300310// "Optimized" version for common(?) case of having 1 exception arg
311mp_obj_t mp_obj_new_exception_arg1(const mp_obj_type_t *exc_type, mp_obj_t arg) {
312 return mp_obj_new_exception_args(exc_type, 1, &arg);
313}
314
Damien Georgefa5a5912017-02-16 16:38:14 +1100315mp_obj_t mp_obj_new_exception_args(const mp_obj_type_t *exc_type, size_t n_args, const mp_obj_t *args) {
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200316 assert(exc_type->make_new == mp_obj_exception_make_new);
Damien George5b3f0b72016-01-03 15:55:55 +0000317 return exc_type->make_new(exc_type, n_args, 0, args);
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200318}
319
Damien Georgec5966122014-02-15 16:10:44 +0000320mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, const char *msg) {
321 return mp_obj_new_exception_msg_varg(exc_type, msg);
Damiend99b0522013-12-21 18:17:45 +0000322}
323
Damien Georgec5966122014-02-15 16:10:44 +0000324mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char *fmt, ...) {
325 // check that the given type is an exception type
326 assert(exc_type->make_new == mp_obj_exception_make_new);
327
Damien George6c73ca12014-01-08 18:11:23 +0000328 // make exception object
Paul Sokolovsky1acf22f2014-04-23 02:19:18 +0300329 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 +0100330 if (o == NULL) {
331 // Couldn't allocate heap memory; use local data instead.
332 // Unfortunately, we won't be able to format the string...
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000333 o = &MP_STATE_VM(mp_emergency_exception_obj);
Damien Georgef0954e32014-04-10 14:38:25 +0100334 o->base.type = exc_type;
Damien George4852e092015-02-27 00:36:39 +0000335 o->traceback_data = NULL;
Damien George999cedb2015-11-27 17:01:44 +0000336 o->args = (mp_obj_tuple_t*)&mp_const_empty_tuple_obj;
Dave Hylands5b7fd202014-07-01 23:46:53 -0700337
338#if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF
339 // If the user has provided a buffer, then we try to create a tuple
340 // of length 1, which has a string object and the string data.
341
342 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 +0000343 mp_obj_tuple_t *tuple = (mp_obj_tuple_t *)MP_STATE_VM(mp_emergency_exception_buf);
Dave Hylands5b7fd202014-07-01 23:46:53 -0700344 mp_obj_str_t *str = (mp_obj_str_t *)&tuple->items[1];
345
346 tuple->base.type = &mp_type_tuple;
347 tuple->len = 1;
Damien George999cedb2015-11-27 17:01:44 +0000348 tuple->items[0] = MP_OBJ_FROM_PTR(str);
Dave Hylands5b7fd202014-07-01 23:46:53 -0700349
350 byte *str_data = (byte *)&str[1];
Damien Georgeee86de12017-04-10 16:02:56 +1000351 size_t max_len = (byte*)MP_STATE_VM(mp_emergency_exception_buf) + mp_emergency_exception_buf_size
Dave Hylands5b7fd202014-07-01 23:46:53 -0700352 - str_data;
353
Damien George044c4732015-04-11 13:03:37 +0100354 vstr_t vstr;
355 vstr_init_fixed_buf(&vstr, max_len, (char *)str_data);
356
Dave Hylands5b7fd202014-07-01 23:46:53 -0700357 va_list ap;
358 va_start(ap, fmt);
Damien George044c4732015-04-11 13:03:37 +0100359 vstr_vprintf(&vstr, fmt, ap);
Dave Hylands5b7fd202014-07-01 23:46:53 -0700360 va_end(ap);
361
362 str->base.type = &mp_type_str;
363 str->hash = qstr_compute_hash(str_data, str->len);
Damien George044c4732015-04-11 13:03:37 +0100364 str->len = vstr.len;
Dave Hylands5b7fd202014-07-01 23:46:53 -0700365 str->data = str_data;
366
367 o->args = tuple;
368
Damien Georgeee86de12017-04-10 16:02:56 +1000369 size_t offset = &str_data[str->len] - (byte*)MP_STATE_VM(mp_emergency_exception_buf);
Dave Hylands5b7fd202014-07-01 23:46:53 -0700370 offset += sizeof(void *) - 1;
371 offset &= ~(sizeof(void *) - 1);
372
Damien George3d2daa22016-01-02 22:04:12 +0000373 if ((mp_emergency_exception_buf_size - offset) > (sizeof(o->traceback_data[0]) * 3)) {
Dave Hylands5b7fd202014-07-01 23:46:53 -0700374 // We have room to store some traceback.
Damien George3d2daa22016-01-02 22:04:12 +0000375 o->traceback_data = (size_t*)((byte *)MP_STATE_VM(mp_emergency_exception_buf) + offset);
Damien Georgeee86de12017-04-10 16:02:56 +1000376 o->traceback_alloc = ((byte*)MP_STATE_VM(mp_emergency_exception_buf) + mp_emergency_exception_buf_size - (byte *)o->traceback_data) / sizeof(o->traceback_data[0]);
Damien George4852e092015-02-27 00:36:39 +0000377 o->traceback_len = 0;
Dave Hylands5b7fd202014-07-01 23:46:53 -0700378 }
379 }
380#endif // MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF
Paul Sokolovskyddf21782014-01-12 23:30:20 +0200381 } else {
Damien Georgef0954e32014-04-10 14:38:25 +0100382 o->base.type = exc_type;
Damien George4852e092015-02-27 00:36:39 +0000383 o->traceback_data = NULL;
Damien George999cedb2015-11-27 17:01:44 +0000384 o->args = MP_OBJ_TO_PTR(mp_obj_new_tuple(1, NULL));
Damien Georgef0954e32014-04-10 14:38:25 +0100385
Damien George5bea0802017-01-17 17:03:16 +1100386 assert(fmt != NULL);
387 {
Paul Sokolovsky29c4f922015-02-15 22:19:30 +0300388 if (strchr(fmt, '%') == NULL) {
389 // no formatting substitutions, avoid allocating vstr.
390 o->args->items[0] = mp_obj_new_str(fmt, strlen(fmt), false);
391 } else {
392 // render exception message and store as .args[0]
393 va_list ap;
394 vstr_t vstr;
395 vstr_init(&vstr, 16);
396 va_start(ap, fmt);
397 vstr_vprintf(&vstr, fmt, ap);
398 va_end(ap);
399 o->args->items[0] = mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
400 }
Damien Georgef0954e32014-04-10 14:38:25 +0100401 }
Damien George6c73ca12014-01-08 18:11:23 +0000402 }
Damien George6c73ca12014-01-08 18:11:23 +0000403
Damien George999cedb2015-11-27 17:01:44 +0000404 return MP_OBJ_FROM_PTR(o);
Damien George6c73ca12014-01-08 18:11:23 +0000405}
406
Damien Georgec5966122014-02-15 16:10:44 +0000407// return true if the given object is an exception type
Damien Georgec5966122014-02-15 16:10:44 +0000408bool mp_obj_is_exception_type(mp_obj_t self_in) {
409 if (MP_OBJ_IS_TYPE(self_in, &mp_type_type)) {
Damien George9e6e9352014-03-26 18:37:06 +0000410 // optimisation when self_in is a builtin exception
Damien George999cedb2015-11-27 17:01:44 +0000411 mp_obj_type_t *self = MP_OBJ_TO_PTR(self_in);
Damien George9e6e9352014-03-26 18:37:06 +0000412 if (self->make_new == mp_obj_exception_make_new) {
413 return true;
414 }
Damien Georgec5966122014-02-15 16:10:44 +0000415 }
Damien George999cedb2015-11-27 17:01:44 +0000416 return mp_obj_is_subclass_fast(self_in, MP_OBJ_FROM_PTR(&mp_type_BaseException));
Damien Georgec5966122014-02-15 16:10:44 +0000417}
418
419// return true if the given object is an instance of an exception type
Damien Georgec5966122014-02-15 16:10:44 +0000420bool mp_obj_is_exception_instance(mp_obj_t self_in) {
Damien George999cedb2015-11-27 17:01:44 +0000421 return mp_obj_is_exception_type(MP_OBJ_FROM_PTR(mp_obj_get_type(self_in)));
Damien Georgec5966122014-02-15 16:10:44 +0000422}
423
Damien George4bcd04b2014-09-24 14:05:40 +0100424// Return true if exception (type or instance) is a subclass of given
425// exception type. Assumes exc_type is a subclass of BaseException, as
426// defined by mp_obj_is_exception_type(exc_type).
427bool mp_obj_exception_match(mp_obj_t exc, mp_const_obj_t exc_type) {
428 // if exc is an instance of an exception, then extract and use its type
429 if (mp_obj_is_exception_instance(exc)) {
Damien George999cedb2015-11-27 17:01:44 +0000430 exc = MP_OBJ_FROM_PTR(mp_obj_get_type(exc));
Damien George4bcd04b2014-09-24 14:05:40 +0100431 }
432 return mp_obj_is_subclass_fast(exc, exc_type);
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200433}
434
Paul Sokolovsky91e556a2014-05-02 02:27:00 +0300435// traceback handling functions
Damien Georgec5966122014-02-15 16:10:44 +0000436
Paul Sokolovsky91e556a2014-05-02 02:27:00 +0300437#define GET_NATIVE_EXCEPTION(self, self_in) \
438 /* make sure self_in is an exception instance */ \
439 assert(mp_obj_is_exception_instance(self_in)); \
440 mp_obj_exception_t *self; \
441 if (mp_obj_is_native_exception_instance(self_in)) { \
Damien George999cedb2015-11-27 17:01:44 +0000442 self = MP_OBJ_TO_PTR(self_in); \
Paul Sokolovsky91e556a2014-05-02 02:27:00 +0300443 } else { \
Damien George999cedb2015-11-27 17:01:44 +0000444 self = MP_OBJ_TO_PTR(((mp_obj_instance_t*)MP_OBJ_TO_PTR(self_in))->subobj[0]); \
Damien George9e6e9352014-03-26 18:37:06 +0000445 }
Paul Sokolovsky91e556a2014-05-02 02:27:00 +0300446
447void mp_obj_exception_clear_traceback(mp_obj_t self_in) {
448 GET_NATIVE_EXCEPTION(self, self_in);
449 // just set the traceback to the null object
450 // we don't want to call any memory management functions here
Damien George4852e092015-02-27 00:36:39 +0000451 self->traceback_data = NULL;
Damienb86e3f92013-12-29 17:17:43 +0000452}
Damien George08335002014-01-18 23:24:36 +0000453
Damien George3d2daa22016-01-02 22:04:12 +0000454void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, size_t line, qstr block) {
Dave Hylands5b7fd202014-07-01 23:46:53 -0700455 GET_NATIVE_EXCEPTION(self, self_in);
456
Damien George4852e092015-02-27 00:36:39 +0000457 // append this traceback info to traceback data
458 // if memory allocation fails (eg because gc is locked), just return
Damien Georgef0b29722014-07-01 14:28:09 +0100459
Damien George4852e092015-02-27 00:36:39 +0000460 if (self->traceback_data == NULL) {
Damien George3d2daa22016-01-02 22:04:12 +0000461 self->traceback_data = m_new_maybe(size_t, 3);
Damien George4852e092015-02-27 00:36:39 +0000462 if (self->traceback_data == NULL) {
Paul Sokolovskyfa3b8952015-02-15 22:24:03 +0300463 return;
464 }
Damien George4852e092015-02-27 00:36:39 +0000465 self->traceback_alloc = 3;
466 self->traceback_len = 0;
467 } else if (self->traceback_len + 3 > self->traceback_alloc) {
468 // be conservative with growing traceback data
Damien George3d2daa22016-01-02 22:04:12 +0000469 size_t *tb_data = m_renew_maybe(size_t, self->traceback_data, self->traceback_alloc, self->traceback_alloc + 3, true);
Damien George4852e092015-02-27 00:36:39 +0000470 if (tb_data == NULL) {
471 return;
472 }
473 self->traceback_data = tb_data;
474 self->traceback_alloc += 3;
Damien George08335002014-01-18 23:24:36 +0000475 }
Damien George4852e092015-02-27 00:36:39 +0000476
Damien George3d2daa22016-01-02 22:04:12 +0000477 size_t *tb_data = &self->traceback_data[self->traceback_len];
Damien George4852e092015-02-27 00:36:39 +0000478 self->traceback_len += 3;
Damien George3d2daa22016-01-02 22:04:12 +0000479 tb_data[0] = file;
480 tb_data[1] = line;
481 tb_data[2] = block;
Damien George08335002014-01-18 23:24:36 +0000482}
483
Damien George3d2daa22016-01-02 22:04:12 +0000484void mp_obj_exception_get_traceback(mp_obj_t self_in, size_t *n, size_t **values) {
Paul Sokolovsky91e556a2014-05-02 02:27:00 +0300485 GET_NATIVE_EXCEPTION(self, self_in);
Damien Georgec5966122014-02-15 16:10:44 +0000486
Damien George4852e092015-02-27 00:36:39 +0000487 if (self->traceback_data == NULL) {
Damien George136b1492014-01-19 12:38:49 +0000488 *n = 0;
489 *values = NULL;
490 } else {
Damien George4852e092015-02-27 00:36:39 +0000491 *n = self->traceback_len;
492 *values = self->traceback_data;
Damien George136b1492014-01-19 12:38:49 +0000493 }
Damien George08335002014-01-18 23:24:36 +0000494}