gh-112071: Make `_random.Random` methods thread-safe in `--disable-gil` builds (gh-112128)

Co-authored-by: Donghee Na <donghee.na@python.org>
This commit is contained in:
Radislav Chugunov 2023-11-28 06:27:39 +03:00 committed by GitHub
parent 154f099e61
commit ac4b44266d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 10 deletions

View File

@ -175,6 +175,7 @@ genrand_uint32(RandomObject *self)
*/
/*[clinic input]
@critical_section
_random.Random.random
self: self(type="RandomObject *")
@ -184,7 +185,7 @@ random() -> x in the interval [0, 1).
static PyObject *
_random_Random_random_impl(RandomObject *self)
/*[clinic end generated code: output=117ff99ee53d755c input=afb2a59cbbb00349]*/
/*[clinic end generated code: output=117ff99ee53d755c input=26492e52d26e8b7b]*/
{
uint32_t a=genrand_uint32(self)>>5, b=genrand_uint32(self)>>6;
return PyFloat_FromDouble((a*67108864.0+b)*(1.0/9007199254740992.0));
@ -368,6 +369,7 @@ Done:
}
/*[clinic input]
@critical_section
_random.Random.seed
self: self(type="RandomObject *")
@ -382,7 +384,7 @@ of the current time and the process identifier.
static PyObject *
_random_Random_seed_impl(RandomObject *self, PyObject *n)
/*[clinic end generated code: output=0fad1e16ba883681 input=78d6ef0d52532a54]*/
/*[clinic end generated code: output=0fad1e16ba883681 input=46d01d2ba938c7b1]*/
{
if (random_seed(self, n) < 0) {
return NULL;
@ -391,6 +393,7 @@ _random_Random_seed_impl(RandomObject *self, PyObject *n)
}
/*[clinic input]
@critical_section
_random.Random.getstate
self: self(type="RandomObject *")
@ -400,7 +403,7 @@ getstate() -> tuple containing the current state.
static PyObject *
_random_Random_getstate_impl(RandomObject *self)
/*[clinic end generated code: output=bf6cef0c092c7180 input=b937a487928c0e89]*/
/*[clinic end generated code: output=bf6cef0c092c7180 input=b6621f31eb639694]*/
{
PyObject *state;
PyObject *element;
@ -428,6 +431,7 @@ Fail:
/*[clinic input]
@critical_section
_random.Random.setstate
self: self(type="RandomObject *")
@ -438,8 +442,8 @@ 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_impl(RandomObject *self, PyObject *state)
/*[clinic end generated code: output=babfc2c2eac6b027 input=358e898ec07469b7]*/
{
int i;
unsigned long element;
@ -479,7 +483,7 @@ _random_Random_setstate(RandomObject *self, PyObject *state)
}
/*[clinic input]
@critical_section
_random.Random.getrandbits
self: self(type="RandomObject *")
@ -491,7 +495,7 @@ getrandbits(k) -> x. Generates an int with k random bits.
static PyObject *
_random_Random_getrandbits_impl(RandomObject *self, int k)
/*[clinic end generated code: output=b402f82a2158887f input=8c0e6396dd176fc0]*/
/*[clinic end generated code: output=b402f82a2158887f input=87603cd60f79f730]*/
{
int i, words;
uint32_t r;

View File

@ -2,6 +2,7 @@
preserve
[clinic start generated code]*/
#include "pycore_critical_section.h"// Py_BEGIN_CRITICAL_SECTION()
#include "pycore_modsupport.h" // _PyArg_CheckPositional()
PyDoc_STRVAR(_random_Random_random__doc__,
@ -19,7 +20,13 @@ _random_Random_random_impl(RandomObject *self);
static PyObject *
_random_Random_random(RandomObject *self, PyObject *Py_UNUSED(ignored))
{
return _random_Random_random_impl(self);
PyObject *return_value = NULL;
Py_BEGIN_CRITICAL_SECTION(self);
return_value = _random_Random_random_impl(self);
Py_END_CRITICAL_SECTION();
return return_value;
}
PyDoc_STRVAR(_random_Random_seed__doc__,
@ -51,7 +58,9 @@ _random_Random_seed(RandomObject *self, PyObject *const *args, Py_ssize_t nargs)
}
n = args[0];
skip_optional:
Py_BEGIN_CRITICAL_SECTION(self);
return_value = _random_Random_seed_impl(self, n);
Py_END_CRITICAL_SECTION();
exit:
return return_value;
@ -72,7 +81,13 @@ _random_Random_getstate_impl(RandomObject *self);
static PyObject *
_random_Random_getstate(RandomObject *self, PyObject *Py_UNUSED(ignored))
{
return _random_Random_getstate_impl(self);
PyObject *return_value = NULL;
Py_BEGIN_CRITICAL_SECTION(self);
return_value = _random_Random_getstate_impl(self);
Py_END_CRITICAL_SECTION();
return return_value;
}
PyDoc_STRVAR(_random_Random_setstate__doc__,
@ -84,6 +99,21 @@ PyDoc_STRVAR(_random_Random_setstate__doc__,
#define _RANDOM_RANDOM_SETSTATE_METHODDEF \
{"setstate", (PyCFunction)_random_Random_setstate, METH_O, _random_Random_setstate__doc__},
static PyObject *
_random_Random_setstate_impl(RandomObject *self, PyObject *state);
static PyObject *
_random_Random_setstate(RandomObject *self, PyObject *state)
{
PyObject *return_value = NULL;
Py_BEGIN_CRITICAL_SECTION(self);
return_value = _random_Random_setstate_impl(self, state);
Py_END_CRITICAL_SECTION();
return return_value;
}
PyDoc_STRVAR(_random_Random_getrandbits__doc__,
"getrandbits($self, k, /)\n"
"--\n"
@ -106,9 +136,11 @@ _random_Random_getrandbits(RandomObject *self, PyObject *arg)
if (k == -1 && PyErr_Occurred()) {
goto exit;
}
Py_BEGIN_CRITICAL_SECTION(self);
return_value = _random_Random_getrandbits_impl(self, k);
Py_END_CRITICAL_SECTION();
exit:
return return_value;
}
/*[clinic end generated code: output=5c800a28c2d7b9d1 input=a9049054013a1b77]*/
/*[clinic end generated code: output=bf49ece1d341b1b6 input=a9049054013a1b77]*/