1
1
/*
2
- This example extends on features0 but demonstrates how to define a class.
2
+ This example extends on features0 but demonstrates how to define a class,
3
+ and a custom exception.
3
4
4
5
The Factorial class constructor takes an integer, and then the calculate
5
6
method can be called to get the factorial.
8
9
>>> f = features4.Factorial(4)
9
10
>>> f.calculate()
10
11
24
12
+
13
+ If the argument to the Factorial class constructor is less than zero, a
14
+ FactorialError is raised.
11
15
*/
12
16
13
17
// Include the header file to get access to the MicroPython API
@@ -22,6 +26,8 @@ typedef struct {
22
26
mp_int_t n ;
23
27
} mp_obj_factorial_t ;
24
28
29
+ mp_obj_full_type_t mp_type_FactorialError ;
30
+
25
31
// Essentially Factorial.__new__ (but also kind of __init__).
26
32
// Takes a single argument (the number to find the factorial of)
27
33
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
30
36
mp_obj_factorial_t * o = mp_obj_malloc (mp_obj_factorial_t , type );
31
37
o -> n = mp_obj_get_int (args_in [0 ]);
32
38
39
+ if (o -> n < 0 ) {
40
+ mp_raise_msg ((mp_obj_type_t * )& mp_type_FactorialError , "argument must be zero or above" );
41
+ }
42
+
33
43
return MP_OBJ_FROM_PTR (o );
34
44
}
35
45
@@ -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
68
78
// Make the Factorial type available on the module.
69
79
mp_store_global (MP_QSTR_Factorial , MP_OBJ_FROM_PTR (& mp_type_factorial ));
70
80
81
+ // Initialise the exception type.
82
+ mp_obj_exception_init (& mp_type_FactorialError , MP_QSTR_FactorialError , & mp_type_Exception );
83
+
84
+ // Make the FactorialError type available on the module.
85
+ mp_store_global (MP_QSTR_FactorialError , MP_OBJ_FROM_PTR (& mp_type_FactorialError ));
86
+
71
87
// This must be last, it restores the globals dict
72
88
MP_DYNRUNTIME_INIT_EXIT
73
89
}
0 commit comments