8000 extmod/modurandom: Use mp_raise_ValueError(). · micropython/micropython@0982884 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0982884

Browse files
author
Paul Sokolovsky
committed
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.
1 parent 4b3da60 commit 0982884

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

extmod/modurandom.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ STATIC uint32_t yasmarang_randbelow(uint32_t n) {
7474
STATIC mp_obj_t mod_urandom_getrandbits(mp_obj_t num_in) {
7575
int n = mp_obj_get_int(num_in);
7676
if (n > 32 || n == 0) {
77-
nlr_raise(mp_obj_new_exception(&mp_type_ValueError));
77+
mp_raise_ValueError(NULL);
7878
}
7979
uint32_t mask = ~0;
8080
// 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) {
102102
if (start > 0) {
103103
return mp_obj_new_int(yasmarang_randbelow(start));
104104
} else {
105-
nlr_raise(mp_obj_new_exception(&mp_type_ValueError));
105+
goto error;
106106
}
107107
} else {
108108
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) {
111111
if (start < stop) {
112112
return mp_obj_new_int(start + yasmarang_randbelow(stop - start));
113113
} else {
114-
nlr_raise(mp_obj_new_exception(&mp_type_ValueError));
114+
goto error;
115115
}
116116
} else {
117117
// range(start, stop, step)
@@ -122,15 +122,18 @@ STATIC mp_obj_t mod_urandom_randrange(size_t n_args, const mp_obj_t *args) {
122122
} else if (step < 0) {
123123
n = (stop - start + step + 1) / step;
124124
} else {
125-
nlr_raise(mp_obj_new_exception(&mp_type_ValueError));
125+
goto error;
126126
}
127127
if (n > 0) {
128128
return mp_obj_new_int(start + step * yasmarang_randbelow(n));
129129
} else {
130-
nlr_raise(mp_obj_new_exception(&mp_type_ValueError));
130+
goto error;
131131
}
132132
}
133133
}
134+
135+
error:
136+
mp_raise_ValueError(NULL);
134137
}
135138
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_urandom_randrange_obj, 1, 3, mod_urandom_randrange);
136139

@@ -140,7 +143,7 @@ STATIC mp_obj_t mod_urandom_randint(mp_obj_t a_in, mp_obj_t b_in) {
140143
if (a <= b) {
141144
return mp_obj_new_int(a + yasmarang_randbelow(b - a + 1));
142145
} else {
143-
nlr_raise(mp_obj_new_exception(&mp_type_ValueError));
146+
mp_raise_ValueError(NULL);
144147
}
145148
}
146149
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_urandom_randint_obj, mod_urandom_randint);

0 commit comments

Comments
 (0)
0