diff options
Diffstat (limited to 'py')
-rw-r--r-- | py/obj.h | 4 | ||||
-rw-r--r-- | py/objexcept.c | 56 | ||||
-rw-r--r-- | py/objgenerator.c | 70 | ||||
-rw-r--r-- | py/objgenerator.h | 1 | ||||
-rw-r--r-- | py/objtype.c | 4 | ||||
-rw-r--r-- | py/qstrdefs.h | 1 | ||||
-rw-r--r-- | py/runtime.c | 28 | ||||
-rw-r--r-- | py/vm.c | 10 |
8 files changed, 133 insertions, 41 deletions
@@ -240,6 +240,7 @@ mp_obj_t mp_obj_new_float(mp_float_t val); mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag); #endif mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type); +mp_obj_t mp_obj_new_exception_args(const mp_obj_type_t *exc_type, uint n_args, const mp_obj_t *args); mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, const char *msg); mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char *fmt, ...); // counts args by number of % symbols in fmt, excluding %%; can only handle void* sizes (ie no float/double!) mp_obj_t mp_obj_new_range(int start, int stop, int step); @@ -261,7 +262,7 @@ mp_obj_t mp_obj_new_module(qstr module_name); mp_obj_type_t *mp_obj_get_type(mp_obj_t o_in); const char *mp_obj_get_type_str(mp_obj_t o_in); -bool mp_obj_is_subclass_fast(mp_obj_t object, mp_obj_t classinfo); // arguments should be type objects +bool mp_obj_is_subclass_fast(mp_const_obj_t object, mp_const_obj_t classinfo); // arguments should be type objects void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind); void mp_obj_print(mp_obj_t o, mp_print_kind_t kind); @@ -310,6 +311,7 @@ machine_int_t mp_obj_int_get_checked(mp_obj_t self_in); // exception bool mp_obj_is_exception_type(mp_obj_t self_in); bool mp_obj_is_exception_instance(mp_obj_t self_in); +bool mp_obj_exception_match(mp_obj_t exc, const mp_obj_type_t *exc_type); void mp_obj_exception_clear_traceback(mp_obj_t self_in); void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, machine_uint_t line, qstr block); void mp_obj_exception_get_traceback(mp_obj_t self_in, machine_uint_t *n, machine_uint_t **values); diff --git a/py/objexcept.c b/py/objexcept.c index 8be3048fa2..facf209df8 100644 --- a/py/objexcept.c +++ b/py/objexcept.c @@ -8,6 +8,8 @@ #include "qstr.h" #include "obj.h" #include "objtuple.h" +#include "runtime.h" +#include "runtime0.h" // This is unified class for C-level and Python-level exceptions // Python-level exceptions have empty ->msg and all arguments are in @@ -178,6 +180,11 @@ mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type) { return mp_obj_new_exception_msg_varg(exc_type, NULL); } +mp_obj_t mp_obj_new_exception_args(const mp_obj_type_t *exc_type, uint n_args, const mp_obj_t *args) { + assert(exc_type->make_new == mp_obj_exception_make_new); + return exc_type->make_new((mp_obj_t)exc_type, n_args, 0, args); +} + mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, const char *msg) { return mp_obj_new_exception_msg_varg(exc_type, msg); } @@ -208,44 +215,55 @@ mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char } // return true if the given object is an exception type -// TODO make this work for user defined exceptions bool mp_obj_is_exception_type(mp_obj_t self_in) { if (MP_OBJ_IS_TYPE(self_in, &mp_type_type)) { + // optimisation when self_in is a builtin exception mp_obj_type_t *self = self_in; - return self->make_new == mp_obj_exception_make_new; - } else { - return false; + if (self->make_new == mp_obj_exception_make_new) { + return true; + } } + return mp_obj_is_subclass_fast(self_in, &mp_type_BaseException); } // return true if the given object is an instance of an exception type -// TODO make this work for user defined exceptions bool mp_obj_is_exception_instance(mp_obj_t self_in) { - return mp_obj_get_type(self_in)->make_new == mp_obj_exception_make_new; + return mp_obj_is_exception_type(mp_obj_get_type(self_in)); +} + +// return true if exception (type or instance) is a subclass of given +// exception type. +bool mp_obj_exception_match(mp_obj_t exc, const mp_obj_type_t *exc_type) { + // TODO: move implementation from RT_BINARY_OP_EXCEPTION_MATCH here. + return rt_binary_op(RT_BINARY_OP_EXCEPTION_MATCH, exc, (mp_obj_t)exc_type) == mp_const_true; } void mp_obj_exception_clear_traceback(mp_obj_t self_in) { // make sure self_in is an exception instance - assert(mp_obj_get_type(self_in)->make_new == mp_obj_exception_make_new); - mp_obj_exception_t *self = self_in; + // TODO add traceback information to user-defined exceptions (need proper builtin subclassing for that) + if (mp_obj_get_type(self_in)->make_new == mp_obj_exception_make_new) { + mp_obj_exception_t *self = self_in; - // just set the traceback to the null object - // we don't want to call any memory management functions here - self->traceback = MP_OBJ_NULL; + // just set the traceback to the null object + // we don't want to call any memory management functions here + self->traceback = MP_OBJ_NULL; + } } void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, machine_uint_t line, qstr block) { // make sure self_in is an exception instance - assert(mp_obj_get_type(self_in)->make_new == mp_obj_exception_make_new); - mp_obj_exception_t *self = self_in; + // TODO add traceback information to user-defined exceptions (need proper builtin subclassing for that) + if (mp_obj_get_type(self_in)->make_new == mp_obj_exception_make_new) { + mp_obj_exception_t *self = self_in; - // for traceback, we are just using the list object for convenience, it's not really a list of Python objects - if (self->traceback == MP_OBJ_NULL) { - self->traceback = mp_obj_new_list(0, NULL); + // for traceback, we are just using the list object for convenience, it's not really a list of Python objects + if (self->traceback == MP_OBJ_NULL) { + self->traceback = mp_obj_new_list(0, NULL); + } + mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)file); + mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)line); + mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)block); } - mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)file); - mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)line); - mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)block); } void mp_obj_exception_get_traceback(mp_obj_t self_in, machine_uint_t *n, machine_uint_t **values) { diff --git a/py/objgenerator.c b/py/objgenerator.c index 39e362e041..eed5a55111 100644 --- a/py/objgenerator.c +++ b/py/objgenerator.c @@ -8,6 +8,7 @@ #include "obj.h" #include "runtime.h" #include "bc.h" +#include "objgenerator.h" /******************************************************************************/ /* generator wrapper */ @@ -73,9 +74,10 @@ mp_obj_t gen_instance_getiter(mp_obj_t self_in) { return self_in; } -STATIC mp_obj_t gen_resume(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw_value) { +mp_obj_t mp_obj_gen_resume(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw_value, mp_vm_return_kind_t *ret_kind) { mp_obj_gen_instance_t *self = self_in; if (self->ip == 0) { + *ret_kind = MP_VM_RETURN_NORMAL; return mp_const_stop_iteration; } if (self->sp == self->state - 1) { @@ -85,10 +87,11 @@ STATIC mp_obj_t gen_resume(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw } else { *self->sp = send_value; } - mp_vm_return_kind_t vm_return_kind = mp_execute_byte_code_2(self->code_info, &self->ip, + *ret_kind = mp_execute_byte_code_2(self->code_info, &self->ip, &self->state[self->n_state - 1], &self->sp, (mp_exc_stack*)(self->state + self->n_state), &self->exc_sp, throw_value); - switch (vm_return_kind) { + + switch (*ret_kind) { case MP_VM_RETURN_NORMAL: // Explicitly mark generator as completed. If we don't do this, // subsequent next() may re-execute statements after last yield @@ -96,19 +99,39 @@ STATIC mp_obj_t gen_resume(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw // TODO: check how return with value behaves under such conditions // in CPython. self->ip = 0; - if (*self->sp == mp_const_none) { + return *self->sp; + + case MP_VM_RETURN_YIELD: + return *self->sp; + + case MP_VM_RETURN_EXCEPTION: + self->ip = 0; + return self->state[self->n_state - 1]; + + default: + assert(0); + return mp_const_none; + } +} + +STATIC mp_obj_t gen_resume_and_raise(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw_value) { + mp_vm_return_kind_t ret_kind; + mp_obj_t ret = mp_obj_gen_resume(self_in, send_value, throw_value, &ret_kind); + + switch (ret_kind) { + case MP_VM_RETURN_NORMAL: + // Optimize return w/o value in case generator is used in for loop + if (ret == mp_const_none) { return mp_const_stop_iteration; } else { - // TODO return StopIteration with value *self->sp - return mp_const_stop_iteration; + nlr_jump(mp_obj_new_exception_args(&mp_type_StopIteration, 1, &ret)); } case MP_VM_RETURN_YIELD: - return *self->sp; + return ret; case MP_VM_RETURN_EXCEPTION: - self->ip = 0; - nlr_jump(self->state[self->n_state - 1]); + nlr_jump(ret); default: assert(0); @@ -117,11 +140,11 @@ STATIC mp_obj_t gen_resume(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw } mp_obj_t gen_instance_iternext(mp_obj_t self_in) { - return gen_resume(self_in, mp_const_none, MP_OBJ_NULL); + return gen_resume_and_raise(self_in, mp_const_none, MP_OBJ_NULL); } STATIC mp_obj_t gen_instance_send(mp_obj_t self_in, mp_obj_t send_value) { - mp_obj_t ret = gen_resume(self_in, send_value, MP_OBJ_NULL); + mp_obj_t ret = gen_resume_and_raise(self_in, send_value, MP_OBJ_NULL); if (ret == mp_const_stop_iteration) { nlr_jump(mp_obj_new_exception(&mp_type_StopIteration)); } else { @@ -132,7 +155,7 @@ STATIC mp_obj_t gen_instance_send(mp_obj_t self_in, mp_obj_t send_value) { STATIC MP_DEFINE_CONST_FUN_OBJ_2(gen_instance_send_obj, gen_instance_send); STATIC mp_obj_t gen_instance_throw(uint n_args, const mp_obj_t *args) { - mp_obj_t ret = gen_resume(args[0], mp_const_none, n_args == 2 ? args[1] : args[2]); + mp_obj_t ret = gen_resume_and_raise(args[0], mp_const_none, n_args == 2 ? args[1] : args[2]); if (ret == mp_const_stop_iteration) { nlr_jump(mp_obj_new_exception(&mp_type_StopIteration)); } else { @@ -142,8 +165,31 @@ STATIC mp_obj_t gen_instance_throw(uint n_args, const mp_obj_t *args) { STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(gen_instance_throw_obj, 2, 4, gen_instance_throw); +STATIC mp_obj_t gen_instance_close(mp_obj_t self_in) { + mp_vm_return_kind_t ret_kind; + mp_obj_t ret = mp_obj_gen_resume(self_in, mp_const_none, (mp_obj_t)&mp_type_GeneratorExit, &ret_kind); + + if (ret_kind == MP_VM_RETURN_YIELD) { + nlr_jump(mp_obj_new_exception_msg(&mp_type_RuntimeError, "generator ignored GeneratorExit")); + } + // Swallow StopIteration & GeneratorExit (== successful close), and re-raise any other + if (ret_kind == MP_VM_RETURN_EXCEPTION) { + // ret should always be an instance of an exception class + if (mp_obj_is_subclass_fast(mp_obj_get_type(ret), &mp_type_GeneratorExit) || + mp_obj_is_subclass_fast(mp_obj_get_type(ret), &mp_type_StopIteration)) { + return mp_const_none; + } + nlr_jump(ret); + } + + // The only choice left is MP_VM_RETURN_NORMAL which is successful close + return mp_const_none; +} + +STATIC MP_DEFINE_CONST_FUN_OBJ_1(gen_instance_close_obj, gen_instance_close); STATIC const mp_method_t gen_type_methods[] = { + { "close", &gen_instance_close_obj }, { "send", &gen_instance_send_obj }, { "throw", &gen_instance_throw_obj }, { NULL, NULL }, // end-of-list sentinel diff --git a/py/objgenerator.h b/py/objgenerator.h new file mode 100644 index 0000000000..3dc69aa0ef --- /dev/null +++ b/py/objgenerator.h @@ -0,0 +1 @@ +mp_obj_t mp_obj_gen_resume(mp_obj_t self_in, mp_obj_t send_val, mp_obj_t throw_val, mp_vm_return_kind_t *ret_kind); diff --git a/py/objtype.c b/py/objtype.c index 4898c5ccb4..9cb29744c7 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -469,7 +469,7 @@ mp_obj_t mp_obj_new_super(mp_obj_t type, mp_obj_t obj) { // object and classinfo should be type objects // (but the function will fail gracefully if they are not) -bool mp_obj_is_subclass_fast(mp_obj_t object, mp_obj_t classinfo) { +bool mp_obj_is_subclass_fast(mp_const_obj_t object, mp_const_obj_t classinfo) { for (;;) { if (object == classinfo) { return true; @@ -482,7 +482,7 @@ bool mp_obj_is_subclass_fast(mp_obj_t object, mp_obj_t classinfo) { return false; } - mp_obj_type_t *self = object; + const mp_obj_type_t *self = object; // for a const struct, this entry might be NULL if (self->bases_tuple == MP_OBJ_NULL) { diff --git a/py/qstrdefs.h b/py/qstrdefs.h index 44c61002a6..1c8afe797d 100644 --- a/py/qstrdefs.h +++ b/py/qstrdefs.h @@ -17,6 +17,7 @@ Q(__repl_print__) Q(__bool__) Q(__len__) +Q(__iter__) Q(__getitem__) Q(__setitem__) Q(__add__) diff --git a/py/runtime.c b/py/runtime.c index 0f5d1229f2..3e0ef98d8b 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -925,15 +925,21 @@ mp_obj_t rt_getiter(mp_obj_t o_in) { if (type->getiter != NULL) { return type->getiter(o_in); } else { - // check for __getitem__ method + // check for __iter__ method mp_obj_t dest[2]; - rt_load_method_maybe(o_in, MP_QSTR___getitem__, dest); + rt_load_method_maybe(o_in, MP_QSTR___iter__, dest); if (dest[0] != MP_OBJ_NULL) { - // __getitem__ exists, create an iterator - return mp_obj_new_getitem_iter(dest); + // __iter__ exists, call it and return its result + return rt_call_method_n_kw(0, 0, dest); } else { - // object not iterable - nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object is not iterable", mp_obj_get_type_str(o_in))); + rt_load_method_maybe(o_in, MP_QSTR___getitem__, dest); + if (dest[0] != MP_OBJ_NULL) { + // __getitem__ exists, create an iterator + return mp_obj_new_getitem_iter(dest); + } else { + // object not iterable + nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object is not iterable", mp_obj_get_type_str(o_in))); + } } } } @@ -943,7 +949,15 @@ mp_obj_t rt_iternext(mp_obj_t o_in) { if (type->iternext != NULL) { return type->iternext(o_in); } else { - nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object is not an iterator", mp_obj_get_type_str(o_in))); + // check for __next__ method + mp_obj_t dest[2]; + rt_load_method_maybe(o_in, MP_QSTR___next__, dest); + if (dest[0] != MP_OBJ_NULL) { + // __next__ exists, call it and return its result + return rt_call_method_n_kw(0, 0, dest); + } else { + nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object is not an iterator", mp_obj_get_type_str(o_in))); + } } } @@ -124,6 +124,7 @@ mp_vm_return_kind_t mp_execute_byte_code_2(const byte *code_info, const byte **i // outer exception handling loop for (;;) { +outer_dispatch_loop: if (nlr_push(&nlr) == 0) { // If we have exception to inject, now that we finish setting up // execution context, raise it. This works as if RAISE_VARARGS @@ -642,6 +643,15 @@ unwind_return: } else { // exception occurred + // check if it's a StopIteration within a for block + if (*save_ip == MP_BC_FOR_ITER && mp_obj_is_subclass_fast(mp_obj_get_type(nlr.ret_val), &mp_type_StopIteration)) { + ip = save_ip + 1; + DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward + --sp; // pop the exhausted iterator + ip += unum; // jump to after for-block + goto outer_dispatch_loop; // continue with dispatch loop + } + // set file and line number that the exception occurred at // TODO: don't set traceback for exceptions re-raised by END_FINALLY. // But consider how to handle nested exceptions. |