Commit 09828846 authored by Paul Sokolovsky's avatar Paul Sokolovsky

extmod/modurandom: Use mp_raise_ValueError().

For the standard unix x86_64 build, this saves 11 bytes on object file
level, but no difference in executable size due to (bloaty) code alignment.
parent 4b3da603
...@@ -74,7 +74,7 @@ STATIC uint32_t yasmarang_randbelow(uint32_t n) { ...@@ -74,7 +74,7 @@ STATIC uint32_t yasmarang_randbelow(uint32_t n) {
STATIC mp_obj_t mod_urandom_getrandbits(mp_obj_t num_in) { STATIC mp_obj_t mod_urandom_getrandbits(mp_obj_t num_in) {
int n = mp_obj_get_int(num_in); int n = mp_obj_get_int(num_in);
if (n > 32 || n == 0) { if (n > 32 || n == 0) {
nlr_raise(mp_obj_new_exception(&mp_type_ValueError)); mp_raise_ValueError(NULL);
} }
uint32_t mask = ~0; uint32_t mask = ~0;
// Beware of C undefined behavior when shifting by >= than bit size // Beware of C undefined behavior when shifting by >= than bit size
...@@ -102,7 +102,7 @@ STATIC mp_obj_t mod_urandom_randrange(size_t n_args, const mp_obj_t *args) { ...@@ -102,7 +102,7 @@ STATIC mp_obj_t mod_urandom_randrange(size_t n_args, const mp_obj_t *args) {
if (start > 0) { if (start > 0) {
return mp_obj_new_int(yasmarang_randbelow(start)); return mp_obj_new_int(yasmarang_randbelow(start));
} else { } else {
nlr_raise(mp_obj_new_exception(&mp_type_ValueError)); goto error;
} }
} else { } else {
mp_int_t stop = mp_obj_get_int(args[1]); mp_int_t stop = mp_obj_get_int(args[1]);
...@@ -111,7 +111,7 @@ STATIC mp_obj_t mod_urandom_randrange(size_t n_args, const mp_obj_t *args) { ...@@ -111,7 +111,7 @@ STATIC mp_obj_t mod_urandom_randrange(size_t n_args, const mp_obj_t *args) {
if (start < stop) { if (start < stop) {
return mp_obj_new_int(start + yasmarang_randbelow(stop - start)); return mp_obj_new_int(start + yasmarang_randbelow(stop - start));
} else { } else {
nlr_raise(mp_obj_new_exception(&mp_type_ValueError)); goto error;
} }
} else { } else {
// range(start, stop, step) // range(start, stop, step)
...@@ -122,15 +122,18 @@ STATIC mp_obj_t mod_urandom_randrange(size_t n_args, const mp_obj_t *args) { ...@@ -122,15 +122,18 @@ STATIC mp_obj_t mod_urandom_randrange(size_t n_args, const mp_obj_t *args) {
} else if (step < 0) { } else if (step < 0) {
n = (stop - start + step + 1) / step; n = (stop - start + step + 1) / step;
} else { } else {
nlr_raise(mp_obj_new_exception(&mp_type_ValueError)); goto error;
} }
if (n > 0) { if (n > 0) {
return mp_obj_new_int(start + step * yasmarang_randbelow(n)); return mp_obj_new_int(start + step * yasmarang_randbelow(n));
} else { } else {
nlr_raise(mp_obj_new_exception(&mp_type_ValueError)); goto error;
} }
} }
} }
error:
mp_raise_ValueError(NULL);
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_urandom_randrange_obj, 1, 3, mod_urandom_randrange); STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_urandom_randrange_obj, 1, 3, mod_urandom_randrange);
...@@ -140,7 +143,7 @@ STATIC mp_obj_t mod_urandom_randint(mp_obj_t a_in, mp_obj_t b_in) { ...@@ -140,7 +143,7 @@ STATIC mp_obj_t mod_urandom_randint(mp_obj_t a_in, mp_obj_t b_in) {
if (a <= b) { if (a <= b) {
return mp_obj_new_int(a + yasmarang_randbelow(b - a + 1)); return mp_obj_new_int(a + yasmarang_randbelow(b - a + 1));
} else { } else {
nlr_raise(mp_obj_new_exception(&mp_type_ValueError)); mp_raise_ValueError(NULL);
} }
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_urandom_randint_obj, mod_urandom_randint); STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_urandom_randint_obj, mod_urandom_randint);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment