diff options
author | Macarthur Inbody <133794m3r@gmail.com> | 2020-12-23 12:05:26 -0500 |
---|---|---|
committer | Damien George <damien@micropython.org> | 2021-05-30 16:41:30 +1000 |
commit | 34d4dab683c69578b0ddd1725290529dd812b291 (patch) | |
tree | ce144236adc4757666878a779c57f2fb71e31d9d /extmod/modurandom.c | |
parent | 025e4b6fbc32cd3118a5519cf6a18aa04b045abe (diff) |
extmod/modurandom: Add error message when getrandbits has bad value.
The random module's getrandbits() method didn't give a proper error message
when calling it with a value that was outside of the range of 1-32, which
can lead to confusion using this function (which under CPython can accept
numbers larger than 32). Now instead of simply giving a ValueError it
gives an error message that states that the number of bits is constrained.
Also, since the random module's functions getrandbits() and randint()
differ from CPython, tests have been added to describe these differences.
For getrandbits the relevant documentation is shown and added to the docs.
The same is given for randint method so that the information is more easily
found.
Finally, since the int object lacks the bit_length() method there is a test
for that method also to include within the docs, showing the difference to
CPython.
Diffstat (limited to 'extmod/modurandom.c')
-rw-r--r-- | extmod/modurandom.c | 2 |
1 files changed, 1 insertions, 1 deletions
diff --git a/extmod/modurandom.c b/extmod/modurandom.c index 5a736c1eb..f44510be9 100644 --- a/extmod/modurandom.c +++ b/extmod/modurandom.c @@ -88,7 +88,7 @@ STATIC uint32_t yasmarang_randbelow(uint32_t n) { STATIC mp_obj_t mod_urandom_getrandbits(mp_obj_t num_in) { int n = mp_obj_get_int(num_in); if (n > 32 || n == 0) { - mp_raise_ValueError(NULL); + mp_raise_ValueError(MP_ERROR_TEXT("bits must be 32 or less")); } uint32_t mask = ~0; // Beware of C undefined behavior when shifting by >= than bit size |