bpo-40282: Allow random.getrandbits(0) (GH-19539)

This commit is contained in:
Antoine Pitrou 2020-04-17 19:32:14 +02:00 committed by GitHub
parent d7c657d4b1
commit 75a3378810
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 44 deletions

View file

@ -474,12 +474,15 @@ _random_Random_getrandbits_impl(RandomObject *self, int k)
uint32_t *wordarray;
PyObject *result;
if (k <= 0) {
if (k < 0) {
PyErr_SetString(PyExc_ValueError,
"number of bits must be greater than zero");
"number of bits must be non-negative");
return NULL;
}
if (k == 0)
return PyLong_FromLong(0);
if (k <= 32) /* Fast path */
return PyLong_FromUnsignedLong(genrand_uint32(self) >> (32 - k));