Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
micropython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
xpstem
micropython
Commits
d532f960
Commit
d532f960
authored
May 17, 2024
by
Damien George
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
examples/natmod/features4: Create custom FactorialError as exc example.
Signed-off-by:
Damien George
<
damien@micropython.org
>
parent
482292cc
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
1 deletion
+17
-1
examples/natmod/features4/features4.c
examples/natmod/features4/features4.c
+17
-1
No files found.
examples/natmod/features4/features4.c
View file @
d532f960
/*
This example extends on features0 but demonstrates how to define a class.
This example extends on features0 but demonstrates how to define a class,
and a custom exception.
The Factorial class constructor takes an integer, and then the calculate
method can be called to get the factorial.
...
...
@@ -8,6 +9,9 @@
>>> f = features4.Factorial(4)
>>> f.calculate()
24
If the argument to the Factorial class constructor is less than zero, a
FactorialError is raised.
*/
// Include the header file to get access to the MicroPython API
...
...
@@ -22,6 +26,8 @@ typedef struct {
mp_int_t
n
;
}
mp_obj_factorial_t
;
mp_obj_full_type_t
mp_type_FactorialError
;
// Essentially Factorial.__new__ (but also kind of __init__).
// Takes a single argument (the number to find the factorial of)
static
mp_obj_t
factorial_make_new
(
const
mp_obj_type_t
*
type
,
size_t
n_args
,
size_t
n_kw
,
const
mp_obj_t
*
args_in
)
{
...
...
@@ -30,6 +36,10 @@ static mp_obj_t factorial_make_new(const mp_obj_type_t *type, size_t n_args, siz
mp_obj_factorial_t
*
o
=
mp_obj_malloc
(
mp_obj_factorial_t
,
type
);
o
->
n
=
mp_obj_get_int
(
args_in
[
0
]);
if
(
o
->
n
<
0
)
{
mp_raise_msg
((
mp_obj_type_t
*
)
&
mp_type_FactorialError
,
"argument must be zero or above"
);
}
return
MP_OBJ_FROM_PTR
(
o
);
}
...
...
@@ -68,6 +78,12 @@ mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *a
// Make the Factorial type available on the module.
mp_store_global
(
MP_QSTR_Factorial
,
MP_OBJ_FROM_PTR
(
&
mp_type_factorial
));
// Initialise the exception type.
mp_obj_exception_init
(
&
mp_type_FactorialError
,
MP_QSTR_FactorialError
,
&
mp_type_Exception
);
// Make the FactorialError type available on the module.
mp_store_global
(
MP_QSTR_FactorialError
,
MP_OBJ_FROM_PTR
(
&
mp_type_FactorialError
));
// This must be last, it restores the globals dict
MP_DYNRUNTIME_INIT_EXIT
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a 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