Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
TASTE
uPython-mirror
Commits
b86e3f92
Commit
b86e3f92
authored
Dec 29, 2013
by
Damien
Browse files
py: implement some basic exception matching.
parent
8f9e2ee1
Changes
5
Hide whitespace changes
Inline
Side-by-side
py/obj.h
View file @
b86e3f92
...
...
@@ -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
);
...
...
py/objexcept.c
View file @
b86e3f92
...
...
@@ -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
;
}
py/runtime.c
View file @
b86e3f92
...
...
@@ -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
);
...
...
py/vm.c
View file @
b86e3f92
...
...
@@ -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
...
...
unix/main.c
View file @
b86e3f92
...
...
@@ -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
"
);
}
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment