Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions Modules/_randommodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
#endif

#include "Python.h"
#include "pycore_critical_section.h"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to add this header manually since the AC automatically adds it.
Please rebase the branch and rerun the make clinic

#include "pycore_long.h" // _PyLong_NumBits()
#include "pycore_modsupport.h" // _PyArg_NoKeywords()
#include "pycore_moduleobject.h" // _PyModule_GetState()
Expand Down Expand Up @@ -175,6 +176,7 @@ genrand_uint32(RandomObject *self)
*/

/*[clinic input]
@critical_section
_random.Random.random

self: self(type="RandomObject *")
Expand All @@ -184,7 +186,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 +370,7 @@ random_seed(RandomObject *self, PyObject *arg)
}

/*[clinic input]
@critical_section
_random.Random.seed

self: self(type="RandomObject *")
Expand All @@ -382,7 +385,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 +394,7 @@ _random_Random_seed_impl(RandomObject *self, PyObject *n)
}

/*[clinic input]
@critical_section
_random.Random.getstate

self: self(type="RandomObject *")
Expand All @@ -400,7 +404,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 +432,7 @@ _random_Random_getstate_impl(RandomObject *self)


/*[clinic input]
@critical_section
_random.Random.setstate

self: self(type="RandomObject *")
Expand All @@ -438,8 +443,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 +484,7 @@ _random_Random_setstate(RandomObject *self, PyObject *state)
}

/*[clinic input]

@critical_section
_random.Random.getrandbits

self: self(type="RandomObject *")
Expand All @@ -491,7 +496,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
37 changes: 34 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.

Loading