From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <gentoo-commits+bounces-1647790-garchives=archives.gentoo.org@lists.gentoo.org>
Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (2048 bits))
	(No client certificate requested)
	by finch.gentoo.org (Postfix) with ESMTPS id BE25615802E
	for <garchives@archives.gentoo.org>; Fri, 28 Jun 2024 18:16:42 +0000 (UTC)
Received: from pigeon.gentoo.org (localhost [127.0.0.1])
	by pigeon.gentoo.org (Postfix) with SMTP id 10C7D2BC013;
	Fri, 28 Jun 2024 18:16:42 +0000 (UTC)
Received: from smtp.gentoo.org (woodpecker.gentoo.org [140.211.166.183])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256)
	(No client certificate requested)
	by pigeon.gentoo.org (Postfix) with ESMTPS id DC7332BC013
	for <gentoo-commits@lists.gentoo.org>; Fri, 28 Jun 2024 18:16:41 +0000 (UTC)
Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256)
	(No client certificate requested)
	by smtp.gentoo.org (Postfix) with ESMTPS id D7F4B335C31
	for <gentoo-commits@lists.gentoo.org>; Fri, 28 Jun 2024 18:16:40 +0000 (UTC)
Received: from localhost.localdomain (localhost [IPv6:::1])
	by oystercatcher.gentoo.org (Postfix) with ESMTP id 4B7051BC0
	for <gentoo-commits@lists.gentoo.org>; Fri, 28 Jun 2024 18:16:39 +0000 (UTC)
From: "Zac Medico" <zmedico@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Content-Transfer-Encoding: 8bit
Content-type: text/plain; charset=UTF-8
Reply-To: gentoo-dev@lists.gentoo.org, "Zac Medico" <zmedico@gentoo.org>
Message-ID: <1719598538.918f4a4aa82f47291deac496d13c2761f306e01f.zmedico@gentoo>
Subject: [gentoo-commits] proj/portage:master commit in: src/
X-VCS-Repository: proj/portage
X-VCS-Files: src/portage_util__whirlpool.c
X-VCS-Directories: src/
X-VCS-Committer: zmedico
X-VCS-Committer-Name: Zac Medico
X-VCS-Revision: 918f4a4aa82f47291deac496d13c2761f306e01f
X-VCS-Branch: master
Date: Fri, 28 Jun 2024 18:16:39 +0000 (UTC)
Precedence: bulk
List-Post: <mailto:gentoo-commits@lists.gentoo.org>
List-Help: <mailto:gentoo-commits+help@lists.gentoo.org>
List-Unsubscribe: <mailto:gentoo-commits+unsubscribe@lists.gentoo.org>
List-Subscribe: <mailto:gentoo-commits+subscribe@lists.gentoo.org>
List-Id: Gentoo Linux mail <gentoo-commits.gentoo.org>
X-BeenThere: gentoo-commits@lists.gentoo.org
X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply
X-Archives-Salt: 1d18a295-1c51-4279-96f6-d8e4cebef381
X-Archives-Hash: dbf5181cf2f0833fb9fc14b5d4dd009b

commit:     918f4a4aa82f47291deac496d13c2761f306e01f
Author:     Zac Medico <zmedico <AT> gentoo <DOT> org>
AuthorDate: Sun Jun 16 19:44:37 2024 +0000
Commit:     Zac Medico <zmedico <AT> gentoo <DOT> org>
CommitDate: Fri Jun 28 18:15:38 2024 +0000
URL:        https://gitweb.gentoo.org/proj/portage.git/commit/?id=918f4a4a

Allow GIL to be disabled in whirlpool C extension

In 3.13 python extensions need to declare support for GIL features, for
example if they don't declare Py_MOD_GIL_NOT_USED then it will cause the
GIL to be enabled even when python was launched in free-threaded mode.

This requires "multi-phase initialization" because Py_mod_create is
incompatible with m_slots. There's a PyUnstable_Module_SetGIL() function
that can be used with single-phase init, but it's an unstable API, so
it's better to use multi-phase init. There's no need to use
PyModule_GetState() because the whirlpool module has no mutable state.

Bug: https://bugs.gentoo.org/934220
Signed-off-by: Zac Medico <zmedico <AT> gentoo.org>

 src/portage_util__whirlpool.c | 46 +++++++++++++++++++++++++++----------------
 1 file changed, 29 insertions(+), 17 deletions(-)

diff --git a/src/portage_util__whirlpool.c b/src/portage_util__whirlpool.c
index 6c9421e56b..4af9307c71 100644
--- a/src/portage_util__whirlpool.c
+++ b/src/portage_util__whirlpool.c
@@ -1112,30 +1112,42 @@ static PyTypeObject WhirlpoolType = {
     .tp_methods = Whirlpool_methods,
 };
 
+static int
+whirlpool_exec(PyObject *mod)
+{
+    if (PyType_Ready(&WhirlpoolType) < 0)
+        return -1;
+
+    Py_INCREF(&WhirlpoolType);
+    if (PyModule_AddObject(mod, "Whirlpool", (PyObject *) &WhirlpoolType) < 0) {
+        Py_DECREF(&WhirlpoolType);
+        return -1;
+    }
+
+    return 0;
+}
+
+static PyModuleDef_Slot _whirlpoolmodule_slots[] = {
+    {Py_mod_exec, whirlpool_exec},
+#ifdef Py_MOD_PER_INTERPRETER_GIL_SUPPORTED
+    {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
+#endif
+#ifdef Py_MOD_GIL_NOT_USED
+    {Py_mod_gil, Py_MOD_GIL_NOT_USED},
+#endif
+    {0, NULL},
+};
+
 static PyModuleDef moduledef = {
     PyModuleDef_HEAD_INIT,
     .m_name = "_whirlpool",
     .m_doc = "Reference Whirlpool implementation",
-    .m_size = -1,
+    .m_size = 0,
+    .m_slots = _whirlpoolmodule_slots,
 };
 
 PyMODINIT_FUNC
 PyInit__whirlpool(void)
 {
-    PyObject *m;
-    if (PyType_Ready(&WhirlpoolType) < 0)
-        return NULL;
-
-    m = PyModule_Create(&moduledef);
-    if (m == NULL)
-        return NULL;
-
-    Py_INCREF(&WhirlpoolType);
-    if (PyModule_AddObject(m, "Whirlpool", (PyObject *) &WhirlpoolType) < 0) {
-        Py_DECREF(&WhirlpoolType);
-        Py_DECREF(m);
-        return NULL;
-    }
-
-    return m;
+    return PyModuleDef_Init(&moduledef);
 }