Skip to content

[3.13] gh-135326: Restore support of __index__ in random.getrandbits() #135332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion Lib/test/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@
from fractions import Fraction
from collections import abc, Counter


class MyIndex:
def __init__(self, value):
self.value = value

def __index__(self):
return self.value


class TestBasicOps:
# Superclass with tests common to all generators.
# Subclasses must arrange for self.gen to retrieve the Random instance
Expand Down Expand Up @@ -393,7 +402,7 @@ def test_getrandbits(self):
self.assertRaises(TypeError, self.gen.getrandbits, 1, 2)
self.assertRaises(ValueError, self.gen.getrandbits, -1)
self.assertRaises(OverflowError, self.gen.getrandbits, 1<<1000)
self.assertRaises(ValueError, self.gen.getrandbits, -1<<1000)
self.assertRaises((ValueError, OverflowError), self.gen.getrandbits, -1<<1000)
self.assertRaises(TypeError, self.gen.getrandbits, 10.1)

def test_pickling(self):
Expand Down Expand Up @@ -809,6 +818,9 @@ def test_getrandbits(self):
self.gen.seed(1234567)
self.assertEqual(self.gen.getrandbits(100),
97904845777343510404718956115)
self.gen.seed(1234567)
self.assertEqual(self.gen.getrandbits(MyIndex(100)),
97904845777343510404718956115)

def test_getrandbits_2G_bits(self):
size = 2**31
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Restore support of integer-like objects with :meth:`!__index__` in
:func:`random.getrandbits`.
12 changes: 9 additions & 3 deletions Modules/_randommodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -495,21 +495,27 @@ _random_Random_setstate_impl(RandomObject *self, PyObject *state)
_random.Random.getrandbits
self: self(type="RandomObject *")
k: unsigned_long_long(bitwise=False)
k: long_long
/
getrandbits(k) -> x. Generates an int with k random bits.
[clinic start generated code]*/

static PyObject *
_random_Random_getrandbits_impl(RandomObject *self, unsigned long long k)
/*[clinic end generated code: output=25a604fab95885d4 input=88e51091eea2f042]*/
_random_Random_getrandbits_impl(RandomObject *self, long long k)
/*[clinic end generated code: output=c2c02a7b0bfdf7f7 input=834d0fe668b981e4]*/
{
Py_ssize_t i, words;
uint32_t r;
uint32_t *wordarray;
PyObject *result;

if (k < 0) {
PyErr_SetString(PyExc_ValueError,
"number of bits must be non-negative");
return NULL;
}

if (k == 0)
return PyLong_FromLong(0);

Expand Down
10 changes: 5 additions & 5 deletions Modules/clinic/_randommodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading