Skip to content

Commit 37027ab

Browse files
[3.13] gh-135326: Restore support of __index__ in random.getrandbits() (#135332)
1 parent dd7ffdb commit 37027ab

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

Lib/test/test_random.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@
1414
from fractions import Fraction
1515
from collections import abc, Counter
1616

17+
18+
class MyIndex:
19+
def __init__(self, value):
20+
self.value = value
21+
22+
def __index__(self):
23+
return self.value
24+
25+
1726
class TestBasicOps:
1827
# Superclass with tests common to all generators.
1928
# Subclasses must arrange for self.gen to retrieve the Random instance
@@ -393,7 +402,7 @@ def test_getrandbits(self):
393402
self.assertRaises(TypeError, self.gen.getrandbits, 1, 2)
394403
self.assertRaises(ValueError, self.gen.getrandbits, -1)
395404
self.assertRaises(OverflowError, self.gen.getrandbits, 1<<1000)
396-
self.assertRaises(ValueError, self.gen.getrandbits, -1<<1000)
405+
self.assertRaises((ValueError, OverflowError), self.gen.getrandbits, -1<<1000)
397406
self.assertRaises(TypeError, self.gen.getrandbits, 10.1)
398407

399408
def test_pickling(self):
@@ -809,6 +818,9 @@ def test_getrandbits(self):
809818
self.gen.seed(1234567)
810819
self.assertEqual(self.gen.getrandbits(100),
811820
97904845777343510404718956115)
821+
self.gen.seed(1234567)
822+
self.assertEqual(self.gen.getrandbits(MyIndex(100)),
823+
97904845777343510404718956115)
812824

813825
def test_getrandbits_2G_bits(self):
814826
size = 2**31
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Restore support of integer-like objects with :meth:`!__index__` in
2+
:func:`random.getrandbits`.

Modules/_randommodule.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -495,21 +495,27 @@ _random_Random_setstate_impl(RandomObject *self, PyObject *state)
495495
_random.Random.getrandbits
496496
497497
self: self(type="RandomObject *")
498-
k: unsigned_long_long(bitwise=False)
498+
k: long_long
499499
/
500500
501501
getrandbits(k) -> x. Generates an int with k random bits.
502502
[clinic start generated code]*/
503503

504504
static PyObject *
505-
_random_Random_getrandbits_impl(RandomObject *self, unsigned long long k)
506-
/*[clinic end generated code: output=25a604fab95885d4 input=88e51091eea2f042]*/
505+
_random_Random_getrandbits_impl(RandomObject *self, long long k)
506+
/*[clinic end generated code: output=c2c02a7b0bfdf7f7 input=834d0fe668b981e4]*/
507507
{
508508
Py_ssize_t i, words;
509509
uint32_t r;
510510
uint32_t *wordarray;
511511
PyObject *result;
512512

513+
if (k < 0) {
514+
PyErr_SetString(PyExc_ValueError,
515+
"number of bits must be non-negative");
516+
return NULL;
517+
}
518+
513519
if (k == 0)
514520
return PyLong_FromLong(0);
515521

Modules/clinic/_randommodule.c.h

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)