examples/natmod/features4: Create custom FactorialError as exc example.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George 2024-05-17 10:43:51 +10:00
parent 482292cc66
commit d532f960a4

View File

@ -1,5 +1,6 @@
/* /*
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 The Factorial class constructor takes an integer, and then the calculate
method can be called to get the factorial. method can be called to get the factorial.
@ -8,6 +9,9 @@
>>> f = features4.Factorial(4) >>> f = features4.Factorial(4)
>>> f.calculate() >>> f.calculate()
24 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 // Include the header file to get access to the MicroPython API
@ -22,6 +26,8 @@ typedef struct {
mp_int_t n; mp_int_t n;
} mp_obj_factorial_t; } mp_obj_factorial_t;
mp_obj_full_type_t mp_type_FactorialError;
// Essentially Factorial.__new__ (but also kind of __init__). // Essentially Factorial.__new__ (but also kind of __init__).
// Takes a single argument (the number to find the factorial of) // 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) { 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); mp_obj_factorial_t *o = mp_obj_malloc(mp_obj_factorial_t, type);
o->n = mp_obj_get_int(args_in[0]); 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); 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. // Make the Factorial type available on the module.
mp_store_global(MP_QSTR_Factorial, MP_OBJ_FROM_PTR(&mp_type_factorial)); 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 // This must be last, it restores the globals dict
MP_DYNRUNTIME_INIT_EXIT MP_DYNRUNTIME_INIT_EXIT
} }