[02/10] _fireinfo: Migrate to Python 3

Message ID 20210507111654.2397-3-michael.tremer@ipfire.org
State New
Headers show
Series [01/10] configure: Link against Python 3 | expand

Commit Message

Michael Tremer May 7, 2021, 11:16 a.m. UTC
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
---
 src/_fireinfo/fireinfo.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)

Patch

diff --git a/src/_fireinfo/fireinfo.c b/src/_fireinfo/fireinfo.c
index 6601c21..58ee7e2 100644
--- a/src/_fireinfo/fireinfo.c
+++ b/src/_fireinfo/fireinfo.c
@@ -185,7 +185,7 @@  do_detect_hypervisor() {
 		if (!hypervisor_vendor)
 			Py_RETURN_NONE;
 
-		return PyString_FromString(hypervisor_vendor);
+		return PyUnicode_FromString(hypervisor_vendor);
 	}
 
 	Py_RETURN_NONE;
@@ -215,22 +215,30 @@  do_get_harddisk_serial(PyObject *o, PyObject *args) {
 		strncpy(serial, (const char *)hd.serial_no, sizeof(serial) - 1);
 
 		if (serial[0])
-			return PyString_FromString(serial);
+			return PyUnicode_FromString(serial);
 	}
 
 	Py_RETURN_NONE;
 }
 
-static PyMethodDef fireinfoModuleMethods[] = {
+static PyMethodDef fireinfo_methods[] = {
 	{ "detect_hypervisor", (PyCFunction) do_detect_hypervisor, METH_NOARGS, NULL },
 	{ "get_harddisk_serial", (PyCFunction) do_get_harddisk_serial, METH_VARARGS, NULL },
 	{ NULL, NULL, 0, NULL }
 };
 
-void init_fireinfo(void) {
-	PyObject *m;
+static struct PyModuleDef fireinfo_module = {
+	.m_base = PyModuleDef_HEAD_INIT,
+	.m_name = "_fireinfo",
+	.m_size = -1,
+	.m_doc = "Python module for fireinfo",
+	.m_methods = fireinfo_methods,
+};
+
+PyMODINIT_FUNC PyInit__fireinfo(void) {
+	PyObject* m = PyModule_Create(&fireinfo_module);
+	if (!m)
+		return NULL;
 
-	m = Py_InitModule("_fireinfo", fireinfoModuleMethods);
-	if (m == NULL)
-		return;
+	return m;
 }