]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-135326: Test support of __index__ in random.getrandbits() (#135356)
authorSerhiy Storchaka <storchaka@gmail.com>
Wed, 11 Jun 2025 01:51:08 +0000 (04:51 +0300)
committerGitHub <noreply@github.com>
Wed, 11 Jun 2025 01:51:08 +0000 (18:51 -0700)
Lib/test/test_random.py

index 54910cd8054a1fb37bae0b1847a654b46da7e5c5..31ebcb3b8b0b004e44000909dcac9dc83a0b8324 100644 (file)
@@ -14,6 +14,15 @@ from test import support
 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
@@ -809,6 +818,9 @@ class MersenneTwister_TestBasicOps(TestBasicOps, unittest.TestCase):
         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