Skip to content

Commit

Permalink
gh-112071: Make _random.Random methods thread-safe in `--disable-gi…
Browse files Browse the repository at this point in the history
…l` builds (gh-112128)


Co-authored-by: Donghee Na <donghee.na@python.org>
  • Loading branch information
chgnrdv and corona10 authored Nov 28, 2023
1 parent 154f099 commit ac4b442
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
18 changes: 11 additions & 7 deletions Modules/_randommodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ genrand_uint32(RandomObject *self)
*/

/*[clinic input]
@critical_section
_random.Random.random
self: self(type="RandomObject *")
Expand All @@ -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));
Expand Down Expand Up @@ -368,6 +369,7 @@ random_seed(RandomObject *self, PyObject *arg)
}

/*[clinic input]
@critical_section
_random.Random.seed
self: self(type="RandomObject *")
Expand All @@ -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;
Expand All @@ -391,6 +393,7 @@ _random_Random_seed_impl(RandomObject *self, PyObject *n)
}

/*[clinic input]
@critical_section
_random.Random.getstate
self: self(type="RandomObject *")
Expand All @@ -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;
Expand Down Expand Up @@ -428,6 +431,7 @@ _random_Random_getstate_impl(RandomObject *self)


/*[clinic input]
@critical_section
_random.Random.setstate
self: self(type="RandomObject *")
Expand All @@ -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;
Expand Down Expand Up @@ -479,7 +483,7 @@ _random_Random_setstate(RandomObject *self, PyObject *state)
}

/*[clinic input]
@critical_section
_random.Random.getrandbits
self: self(type="RandomObject *")
Expand All @@ -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;
Expand Down
38 changes: 35 additions & 3 deletions Modules/clinic/_randommodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ac4b442

Please sign in to comment.