summaryrefslogtreecommitdiffstats
path: root/Modules/_randommodule.c
diff options
context:
space:
mode:
authorAntoine Pitrou <antoine@python.org>2020-04-17 17:32:14 (GMT)
committerGitHub <noreply@github.com>2020-04-17 17:32:14 (GMT)
commit75a3378810bab03949ad9f653f78d933bdf3879c (patch)
tree96ec61f0d287ca5632da7ee091079817d4197733 /Modules/_randommodule.c
parentd7c657d4b121164caa439253da5266b2e29a1bed (diff)
downloadcpython-75a3378810bab03949ad9f653f78d933bdf3879c.zip
cpython-75a3378810bab03949ad9f653f78d933bdf3879c.tar.gz
cpython-75a3378810bab03949ad9f653f78d933bdf3879c.tar.bz2
bpo-40282: Allow random.getrandbits(0) (GH-19539)
Diffstat (limited to 'Modules/_randommodule.c')
-rw-r--r--Modules/_randommodule.c7
1 files changed, 5 insertions, 2 deletions
diff --git a/Modules/_randommodule.c b/Modules/_randommodule.c
index 51c0842..64e44e3 100644
--- a/Modules/_randommodule.c
+++ b/Modules/_randommodule.c
@@ -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));