Blob Blame History Raw
From 2fad72cde02aba45f392fe3a7076bcd549315cab Mon Sep 17 00:00:00 2001
From: Igor Gnatenko <i.gnatenko.brain@gmail.com>
Date: Wed, 25 Nov 2015 23:12:21 +0100
Subject: [PATCH 1/2] py3: make modules compatible

Signed-off-by: Igor Gnatenko <i.gnatenko.brain@gmail.com>
---
 gradunwarp/core/interp3_ext.c               | 42 ++++++++++++++++++++++++++--
 gradunwarp/core/legendre_ext.c              | 43 +++++++++++++++++++++++++++--
 gradunwarp/core/transform_coordinates_ext.c | 43 +++++++++++++++++++++++++++--
 3 files changed, 119 insertions(+), 9 deletions(-)

diff --git a/gradunwarp/core/interp3_ext.c b/gradunwarp/core/interp3_ext.c
index ee1b191..9990e3c 100644
--- a/gradunwarp/core/interp3_ext.c
+++ b/gradunwarp/core/interp3_ext.c
@@ -26,9 +26,45 @@ static PyMethodDef tricubicmethods[] = {
 };
 
 // This function is essential for an extension for Numpy created in C
-void initinterp3_ext() {
-	(void) Py_InitModule("interp3_ext", tricubicmethods);
-	import_array();
+#if PY_MAJOR_VERSION >= 3
+
+static struct PyModuleDef moduledef = {
+        PyModuleDef_HEAD_INIT,
+        "interp3_ext",
+        NULL,
+        -1,
+        tricubicmethods,
+        NULL,
+        NULL,
+        NULL,
+        NULL
+};
+#define INITERROR return NULL
+
+PyObject *
+PyInit_interp3_ext(void)
+
+#else
+#define INITERROR return
+
+void
+initinterp3_ext(void)
+#endif
+{
+#if PY_MAJOR_VERSION >= 3
+    PyObject *module = PyModule_Create(&moduledef);
+#else
+    PyObject *module = Py_InitModule("interp3_ext", tricubicmethods);
+#endif
+
+    if (module == NULL)
+        INITERROR;
+
+    import_array();
+
+#if PY_MAJOR_VERSION >= 3
+    return module;
+#endif
 }
 
 // the data should be FLOAT32 and should be ensured in the wrapper 
diff --git a/gradunwarp/core/legendre_ext.c b/gradunwarp/core/legendre_ext.c
index 6ba6b91..372c629 100644
--- a/gradunwarp/core/legendre_ext.c
+++ b/gradunwarp/core/legendre_ext.c
@@ -23,9 +23,46 @@ static PyMethodDef legendremethods[] = {
 };
 
 // This function is essential for an extension for Numpy created in C
-void initlegendre_ext() {
-	(void) Py_InitModule("legendre_ext", legendremethods);
-	import_array();
+#if PY_MAJOR_VERSION >= 3
+
+static struct PyModuleDef moduledef = {
+        PyModuleDef_HEAD_INIT,
+        "legendre_ext",
+        NULL,
+        -1,
+        legendremethods,
+        NULL,
+        NULL,
+        NULL,
+        NULL
+};
+
+#define INITERROR return NULL
+
+PyObject *
+PyInit_legendre_ext(void)
+
+#else
+#define INITERROR return
+
+void
+initlegendre_ext(void)
+#endif
+{
+#if PY_MAJOR_VERSION >= 3
+    PyObject *module = PyModule_Create(&moduledef);
+#else
+    PyObject *module = Py_InitModule("legendre_ext", legendremethods);
+#endif
+
+    if (module == NULL)
+        INITERROR;
+
+    import_array();
+
+#if PY_MAJOR_VERSION >= 3
+    return module;
+#endif
 }
 
 // the data should be FLOAT32 and should be ensured in the wrapper 
diff --git a/gradunwarp/core/transform_coordinates_ext.c b/gradunwarp/core/transform_coordinates_ext.c
index e5ba7b1..8323f0b 100644
--- a/gradunwarp/core/transform_coordinates_ext.c
+++ b/gradunwarp/core/transform_coordinates_ext.c
@@ -24,9 +24,46 @@ static PyMethodDef transform_coordinatesmethods[] = {
 
 
 // This function is essential for an extension for Numpy created in C
-void inittransform_coordinates_ext() {
-	(void) Py_InitModule("transform_coordinates_ext", transform_coordinatesmethods);
-	import_array();
+#if PY_MAJOR_VERSION >= 3
+
+static struct PyModuleDef moduledef = {
+        PyModuleDef_HEAD_INIT,
+        "transform_coordinates_ext",
+        NULL,
+        -1,
+        transform_coordinatesmethods,
+        NULL,
+        NULL,
+        NULL,
+        NULL
+};
+
+#define INITERROR return NULL
+
+PyObject *
+PyInit_transform_coordinates_ext(void)
+
+#else
+#define INITERROR return
+
+void
+inittransform_coordinates_ext(void)
+#endif
+{
+#if PY_MAJOR_VERSION >= 3
+    PyObject *module = PyModule_Create(&moduledef);
+#else
+    PyObject *module = Py_InitModule("transform_coordinates_ext", transform_coordinatesmethods);
+#endif
+
+    if (module == NULL)
+        INITERROR;
+
+    import_array();
+
+#if PY_MAJOR_VERSION >= 3
+    return module;
+#endif
 }
 
 
-- 
2.6.3