]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-149816: Fix a RC in `_random.Random.__init__` method (#149824)
authorsobolevn <mail@sobolevn.me>
Mon, 18 May 2026 15:39:54 +0000 (18:39 +0300)
committerGitHub <noreply@github.com>
Mon, 18 May 2026 15:39:54 +0000 (18:39 +0300)
Misc/NEWS.d/next/Library/2026-05-14-15-55-28.gh-issue-149816.ZaXQ0q.rst [new file with mode: 0644]
Modules/_randommodule.c
Modules/clinic/_randommodule.c.h

diff --git a/Misc/NEWS.d/next/Library/2026-05-14-15-55-28.gh-issue-149816.ZaXQ0q.rst b/Misc/NEWS.d/next/Library/2026-05-14-15-55-28.gh-issue-149816.ZaXQ0q.rst
new file mode 100644 (file)
index 0000000..3ea7007
--- /dev/null
@@ -0,0 +1,2 @@
+Fix a race condition in ``_random.Random.__init__`` method in free-threading
+mode.
index 0fb73481651748506c035fe576dfb40e2835b127..a06966be23be1efed667ab467d28ef3fa6d955cf 100644 (file)
@@ -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 */
 
@@ -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);
 }
 
 
index 2563a16aea0b6f94d9bf4125ea885fd06a6b21b4..ca9cad7a572dadf2a1fc71e4c8c5d1143d9890e8 100644 (file)
@@ -143,4 +143,35 @@ _random_Random_getrandbits(PyObject *self, PyObject *arg)
 exit:
     return return_value;
 }
-/*[clinic end generated code: output=7ce97b2194eecaf7 input=a9049054013a1b77]*/
+
+static int
+random_init_impl(RandomObject *self, PyObject *seed);
+
+static int
+random_init(PyObject *self, PyObject *args, PyObject *kwargs)
+{
+    int return_value = -1;
+    PyTypeObject *base_tp = (PyTypeObject *)_randomstate_type(Py_TYPE(self))->Random_Type;
+    PyObject *seed = NULL;
+
+    if ((Py_IS_TYPE(self, base_tp) ||
+         Py_TYPE(self)->tp_new == base_tp->tp_new) &&
+        !_PyArg_NoKeywords("Random", kwargs)) {
+        goto exit;
+    }
+    if (!_PyArg_CheckPositional("Random", PyTuple_GET_SIZE(args), 0, 1)) {
+        goto exit;
+    }
+    if (PyTuple_GET_SIZE(args) < 1) {
+        goto skip_optional;
+    }
+    seed = PyTuple_GET_ITEM(args, 0);
+skip_optional:
+    Py_BEGIN_CRITICAL_SECTION(self);
+    return_value = random_init_impl((RandomObject *)self, seed);
+    Py_END_CRITICAL_SECTION();
+
+exit:
+    return return_value;
+}
+/*[clinic end generated code: output=ec95f7df0c3f3c19 input=a9049054013a1b77]*/