Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
micropython
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
card10
micropython
Commits
b86e3f92
Commit
b86e3f92
authored
11 years ago
by
Damien
Browse files
Options
Downloads
Patches
Plain Diff
py: implement some basic exception matching.
parent
8f9e2ee1
No related branches found
No related tags found
No related merge requests found
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
py/obj.h
+4
-0
4 additions, 0 deletions
py/obj.h
py/objexcept.c
+6
-0
6 additions, 0 deletions
py/objexcept.c
py/runtime.c
+26
-0
26 additions, 0 deletions
py/runtime.c
py/vm.c
+1
-1
1 addition, 1 deletion
py/vm.c
unix/main.c
+1
-5
1 addition, 5 deletions
unix/main.c
with
38 additions
and
6 deletions
py/obj.h
+
4
−
0
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
);
...
...
This diff is collapsed.
Click to expand it.
py/objexcept.c
+
6
−
0
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
;
}
This diff is collapsed.
Click to expand it.
py/runtime.c
+
26
−
0
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
);
...
...
This diff is collapsed.
Click to expand it.
py/vm.c
+
1
−
1
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
...
...
This diff is collapsed.
Click to expand it.
unix/main.c
+
1
−
5
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
"
);
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
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!
Save comment
Cancel
Please
register
or
sign in
to comment