[libvirt] [PATCH python 14/15] override: Fix native module registration to work with Python3

Daniel P. Berrange berrange at redhat.com
Tue Dec 3 16:36:50 UTC 2013


From: "Daniel P. Berrange" <berrange at redhat.com>

The way native modules are registered has completely
changed, so the code must be #ifdef'd for Python2 & 3

Signed-off-by: Daniel P. Berrange <berrange at redhat.com>
---
 libvirt-lxc-override.c  | 73 +++++++++++++++++++++++++++++++++++------------
 libvirt-override.c      | 75 ++++++++++++++++++++++++++++++++++++-------------
 libvirt-qemu-override.c | 73 +++++++++++++++++++++++++++++++++++------------
 3 files changed, 166 insertions(+), 55 deletions(-)

diff --git a/libvirt-lxc-override.c b/libvirt-lxc-override.c
index 03b00b0..60b41d8 100644
--- a/libvirt-lxc-override.c
+++ b/libvirt-lxc-override.c
@@ -21,10 +21,18 @@
 #include "libvirt-utils.h"
 #include "build/libvirt-lxc.h"
 
-#ifndef __CYGWIN__
-extern void initlibvirtmod_lxc(void);
+#if PY_MAJOR_VERSION > 2
+# ifndef __CYGWIN__
+extern PyObject *PyInit_libvirtmod_lxc(void);
+# else
+extern PyObject *PyInit_cygvirtmod_lxc(void);
+# endif
 #else
+# ifndef __CYGWIN__
+extern void initlibvirtmod_lxc(void);
+# else
 extern void initcygvirtmod_lxc(void);
+# endif
 #endif
 
 #if 0
@@ -110,30 +118,59 @@ static PyMethodDef libvirtLxcMethods[] = {
     {NULL, NULL, 0, NULL}
 };
 
+#if PY_MAJOR_VERSION > 2
+static struct PyModuleDef moduledef = {
+        PyModuleDef_HEAD_INIT,
+# ifndef __CYGWIN__
+        "libvirtmod_lxc",
+# else
+        "cygvirtmod_lxc",
+# endif
+        NULL,
+        -1,
+        libvirtLxcMethods,
+        NULL,
+        NULL,
+        NULL,
+        NULL
+};
+
+PyObject *
+# ifndef __CYGWIN__
+PyInit_libvirtmod_lxc
+# else
+PyInit_cygvirtmod_lxc
+# endif
+  (void)
+{
+    PyObject *module;
+
+    if (virInitialize() < 0)
+        return NULL;
+
+    module = PyModule_Create(&moduledef);
+
+    return module;
+}
+#else /* ! PY_MAJOR_VERSION > 2 */
 void
-#ifndef __CYGWIN__
+# ifndef __CYGWIN__
 initlibvirtmod_lxc
-#else
+# else
 initcygvirtmod_lxc
-#endif
+# endif
   (void)
 {
-    static int initialized = 0;
-
-    if (initialized != 0)
-        return;
-
     if (virInitialize() < 0)
         return;
 
     /* initialize the python extension module */
     Py_InitModule((char *)
-#ifndef __CYGWIN__
-                  "libvirtmod_lxc"
-#else
-                  "cygvirtmod_lxc"
-#endif
-                  , libvirtLxcMethods);
-
-    initialized = 1;
+# ifndef __CYGWIN__
+                  "libvirtmod_lxc",
+# else
+                  "cygvirtmod_lxc",
+# endif
+		  libvirtLxcMethods);
 }
+#endif /* ! PY_MAJOR_VERSION > 2 */
diff --git a/libvirt-override.c b/libvirt-override.c
index 847e6ce..aaee6b8 100644
--- a/libvirt-override.c
+++ b/libvirt-override.c
@@ -25,10 +25,18 @@
 #include "build/libvirt.h"
 #include "libvirt-utils.h"
 
-#ifndef __CYGWIN__
-extern void initlibvirtmod(void);
+#if PY_MAJOR_VERSION > 2
+# ifndef __CYGWIN__
+extern PyObject *PyInit_libvirtmod(void);
+# else
+extern PyObject *PyInit_cygvirtmod(void);
+# endif
 #else
+# ifndef __CYGWIN__
+extern void initlibvirtmod(void);
+# else
 extern void initcygvirtmod(void);
+# endif
 #endif
 
 #if 0
@@ -7453,30 +7461,59 @@ static PyMethodDef libvirtMethods[] = {
     {NULL, NULL, 0, NULL}
 };
 
