Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a race condition in ``_random.Random.__init__`` method in free-threading
mode.
35 changes: 14 additions & 21 deletions Modules/_randommodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ typedef struct {

/*[clinic input]
module _random
class _random.Random "RandomObject *" "_randomstate_type(type)->Random_Type"
class _random.Random "RandomObject *" "(PyTypeObject *)_randomstate_type(Py_TYPE(self))->Random_Type"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=70a2c99619474983]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f04bcbfba61a322e]*/

/* Random methods */

Expand Down Expand Up @@ -549,27 +549,20 @@ _random_Random_getrandbits_impl(RandomObject *self, uint64_t k)
return result;
}

static int
random_init(PyObject *self, PyObject *args, PyObject *kwds)
{
PyObject *arg = NULL;
_randomstate *state = _randomstate_type(Py_TYPE(self));

if ((Py_IS_TYPE(self, (PyTypeObject *)state->Random_Type) ||
Py_TYPE(self)->tp_init == ((PyTypeObject*)state->Random_Type)->tp_init) &&
!_PyArg_NoKeywords("Random", kwds)) {
return -1;
}

if (PyTuple_GET_SIZE(args) > 1) {
PyErr_SetString(PyExc_TypeError, "Random() requires 0 or 1 argument");
return -1;
}
/*[clinic input]
@critical_section
@text_signature "($self, [seed])"
_random.Random.__init__ as random_init

if (PyTuple_GET_SIZE(args) == 1)
arg = PyTuple_GET_ITEM(args, 0);
seed: object = NULL
/
[clinic start generated code]*/

return random_seed(RandomObject_CAST(self), arg);
static int
random_init_impl(RandomObject *self, PyObject *seed)
/*[clinic end generated code: output=260734a3739c394f input=e516bf32e8a05e28]*/
{
return random_seed(self, seed);
}
Comment thread
sobolevn marked this conversation as resolved.


Expand Down
33 changes: 32 additions & 1 deletion Modules/clinic/_randommodule.c.h

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

Loading