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
4bcd04bc
Commit
4bcd04bc
authored
Sep 24, 2014
by
Damien George
Browse files
Options
Downloads
Patches
Plain Diff
py: Tidy up exception matching; allow matching of tuple of exceptions.
Addresses issue #864.
parent
16ef60fb
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
py/obj.h
+1
-1
1 addition, 1 deletion
py/obj.h
py/objexcept.c
+9
-5
9 additions, 5 deletions
py/objexcept.c
py/runtime.c
+14
-7
14 additions, 7 deletions
py/runtime.c
with
24 additions
and
13 deletions
py/obj.h
+
1
−
1
View file @
4bcd04bc
...
...
@@ -458,7 +458,7 @@ mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in);
#define mp_obj_is_native_exception_instance(o) (mp_obj_get_type(o)->make_new == mp_obj_exception_make_new)
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_t
ype_t
*
exc_type
);
bool
mp_obj_exception_match
(
mp_obj_t
exc
,
mp_
const_obj_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
,
mp_uint_t
line
,
qstr
block
);
void
mp_obj_exception_get_traceback
(
mp_obj_t
self_in
,
mp_uint_t
*
n
,
mp_uint_t
**
values
);
...
...
This diff is collapsed.
Click to expand it.
py/objexcept.c
+
9
−
5
View file @
4bcd04bc
...
...
@@ -403,11 +403,15 @@ bool mp_obj_is_exception_instance(mp_obj_t self_in) {
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
mp_binary_op
(
MP_BINARY_OP_EXCEPTION_MATCH
,
exc
,
(
mp_obj_t
)
exc_type
)
==
mp_const_true
;
// Return true if exception (type or instance) is a subclass of given
// exception type. Assumes exc_type is a subclass of BaseException, as
// defined by mp_obj_is_exception_type(exc_type).
bool
mp_obj_exception_match
(
mp_obj_t
exc
,
mp_const_obj_t
exc_type
)
{
// if exc is an instance of an exception, then extract and use its type
if
(
mp_obj_is_exception_instance
(
exc
))
{
exc
=
mp_obj_get_type
(
exc
);
}
return
mp_obj_is_subclass_fast
(
exc
,
exc_type
);
}
// traceback handling functions
...
...
This diff is collapsed.
Click to expand it.
py/runtime.c
+
14
−
7
View file @
4bcd04bc
...
...
@@ -263,19 +263,26 @@ mp_obj_t mp_binary_op(mp_uint_t op, mp_obj_t lhs, mp_obj_t rhs) {
if
(
op
==
MP_BINARY_OP_EXCEPTION_MATCH
)
{
// rhs must be issubclass(rhs, BaseException)
if
(
mp_obj_is_exception_type
(
rhs
))
{
// if lhs is an instance of an exception, then extract and use its type
if
(
mp_obj_is_exception_instance
(
lhs
))
{
lhs
=
mp_obj_get_type
(
lhs
);
}
if
(
mp_obj_is_subclass_fast
(
lhs
,
rhs
))
{
if
(
mp_obj_exception_match
(
lhs
,
rhs
))
{
return
mp_const_true
;
}
else
{
return
mp_const_false
;
}
}
else
if
(
MP_OBJ_IS_TYPE
(
rhs
,
&
mp_type_tuple
))
{
mp_obj_tuple_t
*
tuple
=
rhs
;
for
(
mp_uint_t
i
=
0
;
i
<
tuple
->
len
;
i
++
)
{
rhs
=
tuple
->
items
[
i
];
if
(
!
mp_obj_is_exception_type
(
rhs
))
{
goto
unsupported_op
;
}
if
(
mp_obj_exception_match
(
lhs
,
rhs
))
{
return
mp_const_true
;
}
}
assert
(
0
);
return
mp_const_false
;
}
goto
unsupported_op
;
}
if
(
MP_OBJ_IS_SMALL_INT
(
lhs
))
{
mp_int_t
lhs_val
=
MP_OBJ_SMALL_INT_VALUE
(
lhs
);
...
...
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