+#if PY_MAJOR_VERSION > 2
+static struct PyModuleDef moduledef = {
+        PyModuleDef_HEAD_INIT,
+# ifndef __CYGWIN__
+        "libvirtmod",
+# else
+        "cygvirtmod",
+# endif
+        NULL,
+        -1,
+        libvirtMethods,
+        NULL,
+        NULL,
+        NULL,
+        NULL
+};
+
+PyObject *
+# ifndef __CYGWIN__
+PyInit_libvirtmod
+# else
+PyInit_cygvirtmod
+# endif
+  (void)
+{
+    PyObject *module;
+
+    if (virInitialize() < 0)
+        return NULL;
+
+    module = PyModule_Create(&moduledef);
+
+    return module;
+}
+#else /* ! PY_MAJOR_VERSION > 2 */
 void
-#ifndef __CYGWIN__
+# ifndef __CYGWIN__
 initlibvirtmod
-#else
+# else
 initcygvirtmod
-#endif
+# endif
   (void)
 {
-    static int initialized = 0;
-
-    if (initialized != 0)
-        return;
-
     if (virInitialize() < 0)
         return;
 
     /* initialize the python extension module */
     Py_InitModule((char *)
-#ifndef __CYGWIN__
-                  "libvirtmod"
-#else
-                  "cygvirtmod"
-#endif
-                  , libvirtMethods);
-
-    initialized = 1;
-}
+# ifndef __CYGWIN__
+                  "libvirtmod",
+# else
+                  "cygvirtmod",
+# endif
+		  libvirtMethods);
+}
+#endif /* ! PY_MAJOR_VERSION > 2 */
diff --git a/libvirt-qemu-override.c b/libvirt-qemu-override.c
index a8e8c09..72257ac 100644
--- a/libvirt-qemu-override.c
+++ b/libvirt-qemu-override.c
@@ -21,10 +21,18 @@
 #include "libvirt-utils.h"
 #include "build/libvirt-qemu.h"
 
-#ifndef __CYGWIN__
-extern void initlibvirtmod_qemu(void);
+#if PY_MAJOR_VERSION > 2
+# ifndef __CYGWIN__
+extern PyObject *PyInit_libvirtmod_qemu(void);
+# else
+extern PyObject *PyInit_cygvirtmod_qemu(void);
+# endif
 #else
+# ifndef __CYGWIN__
+extern void initlibvirtmod_qemu(void);
+# else
 extern void initcygvirtmod_qemu(void);
+# endif
 #endif
 
 #if 0
@@ -128,30 +136,59 @@ static PyMethodDef libvirtQemuMethods[] = {
     {NULL, NULL, 0, NULL}
 };
 
+#if PY_MAJOR_VERSION > 2
+static struct PyModuleDef moduledef = {
+        PyModuleDef_HEAD_INIT,
+# ifndef __CYGWIN__
+        "libvirtmod_qemu",
+# else
+        "cygvirtmod_qemu",
+# endif
+        NULL,
+        -1,
+        libvirtQemuMethods,
+        NULL,
+        NULL,
+        NULL,
+        NULL
+};
+
+PyObject *
+# ifndef __CYGWIN__
+PyInit_libvirtmod_qemu
+# else
+PyInit_cygvirtmod_qemu
+# endif
+  (void)
+{
+    PyObject *module;
+
+    if (virInitialize() < 0)
+        return NULL;
+
+    module = PyModule_Create(&moduledef);
+
+    return module;
+}
+#else /* ! PY_MAJOR_VERSION > 2 */
 void
-#ifndef __CYGWIN__
+# ifndef __CYGWIN__
 initlibvirtmod_qemu
-#else
+# else
 initcygvirtmod_qemu
-#endif
+# endif
   (void)
 {
-    static int initialized = 0;
-
-    if (initialized != 0)
-        return;
-
     if (virInitialize() < 0)
         return;
 
     /* initialize the python extension module */
     Py_InitModule((char *)
-#ifndef __CYGWIN__
-                  "libvirtmod_qemu"
-#else
-                  "cygvirtmod_qemu"
-#endif
-                  , libvirtQemuMethods);
-
-    initialized = 1;
+# ifndef __CYGWIN__
+                  "libvirtmod_qemu",
+# else
+                  "cygvirtmod_qemu",
+# endif
+		  libvirtQemuMethods);
 }
+#endif /* ! PY_MAJOR_VERSION > 2 */
-- 
1.8.3.1




More information about the libvir-list mailing list