summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--py/obj.h4
-rw-r--r--py/objexcept.c6
-rw-r--r--py/runtime.c26
-rw-r--r--py/vm.c2
-rw-r--r--unix/main.c6
5 files changed, 38 insertions, 6 deletions
diff --git a/py/obj.h b/py/obj.h
index bf598608e8..8d857b1959 100644
--- a/py/obj.h
+++ b/py/obj.h
@@ -176,6 +176,10 @@ extern const mp_obj_type_t bool_type;
mp_obj_t mp_obj_cell_get(mp_obj_t self_in);
void mp_obj_cell_set(mp_obj_t self_in, mp_obj_t obj);
+// exception
+extern const mp_obj_type_t exception_type;
+qstr mp_obj_exception_get_type(mp_obj_t self_in);
+
// str
extern const mp_obj_type_t str_type;
qstr mp_obj_str_get(mp_obj_t self_in);
diff --git a/py/objexcept.c b/py/objexcept.c
index 4abc624376..e735852c37 100644
--- a/py/objexcept.c
+++ b/py/objexcept.c
@@ -84,3 +84,9 @@ mp_obj_t mp_obj_new_exception_msg_2_args(qstr id, const char *fmt, const char *a
o->args[2] = a2;
return o;
}
+
+qstr mp_obj_exception_get_type(mp_obj_t self_in) {
+ assert(MP_OBJ_IS_TYPE(self_in, &exception_type));
+ mp_obj_exception_t *self = self_in;
+ return self->id;
+}
diff --git a/py/runtime.c b/py/runtime.c
index 7d1357a3fc..7258b36caf 100644
--- a/py/runtime.c
+++ b/py/runtime.c
@@ -103,9 +103,23 @@ void rt_init(void) {
map_locals = map_globals = mp_map_new(MP_MAP_QSTR, 1);
mp_qstr_map_lookup(map_globals, qstr_from_str_static("__name__"), true)->value = mp_obj_new_str(qstr_from_str_static("__main__"));
+ // init built-in hash table
mp_map_init(&map_builtins, MP_MAP_QSTR, 3);
+
+ // built-in exceptions (TODO, make these proper classes)
+ mp_qstr_map_lookup(&map_builtins, rt_q_AttributeError, true)->value = mp_obj_new_exception(rt_q_AttributeError);
+ mp_qstr_map_lookup(&map_builtins, rt_q_IndexError, true)->value = mp_obj_new_exception(rt_q_IndexError);
+ mp_qstr_map_lookup(&map_builtins, rt_q_KeyError, true)->value = mp_obj_new_exception(rt_q_KeyError);
+ mp_qstr_map_lookup(&map_builtins, rt_q_NameError, true)->value = mp_obj_new_exception(rt_q_NameError);
+ mp_qstr_map_lookup(&map_builtins, rt_q_TypeError, true)->value = mp_obj_new_exception(rt_q_TypeError);
+ mp_qstr_map_lookup(&map_builtins, rt_q_SyntaxError, true)->value = mp_obj_new_exception(rt_q_SyntaxError);
+ mp_qstr_map_lookup(&map_builtins, rt_q_ValueError, true)->value = mp_obj_new_exception(rt_q_ValueError);
+
+ // built-in core functions
mp_qstr_map_lookup(&map_builtins, rt_q___build_class__, true)->value = rt_make_function_2(mp_builtin___build_class__);
mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("__repl_print__"), true)->value = rt_make_function_1(mp_builtin___repl_print__);
+
+ // built-in user functions
mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("abs"), true)->value = rt_make_function_1(mp_builtin_abs);
mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("all"), true)->value = rt_make_function_1(mp_builtin_all);
mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("any"), true)->value = rt_make_function_1(mp_builtin_any);
@@ -550,6 +564,18 @@ mp_obj_t rt_compare_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
}
}
+ // deal with exception_match
+ if (op == RT_COMPARE_OP_EXCEPTION_MATCH) {
+ // TODO properly! at the moment it just compares the exception identifier for equality
+ if (MP_OBJ_IS_TYPE(lhs, &exception_type) && MP_OBJ_IS_TYPE(rhs, &exception_type)) {
+ if (mp_obj_exception_get_type(lhs) == mp_obj_exception_get_type(rhs)) {
+ return mp_const_true;
+ } else {
+ return mp_const_false;
+ }
+ }
+ }
+
// deal with small ints
if (MP_OBJ_IS_SMALL_INT(lhs) && MP_OBJ_IS_SMALL_INT(rhs)) {
mp_small_int_t lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs);
diff --git a/py/vm.c b/py/vm.c
index 0a7c2fbf2c..2633e47987 100644
--- a/py/vm.c
+++ b/py/vm.c
@@ -491,7 +491,7 @@ bool mp_execute_byte_code_2(const byte **ip_in_out, mp_obj_t *fastn, mp_obj_t **
// push(traceback, exc-val, exc-type)
PUSH(mp_const_none);
PUSH(nlr.ret_val);
- PUSH(mp_const_none);
+ PUSH(nlr.ret_val); // TODO should be type(nlr.ret_val), I think...
} else {
// re-raise exception to higher level
diff --git a/unix/main.c b/unix/main.c
index 295e82d26a..16012e7181 100644
--- a/unix/main.c
+++ b/unix/main.c
@@ -115,14 +115,10 @@ void do_file(const char *file) {
if (module_fun != mp_const_none) {
nlr_buf_t nlr;
if (nlr_push(&nlr) == 0) {
- mp_obj_t ret = rt_call_function_0(module_fun);
- printf("done! got: ");
- mp_obj_print(ret);
- printf("\n");
+ rt_call_function_0(module_fun);
nlr_pop();
} else {
// uncaught exception
- printf("exception: ");
mp_obj_print((mp_obj_t)nlr.ret_val);
printf("\n");
}