From 2f6c5aa989a2ff506ef0b714c949f5ded8b2c088 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Mon, 1 Sep 2025 15:18:50 +0200 Subject: [PATCH 1/3] fully implement GC protocol for `random.Random` --- Modules/_randommodule.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/Modules/_randommodule.c b/Modules/_randommodule.c index 2f4f388ce1161a..8e45f230b8be88 100644 --- a/Modules/_randommodule.c +++ b/Modules/_randommodule.c @@ -572,6 +572,12 @@ random_init(PyObject *self, PyObject *args, PyObject *kwds) return random_seed(RandomObject_CAST(self), arg); } +static int +random_traverse(PyObject *op, visitproc visit, void *arg) +{ + Py_VISIT(Py_TYPE(op)); + return 0; +} static PyMethodDef random_methods[] = { _RANDOM_RANDOM_RANDOM_METHODDEF @@ -590,16 +596,15 @@ static PyType_Slot Random_Type_slots[] = { {Py_tp_methods, random_methods}, {Py_tp_new, PyType_GenericNew}, {Py_tp_init, random_init}, - {Py_tp_free, PyObject_Free}, + {Py_tp_traverse, random_traverse}, {0, 0}, }; static PyType_Spec Random_Type_spec = { - "_random.Random", - sizeof(RandomObject), - 0, - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, - Random_Type_slots + .name = "_random.Random", + .basicsize = sizeof(RandomObject), + .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, + .slots = Random_Type_slots }; PyDoc_STRVAR(module_doc, From 488be06544ee5ada166607677470c1ee2794312a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Tue, 2 Sep 2025 10:20:40 +0200 Subject: [PATCH 2/3] make random.Random immutable instead of supporting full GC --- Modules/_randommodule.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/Modules/_randommodule.c b/Modules/_randommodule.c index 8e45f230b8be88..aa2fd28c232f28 100644 --- a/Modules/_randommodule.c +++ b/Modules/_randommodule.c @@ -572,12 +572,6 @@ random_init(PyObject *self, PyObject *args, PyObject *kwds) return random_seed(RandomObject_CAST(self), arg); } -static int -random_traverse(PyObject *op, visitproc visit, void *arg) -{ - Py_VISIT(Py_TYPE(op)); - return 0; -} static PyMethodDef random_methods[] = { _RANDOM_RANDOM_RANDOM_METHODDEF @@ -596,14 +590,18 @@ static PyType_Slot Random_Type_slots[] = { {Py_tp_methods, random_methods}, {Py_tp_new, PyType_GenericNew}, {Py_tp_init, random_init}, - {Py_tp_traverse, random_traverse}, + {Py_tp_free, PyObject_Free}, {0, 0}, }; static PyType_Spec Random_Type_spec = { .name = "_random.Random", .basicsize = sizeof(RandomObject), - .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, + .flags = ( + Py_TPFLAGS_DEFAULT + | Py_TPFLAGS_BASETYPE + | Py_TPFLAGS_IMMUTABLETYPE + ), .slots = Random_Type_slots }; From 2068b50d9a902e39a00bbe736bf1e7174a2cec22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Tue, 2 Sep 2025 10:23:22 +0200 Subject: [PATCH 3/3] blurb --- .../next/Library/2025-09-02-10-23-09.gh-issue-116946.U6RpwK.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2025-09-02-10-23-09.gh-issue-116946.U6RpwK.rst diff --git a/Misc/NEWS.d/next/Library/2025-09-02-10-23-09.gh-issue-116946.U6RpwK.rst b/Misc/NEWS.d/next/Library/2025-09-02-10-23-09.gh-issue-116946.U6RpwK.rst new file mode 100644 index 00000000000000..015cf24c8869f8 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-09-02-10-23-09.gh-issue-116946.U6RpwK.rst @@ -0,0 +1,2 @@ +The :class:`!_random.Random` C type is now immutable. Patch by Bénédikt +Tran.