diff options
author | shadchin <shadchin@yandex-team.ru> | 2022-02-10 16:44:30 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:44:30 +0300 |
commit | 2598ef1d0aee359b4b6d5fdd1758916d5907d04f (patch) | |
tree | 012bb94d777798f1f56ac1cec429509766d05181 /contrib/tools/python3/src/Modules/_randommodule.c | |
parent | 6751af0b0c1b952fede40b19b71da8025b5d8bcf (diff) | |
download | ydb-2598ef1d0aee359b4b6d5fdd1758916d5907d04f.tar.gz |
Restoring authorship annotation for <shadchin@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'contrib/tools/python3/src/Modules/_randommodule.c')
-rw-r--r-- | contrib/tools/python3/src/Modules/_randommodule.c | 398 |
1 files changed, 199 insertions, 199 deletions
diff --git a/contrib/tools/python3/src/Modules/_randommodule.c b/contrib/tools/python3/src/Modules/_randommodule.c index a402b937f7..d4b811c76b 100644 --- a/contrib/tools/python3/src/Modules/_randommodule.c +++ b/contrib/tools/python3/src/Modules/_randommodule.c @@ -11,7 +11,7 @@ * renamed genrand_res53() to random_random() and wrapped in python calling/return code. - * genrand_uint32() and the helper functions, init_genrand() + * genrand_uint32() and the helper functions, init_genrand() and init_by_array(), were declared static, wrapped in Python calling/return code. also, their global data references were replaced with structure references. @@ -67,9 +67,9 @@ /* ---------------------------------------------------------------*/ #include "Python.h" -#include "pycore_byteswap.h" // _Py_bswap32() +#include "pycore_byteswap.h" // _Py_bswap32() #ifdef HAVE_PROCESS_H -# include <process.h> // getpid() +# include <process.h> // getpid() #endif /* Period parameters -- These are all magic. Don't change. */ @@ -80,43 +80,43 @@ #define LOWER_MASK 0x7fffffffU /* least significant r bits */ typedef struct { - PyObject *Random_Type; - PyObject *Long___abs__; -} _randomstate; - -static inline _randomstate* -get_random_state(PyObject *module) -{ - void *state = PyModule_GetState(module); - assert(state != NULL); - return (_randomstate *)state; -} - -static struct PyModuleDef _randommodule; - -#define _randomstate_global get_random_state(PyState_FindModule(&_randommodule)) - -typedef struct { + PyObject *Random_Type; + PyObject *Long___abs__; +} _randomstate; + +static inline _randomstate* +get_random_state(PyObject *module) +{ + void *state = PyModule_GetState(module); + assert(state != NULL); + return (_randomstate *)state; +} + +static struct PyModuleDef _randommodule; + +#define _randomstate_global get_random_state(PyState_FindModule(&_randommodule)) + +typedef struct { PyObject_HEAD int index; uint32_t state[N]; } RandomObject; -#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]*/ +#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 */ /* generates a random number on [0,0xffffffff]-interval */ static uint32_t -genrand_uint32(RandomObject *self) +genrand_uint32(RandomObject *self) { uint32_t y; static const uint32_t mag01[2] = {0x0U, MATRIX_A}; @@ -158,20 +158,20 @@ genrand_uint32(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]*/ - + +/*[clinic input] +_random.Random.random + + self: self(type="RandomObject *") + +random() -> x in the interval [0, 1). +[clinic start generated code]*/ + static PyObject * -_random_Random_random_impl(RandomObject *self) -/*[clinic end generated code: output=117ff99ee53d755c input=afb2a59cbbb00349]*/ +_random_Random_random_impl(RandomObject *self) +/*[clinic end generated code: output=117ff99ee53d755c input=afb2a59cbbb00349]*/ { - uint32_t a=genrand_uint32(self)>>5, b=genrand_uint32(self)>>6; + uint32_t a=genrand_uint32(self)>>5, b=genrand_uint32(self)>>6; return PyFloat_FromDouble((a*67108864.0+b)*(1.0/9007199254740992.0)); } @@ -234,7 +234,7 @@ init_by_array(RandomObject *self, uint32_t init_key[], size_t key_length) static int random_seed_urandom(RandomObject *self) { - uint32_t key[N]; + uint32_t key[N]; if (_PyOS_URandomNonblock(key, sizeof(key)) < 0) { return -1; @@ -250,20 +250,20 @@ random_seed_time_pid(RandomObject *self) uint32_t key[5]; now = _PyTime_GetSystemClock(); - key[0] = (uint32_t)(now & 0xffffffffU); - key[1] = (uint32_t)(now >> 32); + key[0] = (uint32_t)(now & 0xffffffffU); + key[1] = (uint32_t)(now >> 32); - key[2] = (uint32_t)getpid(); + key[2] = (uint32_t)getpid(); now = _PyTime_GetMonotonicClock(); - key[3] = (uint32_t)(now & 0xffffffffU); - key[4] = (uint32_t)(now >> 32); + key[3] = (uint32_t)(now & 0xffffffffU); + key[4] = (uint32_t)(now >> 32); init_by_array(self, key, Py_ARRAY_LENGTH(key)); } static PyObject * -random_seed(RandomObject *self, PyObject *arg) +random_seed(RandomObject *self, PyObject *arg) { PyObject *result = NULL; /* guilty until proved innocent */ PyObject *n = NULL; @@ -271,8 +271,8 @@ random_seed(RandomObject *self, PyObject *arg) size_t bits, keyused; int res; - 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: @@ -286,12 +286,12 @@ random_seed(RandomObject *self, PyObject *arg) * So: if the arg is a PyLong, use its absolute value. * Otherwise use its hash value, cast to unsigned. */ - if (PyLong_CheckExact(arg)) { - n = PyNumber_Absolute(arg); - } else if (PyLong_Check(arg)) { + if (PyLong_CheckExact(arg)) { + n = PyNumber_Absolute(arg); + } else if (PyLong_Check(arg)) { /* Calling int.__abs__() prevents calling arg.__abs__(), which might return an invalid value. See issue #31478. */ - n = PyObject_CallOneArg(_randomstate_global->Long___abs__, arg); + n = PyObject_CallOneArg(_randomstate_global->Long___abs__, arg); } else { Py_hash_t hash = PyObject_Hash(arg); @@ -346,38 +346,38 @@ 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]*/ - +/*[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_Random_getstate_impl(RandomObject *self) -/*[clinic end generated code: output=bf6cef0c092c7180 input=b937a487928c0e89]*/ +_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 *element; int i; @@ -402,20 +402,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]*/ - + +/*[clinic input] +_random.Random.setstate + + self: self(type="RandomObject *") + state: object + / + +setstate(state) -> None. Restores generator state. +[clinic start generated code]*/ + static PyObject * -_random_Random_setstate(RandomObject *self, PyObject *state) -/*[clinic end generated code: output=fd1c3cd0037b6681 input=b3b4efbb1bc66af8]*/ +_random_Random_setstate(RandomObject *self, PyObject *state) +/*[clinic end generated code: output=fd1c3cd0037b6681 input=b3b4efbb1bc66af8]*/ { int i; unsigned long element; @@ -454,37 +454,37 @@ _random_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]*/ - +/*[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_Random_getrandbits_impl(RandomObject *self, int k) -/*[clinic end generated code: output=b402f82a2158887f input=8c0e6396dd176fc0]*/ +_random_Random_getrandbits_impl(RandomObject *self, int k) +/*[clinic end generated code: output=b402f82a2158887f input=8c0e6396dd176fc0]*/ { - int i, words; + int i, words; uint32_t r; uint32_t *wordarray; PyObject *result; - if (k < 0) { + if (k < 0) { PyErr_SetString(PyExc_ValueError, - "number of bits must be non-negative"); + "number of bits must be non-negative"); return NULL; } - if (k == 0) - return PyLong_FromLong(0); - + if (k == 0) + return PyLong_FromLong(0); + if (k <= 32) /* Fast path */ - return PyLong_FromUnsignedLong(genrand_uint32(self) >> (32 - k)); + return PyLong_FromUnsignedLong(genrand_uint32(self) >> (32 - k)); words = (k - 1) / 32 + 1; wordarray = (uint32_t *)PyMem_Malloc(words * 4); @@ -501,7 +501,7 @@ _random_Random_getrandbits_impl(RandomObject *self, int k) for (i = words - 1; i >= 0; i--, k -= 32) #endif { - r = genrand_uint32(self); + r = genrand_uint32(self); if (k < 32) r >>= (32 - k); /* Drop least significant bits */ wordarray[i] = r; @@ -519,12 +519,12 @@ random_new(PyTypeObject *type, PyObject *args, PyObject *kwds) RandomObject *self; PyObject *tmp; - if (type == (PyTypeObject*)_randomstate_global->Random_Type && - !_PyArg_NoKeywords("Random()", kwds)) { + if (type == (PyTypeObject*)_randomstate_global->Random_Type && + !_PyArg_NoKeywords("Random()", kwds)) { return NULL; - } + } - self = (RandomObject *)PyType_GenericAlloc(type, 0); + self = (RandomObject *)PyType_GenericAlloc(type, 0); if (self == NULL) return NULL; tmp = random_seed(self, args); @@ -536,69 +536,69 @@ random_new(PyTypeObject *type, PyObject *args, PyObject *kwds) return (PyObject *)self; } - + static PyMethodDef random_methods[] = { - _RANDOM_RANDOM_RANDOM_METHODDEF - _RANDOM_RANDOM_SEED_METHODDEF - _RANDOM_RANDOM_GETSTATE_METHODDEF - _RANDOM_RANDOM_SETSTATE_METHODDEF - _RANDOM_RANDOM_GETRANDBITS_METHODDEF + _RANDOM_RANDOM_RANDOM_METHODDEF + _RANDOM_RANDOM_SEED_METHODDEF + _RANDOM_RANDOM_GETSTATE_METHODDEF + _RANDOM_RANDOM_SETSTATE_METHODDEF + _RANDOM_RANDOM_GETRANDBITS_METHODDEF {NULL, NULL} /* sentinel */ }; PyDoc_STRVAR(random_doc, "Random() -> create a random number generator with its own internal state."); -static PyType_Slot Random_Type_slots[] = { - {Py_tp_doc, (void *)random_doc}, - {Py_tp_methods, random_methods}, - {Py_tp_new, random_new}, - {Py_tp_free, PyObject_Free}, - {0, 0}, -}; - -static PyType_Spec Random_Type_spec = { - "_random.Random", - sizeof(RandomObject), - 0, - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, - Random_Type_slots +static PyType_Slot Random_Type_slots[] = { + {Py_tp_doc, (void *)random_doc}, + {Py_tp_methods, random_methods}, + {Py_tp_new, random_new}, + {Py_tp_free, PyObject_Free}, + {0, 0}, }; +static PyType_Spec Random_Type_spec = { + "_random.Random", + sizeof(RandomObject), + 0, + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, + Random_Type_slots +}; + PyDoc_STRVAR(module_doc, "Module implements the Mersenne Twister random number generator."); -static int -_random_traverse(PyObject *module, visitproc visit, void *arg) -{ - Py_VISIT(get_random_state(module)->Random_Type); - return 0; -} - -static int -_random_clear(PyObject *module) -{ - Py_CLEAR(get_random_state(module)->Random_Type); - Py_CLEAR(get_random_state(module)->Long___abs__); - return 0; -} - -static void -_random_free(void *module) -{ - _random_clear((PyObject *)module); -} - +static int +_random_traverse(PyObject *module, visitproc visit, void *arg) +{ + Py_VISIT(get_random_state(module)->Random_Type); + return 0; +} + +static int +_random_clear(PyObject *module) +{ + Py_CLEAR(get_random_state(module)->Random_Type); + Py_CLEAR(get_random_state(module)->Long___abs__); + return 0; +} + +static void +_random_free(void *module) +{ + _random_clear((PyObject *)module); +} + static struct PyModuleDef _randommodule = { PyModuleDef_HEAD_INIT, "_random", module_doc, - sizeof(_randomstate), + sizeof(_randomstate), NULL, NULL, - _random_traverse, - _random_clear, - _random_free, + _random_traverse, + _random_clear, + _random_free, }; PyMODINIT_FUNC @@ -606,41 +606,41 @@ PyInit__random(void) { PyObject *m; - PyObject *Random_Type = PyType_FromSpec(&Random_Type_spec); - if (Random_Type == NULL) { + PyObject *Random_Type = PyType_FromSpec(&Random_Type_spec); + if (Random_Type == NULL) { return NULL; - } - + } + m = PyModule_Create(&_randommodule); - if (m == NULL) { - Py_DECREF(Random_Type); + if (m == NULL) { + Py_DECREF(Random_Type); return NULL; - } - get_random_state(m)->Random_Type = Random_Type; - - Py_INCREF(Random_Type); - PyModule_AddObject(m, "Random", Random_Type); - - /* Look up and save int.__abs__, which is needed in random_seed(). */ - PyObject *longval = NULL, *longtype = NULL; - longval = PyLong_FromLong(0); - if (longval == NULL) goto fail; - - longtype = PyObject_Type(longval); - if (longtype == NULL) goto fail; - - PyObject *abs = PyObject_GetAttrString(longtype, "__abs__"); - if (abs == NULL) goto fail; - - Py_DECREF(longtype); - Py_DECREF(longval); - get_random_state(m)->Long___abs__ = abs; - + } + get_random_state(m)->Random_Type = Random_Type; + + Py_INCREF(Random_Type); + PyModule_AddObject(m, "Random", Random_Type); + + /* Look up and save int.__abs__, which is needed in random_seed(). */ + PyObject *longval = NULL, *longtype = NULL; + longval = PyLong_FromLong(0); + if (longval == NULL) goto fail; + + longtype = PyObject_Type(longval); + if (longtype == NULL) goto fail; + + PyObject *abs = PyObject_GetAttrString(longtype, "__abs__"); + if (abs == NULL) goto fail; + + Py_DECREF(longtype); + Py_DECREF(longval); + get_random_state(m)->Long___abs__ = abs; + return m; - -fail: - Py_XDECREF(longtype); - Py_XDECREF(longval); - Py_DECREF(m); - return NULL; + +fail: + Py_XDECREF(longtype); + Py_XDECREF(longval); + Py_DECREF(m); + return NULL; } |