nrf: Use mp_raise_ValueError instead of nlr_raise(...)

Saves 60 bytes on the nRF52 with SD disabled. There will be a bigger
saving with SD enabled and/or on the micro:bit board.
This commit is contained in:
Ayke van Laethem
2018-07-18 15:25:17 +02:00
parent 4117a3d672
commit 2f0f4fdcd3
9 changed files with 30 additions and 42 deletions

View File

@@ -91,7 +91,7 @@ static inline int randbelow(int n) {
STATIC mp_obj_t mod_random_getrandbits(mp_obj_t num_in) {
int n = mp_obj_get_int(num_in);
if (n > 30 || n == 0) {
nlr_raise(mp_obj_new_exception(&mp_type_ValueError));
mp_raise_ValueError(NULL);
}
uint32_t mask = ~0;
// Beware of C undefined behavior when shifting by >= than bit size
@@ -107,7 +107,7 @@ STATIC mp_obj_t mod_random_randrange(size_t n_args, const mp_obj_t *args) {
if (start > 0) {
return mp_obj_new_int(randbelow(start));
} else {
nlr_raise(mp_obj_new_exception(&mp_type_ValueError));
mp_raise_ValueError(NULL);
}
} else {
mp_int_t stop = mp_obj_get_int(args[1]);
@@ -116,7 +116,7 @@ STATIC mp_obj_t mod_random_randrange(size_t n_args, const mp_obj_t *args) {
if (start < stop) {
return mp_obj_new_int(start + randbelow(stop - start));
} else {
nlr_raise(mp_obj_new_exception(&mp_type_ValueError));
mp_raise_ValueError(NULL);
}
} else {
// range(start, stop, step)
@@ -127,12 +127,12 @@ STATIC mp_obj_t mod_random_randrange(size_t n_args, const mp_obj_t *args) {
} else if (step < 0) {
n = (stop - start + step + 1) / step;
} else {
nlr_raise(mp_obj_new_exception(&mp_type_ValueError));
mp_raise_ValueError(NULL);
}
if (n > 0) {
return mp_obj_new_int(start + step * randbelow(n));
} else {
nlr_raise(mp_obj_new_exception(&mp_type_ValueError));
mp_raise_ValueError(NULL);
}
}
}
@@ -145,7 +145,7 @@ STATIC mp_obj_t mod_random_randint(mp_obj_t a_in, mp_obj_t b_in) {
if (a <= b) {
return mp_obj_new_int(a + randbelow(b - a + 1));
} else {
nlr_raise(mp_obj_new_exception(&mp_type_ValueError));
mp_raise_ValueError(NULL);
}
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_random_randint_obj, mod_random_randint);

View File

@@ -63,8 +63,7 @@ STATIC mp_obj_t ubluepy_characteristic_make_new(const mp_obj_type_t *type, size_
s->p_uuid = MP_OBJ_TO_PTR(uuid_obj);
// (void)sd_characterstic_add(s);
} else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
"Invalid UUID parameter"));
mp_raise_ValueError("Invalid UUID parameter");
}
if (args[1].u_int > 0) {

View File

@@ -68,15 +68,13 @@ STATIC mp_obj_t ubluepy_service_make_new(const mp_obj_type_t *type, size_t n_arg
if (type > 0 && type <= UBLUEPY_SERVICE_PRIMARY) {
s->type = type;
} else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
"Invalid Service type"));
mp_raise_ValueError("Invalid Service type");
}
(void)ble_drv_service_add(s);
} else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
"Invalid UUID parameter"));
mp_raise_ValueError("Invalid UUID parameter");
}
// clear reference to peripheral
@@ -127,8 +125,7 @@ STATIC mp_obj_t service_get_characteristic(mp_obj_t self_in, mp_obj_t uuid) {
// validate that there is an UUID object passed in as parameter
if (!(MP_OBJ_IS_TYPE(uuid, &ubluepy_uuid_type))) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
"Invalid UUID parameter"));
mp_raise_ValueError("Invalid UUID parameter");
}
mp_obj_t * chars = NULL;

View File

@@ -122,8 +122,7 @@ STATIC mp_obj_t ubluepy_uuid_make_new(const mp_obj_type_t *type, size_t n_args,
ble_drv_uuid_add_vs(buffer, &s->uuid_vs_idx);
} else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
"Invalid UUID string length"));
mp_raise_ValueError("Invalid UUID string length");
}
} else if (MP_OBJ_IS_TYPE(uuid_obj, &ubluepy_uuid_type)) {
// deep copy instance
@@ -132,8 +131,7 @@ STATIC mp_obj_t ubluepy_uuid_make_new(const mp_obj_type_t *type, size_t n_args,
s->value[0] = p_old->value[0];
s->value[1] = p_old->value[1];
} else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
"Invalid UUID parameter"));
mp_raise_ValueError("Invalid UUID parameter");
}
return MP_OBJ_FROM_PTR(s);

View File

@@ -32,7 +32,6 @@
#include "microbitfs.h"
#include "drivers/flash.h"
#include "modrandom.h"
#include "py/nlr.h"
#include "py/obj.h"
#include "py/stream.h"
#include "py/runtime.h"
@@ -390,7 +389,7 @@ STATIC mp_obj_t microbit_remove(mp_obj_t filename) {
STATIC void check_file_open(file_descriptor_obj *self) {
if (!self->open) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "I/O operation on closed file"));
mp_raise_ValueError("I/O operation on closed file");
}
}
@@ -680,7 +679,7 @@ mp_obj_t uos_mbfs_open(size_t n_args, const mp_obj_t *args) {
}
return res;
mode_error:
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "illegal mode"));
mp_raise_ValueError("illegal mode");
}
STATIC mp_obj_t uos_mbfs_stat(mp_obj_t filename) {