bpo-37021: Port _randommodule to the argument clinic (GH-13532)
This commit is contained in:
parent
1c9debd236
commit
561612d845
@ -89,6 +89,13 @@ static PyTypeObject Random_Type;
|
|||||||
|
|
||||||
#define RandomObject_Check(v) (Py_TYPE(v) == &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 */
|
/* Random methods */
|
||||||
|
|
||||||
@ -137,8 +144,18 @@ genrand_int32(RandomObject *self)
|
|||||||
* lower 26 bits of the 53-bit numerator.
|
* lower 26 bits of the 53-bit numerator.
|
||||||
* The original code credited Isaku Wada for this algorithm, 2002/01/09.
|
* 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 *
|
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;
|
uint32_t a=genrand_int32(self)>>5, b=genrand_int32(self)>>6;
|
||||||
return PyFloat_FromDouble((a*67108864.0+b)*(1.0/9007199254740992.0));
|
return PyFloat_FromDouble((a*67108864.0+b)*(1.0/9007199254740992.0));
|
||||||
@ -232,17 +249,13 @@ random_seed_time_pid(RandomObject *self)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
random_seed(RandomObject *self, PyObject *args)
|
random_seed(RandomObject *self, PyObject *arg)
|
||||||
{
|
{
|
||||||
PyObject *result = NULL; /* guilty until proved innocent */
|
PyObject *result = NULL; /* guilty until proved innocent */
|
||||||
PyObject *n = NULL;
|
PyObject *n = NULL;
|
||||||
uint32_t *key = NULL;
|
uint32_t *key = NULL;
|
||||||
size_t bits, keyused;
|
size_t bits, keyused;
|
||||||
int res;
|
int res;
|
||||||
PyObject *arg = NULL;
|
|
||||||
|
|
||||||
if (!PyArg_UnpackTuple(args, "seed", 0, 1, &arg))
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
if (arg == NULL || arg == Py_None) {
|
if (arg == NULL || arg == Py_None) {
|
||||||
if (random_seed_urandom(self) < 0) {
|
if (random_seed_urandom(self) < 0) {
|
||||||
@ -317,8 +330,37 @@ Done:
|
|||||||
return result;
|
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 *
|
static PyObject *
|
||||||
random_getstate(RandomObject *self, PyObject *Py_UNUSED(ignored))
|
_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_Random_getstate_impl(RandomObject *self)
|
||||||
|
/*[clinic end generated code: output=bf6cef0c092c7180 input=b937a487928c0e89]*/
|
||||||
{
|
{
|
||||||
PyObject *state;
|
PyObject *state;
|
||||||
PyObject *element;
|
PyObject *element;
|
||||||
@ -344,8 +386,20 @@ Fail:
|
|||||||
return NULL;
|
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 *
|
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;
|
int i;
|
||||||
unsigned long element;
|
unsigned long element;
|
||||||
@ -384,17 +438,26 @@ random_setstate(RandomObject *self, PyObject *state)
|
|||||||
Py_RETURN_NONE;
|
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 *
|
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 r;
|
||||||
uint32_t *wordarray;
|
uint32_t *wordarray;
|
||||||
PyObject *result;
|
PyObject *result;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "i:getrandbits", &k))
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
if (k <= 0) {
|
if (k <= 0) {
|
||||||
PyErr_SetString(PyExc_ValueError,
|
PyErr_SetString(PyExc_ValueError,
|
||||||
"number of bits must be greater than zero");
|
"number of bits must be greater than zero");
|
||||||
@ -453,17 +516,11 @@ random_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static PyMethodDef random_methods[] = {
|
static PyMethodDef random_methods[] = {
|
||||||
{"random", (PyCFunction)random_random, METH_NOARGS,
|
_RANDOM_RANDOM_RANDOM_METHODDEF
|
||||||
PyDoc_STR("random() -> x in the interval [0, 1).")},
|
_RANDOM_RANDOM_SEED_METHODDEF
|
||||||
{"seed", (PyCFunction)random_seed, METH_VARARGS,
|
_RANDOM_RANDOM_GETSTATE_METHODDEF
|
||||||
PyDoc_STR("seed([n]) -> None. Defaults to current time.")},
|
_RANDOM_RANDOM_SETSTATE_METHODDEF
|
||||||
{"getstate", (PyCFunction)random_getstate, METH_NOARGS,
|
_RANDOM_RANDOM_GETRANDBITS_METHODDEF
|
||||||
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.")},
|
|
||||||
{NULL, NULL} /* sentinel */
|
{NULL, NULL} /* sentinel */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
117
Modules/clinic/_randommodule.c.h
generated
Normal file
117
Modules/clinic/_randommodule.c.h
generated
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
/*[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]*/
|
Loading…
x
Reference in New Issue
Block a user