From: Vincent Fazio <vfazio@gmail.com>
To: linux-gpio@vger.kernel.org
Cc: Vincent Fazio <vfazio@gmail.com>
Subject: [libgpiod][PATCH 7/9] bindings: python: migrate the gpiod._ext module to multi-phase init
Date: Tue, 21 Apr 2026 20:20:39 -0500 [thread overview]
Message-ID: <20260422012041.39933-8-vfazio@gmail.com> (raw)
In-Reply-To: <20260422012041.39933-1-vfazio@gmail.com>
Single-phase initialization has been classified as legacy within CPython
documentation [0] with multi-phase being its successor [1].
As such, switch to the new methodology.
[0]: https://docs.python.org/3/c-api/extension-modules.html#legacy-single-phase-initialization
[1]: https://docs.python.org/3/c-api/extension-modules.html#multi-phase-initialization
Signed-off-by: Vincent Fazio <vfazio@gmail.com>
---
bindings/python/gpiod/ext/module.c | 68 +++++++++++++++---------------
1 file changed, 33 insertions(+), 35 deletions(-)
diff --git a/bindings/python/gpiod/ext/module.c b/bindings/python/gpiod/ext/module.c
index 25c252a..71fa3c2 100644
--- a/bindings/python/gpiod/ext/module.c
+++ b/bindings/python/gpiod/ext/module.c
@@ -135,12 +135,6 @@ static PyMethodDef module_methods[] = {
{ }
};
-static PyModuleDef module_def = {
- PyModuleDef_HEAD_INIT,
- .m_name = "gpiod._ext",
- .m_methods = module_methods,
-};
-
extern PyTypeObject chip_type;
extern PyTypeObject line_config_type;
extern PyTypeObject line_settings_type;
@@ -154,53 +148,57 @@ static PyTypeObject *types[] = {
NULL,
};
-PyMODINIT_FUNC PyInit__ext(void)
+static int module_exec(PyObject* module)
{
const struct module_const *modconst;
- PyObject *module, *all;
+ PyObject *all;
PyTypeObject **type;
int ret;
- module = PyModule_Create(&module_def);
- if (!module)
- return NULL;
-
ret = PyModule_AddStringConstant(module, "api_version",
gpiod_api_version());
- if (ret) {
- Py_DECREF(module);
- return NULL;
- }
+
+ if (ret < 0)
+ return -1;
all = PyList_New(0);
- if (!all) {
- Py_DECREF(module);
- return NULL;
- }
+ if (!all)
+ return -1;
ret = PyModule_AddObjectRef(module, "__all__", all);
Py_DECREF(all);
- if (ret) {
- Py_DECREF(module);
- return NULL;
- }
+ if (ret)
+ return -1;
for (type = types; *type; type++) {
ret = PyModule_AddType(module, *type);
- if (ret) {
- Py_DECREF(module);
- return NULL;
- }
+ if (ret < 0)
+ return -1;
}
for (modconst = module_constants; modconst->name; modconst++) {
- ret = PyModule_AddIntConstant(module,
- modconst->name, modconst->val);
- if (ret) {
- Py_DECREF(module);
- return NULL;
- }
+ ret = PyModule_AddIntConstant(module, modconst->name,
+ modconst->val);
+ if (ret < 0)
+ return -1;
}
- return module;
+ return 0;
+}
+
+static struct PyModuleDef_Slot module_slots[] = {
+ {Py_mod_exec, module_exec},
+ {0, NULL},
+};
+
+static PyModuleDef module_def = {
+ PyModuleDef_HEAD_INIT,
+ .m_name = "gpiod._ext",
+ .m_methods = module_methods,
+ .m_slots = module_slots,
+};
+
+PyMODINIT_FUNC PyInit__ext(void)
+{
+ return PyModuleDef_Init(&module_def);
}
--
2.43.0
next prev parent reply other threads:[~2026-04-22 1:21 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-22 1:20 [libgpiod][PATCH 0/9] bindings: python: modernize C extensions Vincent Fazio
2026-04-22 1:20 ` [libgpiod][PATCH 1/9] bindings: python: use Py_RETURN_NONE in chip_get_line_name Vincent Fazio
2026-04-22 1:20 ` [libgpiod][PATCH 2/9] bindings: python: avoid PyObject_CallMethod during chip finalize Vincent Fazio
2026-04-22 1:20 ` [libgpiod][PATCH 3/9] bindings: python: avoid PyObject_CallMethod during request finalize Vincent Fazio
2026-04-22 1:20 ` [libgpiod][PATCH 4/9] bindings: python: simplify disallowing _ext.Request from being created Vincent Fazio
2026-04-22 1:20 ` [libgpiod][PATCH 5/9] bindings: python: use suggestions from upgrade_pythoncapi.py Vincent Fazio
2026-04-22 1:20 ` [libgpiod][PATCH 6/9] bindings: python: use PyImport_ImportModuleAttrString when available Vincent Fazio
2026-04-22 1:20 ` Vincent Fazio [this message]
2026-04-22 1:20 ` [libgpiod][PATCH 8/9] bindings: python: tests: migrate the system module to multi-phase init Vincent Fazio
2026-04-22 1:20 ` [libgpiod][PATCH 9/9] bindings: python: tests: migrate the gpiosim " Vincent Fazio
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260422012041.39933-8-vfazio@gmail.com \
--to=vfazio@gmail.com \
--cc=linux-gpio@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox