Commit 561612d8 authored by Pablo Galindo's avatar Pablo Galindo Committed by GitHub

bpo-37021: Port _randommodule to the argument clinic (GH-13532)

parent 1c9debd2
......@@ -89,6 +89,13 @@ static PyTypeObject Random_Type;
#define RandomObject_Check(v) (Py_TYPE(v) == &Random_Type)
#include "clinic/_randommodule.c.h"
/*[clinic input]
module _random
class _random.Random "RandomObject *" "&Random_Type"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f79898ae7847c321]*/
/* Random methods */
......@@ -137,8 +144,18 @@ genrand_int32(RandomObject *self)
* lower 26 bits of the 53-bit numerator.
* The original code credited Isaku Wada for this algorithm, 2002/01/09.
*/
/*[clinic input]
_random.Random.random
self: self(type="RandomObject *")
random() -> x in the interval [0, 1).
[clinic start generated code]*/
static PyObject *
random_random(RandomObject *self, PyObject *Py_UNUSED(ignored))
_random_Random_random_impl(RandomObject *self)
/*[clinic end generated code: output=117ff99ee53d755c input=afb2a59cbbb00349]*/
{
uint32_t a=genrand_int32(self)>>5, b=genrand_int32(self)>>6;
return PyFloat_FromDouble((a*67108864.0+b)*(1.0/9007199254740992.0));
......@@ -232,20 +249,16 @@ random_seed_time_pid(RandomObject *self)
}
static PyObject *
random_seed(RandomObject *self, PyObject *args)
random_seed(RandomObject *self, PyObject *arg)
{
PyObject *result = NULL; /* guilty until proved innocent */
PyObject *n = NULL;
uint32_t *key = NULL;
size_t bits, keyused;
int res;
PyObject *arg = NULL;
if (!PyArg_UnpackTuple(args, "seed", 0, 1, &arg))
return NULL;
if (arg == NULL || arg == Py_None) {
if (random_seed_urandom(self) < 0) {
if (arg == NULL || arg == Py_None) {
if (random_seed_urandom(self) < 0) {
PyErr_Clear();
/* Reading system entropy failed, fall back on the worst entropy:
......@@ -317,8 +330,37 @@ Done:
return result;
}
/*[clinic input]
_random.Random.seed
self: self(type="RandomObject *")
n: object = None
/
seed([n]) -> None.
Defaults to use urandom and falls back to a combination
of the current time and the process identifier.
[clinic start generated code]*/
static PyObject *
_random_Random_seed_impl(RandomObject *self, PyObject *n)
/*[clinic end generated code: output=0fad1e16ba883681 input=78d6ef0d52532a54]*/
{
return random_seed(self, n);
}
/*[clinic input]
_random.Random.getstate
self: self(type="RandomObject *")
getstate() -> tuple containing the current state.
[clinic start generated code]*/
static PyObject *
random_getstate(RandomObject *self, PyObject *Py_UNUSED(ignored))
_random_Random_getstate_impl(RandomObject *self)
/*[clinic end generated code: output=bf6cef0c092c7180 input=b937a487928c0e89]*/
{
PyObject *state;
PyObject *element;
......@@ -344,8 +386,20 @@ Fail:
return NULL;
}
/*[clinic input]
_random.Random.setstate
self: self(type="RandomObject *")
state: object
/
setstate(state) -> None. Restores generator state.
[clinic start generated code]*/
static PyObject *
random_setstate(RandomObject *self, PyObject *state)
_random_Random_setstate(RandomObject *self, PyObject *state)
/*[clinic end generated code: output=fd1c3cd0037b6681 input=b3b4efbb1bc66af8]*/
{
int i;
unsigned long element;
......@@ -384,17 +438,26 @@ random_setstate(RandomObject *self, PyObject *state)
Py_RETURN_NONE;
}
/*[clinic input]
_random.Random.getrandbits
self: self(type="RandomObject *")
k: int
/
getrandbits(k) -> x. Generates an int with k random bits.
[clinic start generated code]*/
static PyObject *
random_getrandbits(RandomObject *self, PyObject *args)
_random_Random_getrandbits_impl(RandomObject *self, int k)
/*[clinic end generated code: output=b402f82a2158887f input=8c0e6396dd176fc0]*/
{
int k, i, words;
int i, words;
uint32_t r;
uint32_t *wordarray;
PyObject *result;
if (!PyArg_ParseTuple(args, "i:getrandbits", &k))
return NULL;
if (k <= 0) {
PyErr_SetString(PyExc_ValueError,
"number of bits must be greater than zero");
......@@ -453,17 +516,11 @@ random_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
}
static PyMethodDef random_methods[] = {
{"random", (PyCFunction)random_random, METH_NOARGS,
PyDoc_STR("random() -> x in the interval [0, 1).")},
{"seed", (PyCFunction)random_seed, METH_VARARGS,
PyDoc_STR("seed([n]) -> None. Defaults to current time.")},
{"getstate", (PyCFunction)random_getstate, METH_NOARGS,
PyDoc_STR("getstate() -> tuple containing the current state.")},
{"setstate", (PyCFunction)random_setstate, METH_O,
PyDoc_STR("setstate(state) -> None. Restores generator state.")},
{"getrandbits", (PyCFunction)random_getrandbits, METH_VARARGS,
PyDoc_STR("getrandbits(k) -> x. Generates an int with "
"k random bits.")},
_RANDOM_RANDOM_RANDOM_METHODDEF
_RANDOM_RANDOM_SEED_METHODDEF
_RANDOM_RANDOM_GETSTATE_METHODDEF
_RANDOM_RANDOM_SETSTATE_METHODDEF
_RANDOM_RANDOM_GETRANDBITS_METHODDEF
{NULL, NULL} /* sentinel */
};
......
/*[clinic input]
preserve
[clinic start generated code]*/
PyDoc_STRVAR(_random_Random_random__doc__,
"random($self, /)\n"
"--\n"
"\n"
"random() -> x in the interval [0, 1).");
#define _RANDOM_RANDOM_RANDOM_METHODDEF \
{"random", (PyCFunction)_random_Random_random, METH_NOARGS, _random_Random_random__doc__},
static PyObject *
_random_Random_random_impl(RandomObject *self);
static PyObject *
_random_Random_random(RandomObject *self, PyObject *Py_UNUSED(ignored))
{
return _random_Random_random_impl(self);
}
PyDoc_STRVAR(_random_Random_seed__doc__,
"seed($self, n=None, /)\n"
"--\n"
"\n"
"seed([n]) -> None.\n"
"\n"
"Defaults to use urandom and falls back to a combination\n"
"of the current time and the process identifier.");
#define _RANDOM_RANDOM_SEED_METHODDEF \
{"seed", (PyCFunction)(void(*)(void))_random_Random_seed, METH_FASTCALL, _random_Random_seed__doc__},
static PyObject *
_random_Random_seed_impl(RandomObject *self, PyObject *n);
static PyObject *
_random_Random_seed(RandomObject *self, PyObject *const *args, Py_ssize_t nargs)
{
PyObject *return_value = NULL;
PyObject *n = Py_None;
if (!_PyArg_CheckPositional("seed", nargs, 0, 1)) {
goto exit;
}
if (nargs < 1) {
goto skip_optional;
}
n = args[0];
skip_optional:
return_value = _random_Random_seed_impl(self, n);
exit:
return return_value;
}
PyDoc_STRVAR(_random_Random_getstate__doc__,
"getstate($self, /)\n"
"--\n"
"\n"
"getstate() -> tuple containing the current state.");
#define _RANDOM_RANDOM_GETSTATE_METHODDEF \
{"getstate", (PyCFunction)_random_Random_getstate, METH_NOARGS, _random_Random_getstate__doc__},
static PyObject *
_random_Random_getstate_impl(RandomObject *self);
static PyObject *
_random_Random_getstate(RandomObject *self, PyObject *Py_UNUSED(ignored))
{
return _random_Random_getstate_impl(self);
}
PyDoc_STRVAR(_random_Random_setstate__doc__,
"setstate($self, state, /)\n"
"--\n"
"\n"
"setstate(state) -> None. Restores generator state.");
#define _RANDOM_RANDOM_SETSTATE_METHODDEF \
{"setstate", (PyCFunction)_random_Random_setstate, METH_O, _random_Random_setstate__doc__},
PyDoc_STRVAR(_random_Random_getrandbits__doc__,
"getrandbits($self, k, /)\n"
"--\n"
"\n"
"getrandbits(k) -> x. Generates an int with k random bits.");
#define _RANDOM_RANDOM_GETRANDBITS_METHODDEF \
{"getrandbits", (PyCFunction)_random_Random_getrandbits, METH_O, _random_Random_getrandbits__doc__},
static PyObject *
_random_Random_getrandbits_impl(RandomObject *self, int k);
static PyObject *
_random_Random_getrandbits(RandomObject *self, PyObject *arg)
{
PyObject *return_value = NULL;
int k;
if (PyFloat_Check(arg)) {
PyErr_SetString(PyExc_TypeError,
"integer argument expected, got float" );
goto exit;
}
k = _PyLong_AsInt(arg);
if (k == -1 && PyErr_Occurred()) {
goto exit;
}
return_value = _random_Random_getrandbits_impl(self, k);
exit:
return return_value;
}
/*[clinic end generated code: output=a7feb0c9c8d1b627 input=a9049054013a1b77]*/
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment