Skip to content

Commit ac4b442

Browse files
chgnrdvcorona10
andauthored
gh-112071: Make _random.Random methods thread-safe in --disable-gil builds (gh-112128)
Co-authored-by: Donghee Na <[email protected]>
1 parent 154f099 commit ac4b442

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

Modules/_randommodule.c

+11-7
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ genrand_uint32(RandomObject *self)
175175
*/
176176

177177
/*[clinic input]
178+
@critical_section
178179
_random.Random.random
179180
180181
self: self(type="RandomObject *")
@@ -184,7 +185,7 @@ random() -> x in the interval [0, 1).
184185

185186
static PyObject *
186187
_random_Random_random_impl(RandomObject *self)
187-
/*[clinic end generated code: output=117ff99ee53d755c input=afb2a59cbbb00349]*/
188+
/*[clinic end generated code: output=117ff99ee53d755c input=26492e52d26e8b7b]*/
188189
{
189190
uint32_t a=genrand_uint32(self)>>5, b=genrand_uint32(self)>>6;
190191
return PyFloat_FromDouble((a*67108864.0+b)*(1.0/9007199254740992.0));
@@ -368,6 +369,7 @@ random_seed(RandomObject *self, PyObject *arg)
368369
}
369370

370371
/*[clinic input]
372+
@critical_section
371373
_random.Random.seed
372374
373375
self: self(type="RandomObject *")
@@ -382,7 +384,7 @@ of the current time and the process identifier.
382384

383385
static PyObject *
384386
_random_Random_seed_impl(RandomObject *self, PyObject *n)
385-
/*[clinic end generated code: output=0fad1e16ba883681 input=78d6ef0d52532a54]*/
387+
/*[clinic end generated code: output=0fad1e16ba883681 input=46d01d2ba938c7b1]*/
386388
{
387389
if (random_seed(self, n) < 0) {
388390
return NULL;
@@ -391,6 +393,7 @@ _random_Random_seed_impl(RandomObject *self, PyObject *n)
391393
}
392394

393395
/*[clinic input]
396+
@critical_section
394397
_random.Random.getstate
395398
396399
self: self(type="RandomObject *")
@@ -400,7 +403,7 @@ getstate() -> tuple containing the current state.
400403

401404
static PyObject *
402405
_random_Random_getstate_impl(RandomObject *self)
403-
/*[clinic end generated code: output=bf6cef0c092c7180 input=b937a487928c0e89]*/
406+
/*[clinic end generated code: output=bf6cef0c092c7180 input=b6621f31eb639694]*/
404407
{
405408
PyObject *state;
406409
PyObject *element;
@@ -428,6 +431,7 @@ _random_Random_getstate_impl(RandomObject *self)
428431

429432

430433
/*[clinic input]
434+
@critical_section
431435
_random.Random.setstate
432436
433437
self: self(type="RandomObject *")
@@ -438,8 +442,8 @@ setstate(state) -> None. Restores generator state.
438442
[clinic start generated code]*/
439443

440444
static PyObject *
441-
_random_Random_setstate(RandomObject *self, PyObject *state)
442-
/*[clinic end generated code: output=fd1c3cd0037b6681 input=b3b4efbb1bc66af8]*/
445+
_random_Random_setstate_impl(RandomObject *self, PyObject *state)
446+
/*[clinic end generated code: output=babfc2c2eac6b027 input=358e898ec07469b7]*/
443447
{
444448
int i;
445449
unsigned long element;
@@ -479,7 +483,7 @@ _random_Random_setstate(RandomObject *self, PyObject *state)
479483
}
480484

481485
/*[clinic input]
482-
486+
@critical_section
483487
_random.Random.getrandbits
484488
485489
self: self(type="RandomObject *")
@@ -491,7 +495,7 @@ getrandbits(k) -> x. Generates an int with k random bits.
491495

492496
static PyObject *
493497
_random_Random_getrandbits_impl(RandomObject *self, int k)
494-
/*[clinic end generated code: output=b402f82a2158887f input=8c0e6396dd176fc0]*/
498+
/*[clinic end generated code: output=b402f82a2158887f input=87603cd60f79f730]*/
495499
{
496500
int i, words;
497501
uint32_t r;

Modules/clinic/_randommodule.c.h

+35-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)