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
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):
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
--- /dev/null
+Restore support of integer-like objects with :meth:`!__index__` in
+:func:`random.getrandbits`.
_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);
[clinic start generated code]*/
#include "pycore_critical_section.h"// Py_BEGIN_CRITICAL_SECTION()
-#include "pycore_long.h" // _PyLong_UnsignedLongLong_Converter()
#include "pycore_modsupport.h" // _PyArg_CheckPositional()
PyDoc_STRVAR(_random_Random_random__doc__,
{"getrandbits", (PyCFunction)_random_Random_getrandbits, METH_O, _random_Random_getrandbits__doc__},
static PyObject *
-_random_Random_getrandbits_impl(RandomObject *self, unsigned long long k);
+_random_Random_getrandbits_impl(RandomObject *self, long long k);
static PyObject *
_random_Random_getrandbits(RandomObject *self, PyObject *arg)
{
PyObject *return_value = NULL;
- unsigned long long k;
+ long long k;
- if (!_PyLong_UnsignedLongLong_Converter(arg, &k)) {
+ k = PyLong_AsLongLong(arg);
+ if (k == -1 && PyErr_Occurred()) {
goto exit;
}
Py_BEGIN_CRITICAL_SECTION(self);
exit:
return return_value;
}
-/*[clinic end generated code: output=e9a5c68295678cff input=a9049054013a1b77]*/
+/*[clinic end generated code: output=d3a199bc869e5c63 input=a9049054013a1b77]*/