xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: "Marek Marczykowski-Górecki" <marmarek@invisiblethingslab.com>
To: xen-devel@lists.xen.org
Cc: "Wei Liu" <wei.liu2@citrix.com>,
	"Ian Jackson" <ian.jackson@eu.citrix.com>,
	"Marek Marczykowski-Górecki" <marmarek@invisiblethingslab.com>
Subject: [PATCH 7/8] python: adjust module initalization for Python3
Date: Thu, 23 Feb 2017 11:48:27 +0100	[thread overview]
Message-ID: <1487846908-21462-8-git-send-email-marmarek@invisiblethingslab.com> (raw)
In-Reply-To: <1487846908-21462-1-git-send-email-marmarek@invisiblethingslab.com>

In Python3, PyTypeObject looks slightly different, and also module
initialization is different. Instead of Py_InitModule, PyModule_Create
should be called on already defined PyModuleDef structure. And then
initialization function should return that module.

Additionally initialization function should be named PyInit_<name>,
instead of init<name>.

Signed-off-by: Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
---
 tools/python/xen/lowlevel/xc/xc.c | 35 ++++++++++++++++++++++++++++++++---
 tools/python/xen/lowlevel/xs/xs.c | 34 +++++++++++++++++++++++++++++++---
 2 files changed, 63 insertions(+), 6 deletions(-)

diff --git a/tools/python/xen/lowlevel/xc/xc.c b/tools/python/xen/lowlevel/xc/xc.c
index df31a1d..d09c45f 100644
--- a/tools/python/xen/lowlevel/xc/xc.c
+++ b/tools/python/xen/lowlevel/xc/xc.c
@@ -2685,7 +2685,11 @@ static void PyXc_dealloc(XcObject *self)
 }
 
 static PyTypeObject PyXcType = {
+#if PY_MAJOR_VERSION >= 3
+    .ob_base = { PyObject_HEAD_INIT(NULL) },
+#else
     PyObject_HEAD_INIT(NULL)
+#endif
     .tp_name = PKG "." CLS,
     .tp_basicsize = sizeof(XcObject),
     .tp_itemsize = 0,
@@ -2699,22 +2703,44 @@ static PyTypeObject PyXcType = {
 
 static PyMethodDef xc_methods[] = { { NULL } };
 
+
+#if PY_MAJOR_VERSION >= 3
+static PyModuleDef xc_module = {
+    PyModuleDef_HEAD_INIT,
+    PKG,     /* name */
+    NULL,   /* docstring */
+    -1,     /* size of per-interpreter state, -1 means the module use global
+               variables */
+    xc_methods
+};
+#endif
+
+#if PY_MAJOR_VERSION >= 3
+#define INITERROR return NULL
+PyMODINIT_FUNC PyInit_xc(void)
+#else
+#define INITERROR return
 PyMODINIT_FUNC initxc(void)
+#endif
 {
     PyObject *m;
 
     if (PyType_Ready(&PyXcType) < 0)
-        return;
+        INITERROR;
 
+#if PY_MAJOR_VERSION >= 3
+    m = PyModule_Create(&xc_module);
+#else
     m = Py_InitModule(PKG, xc_methods);
+#endif
 
     if (m == NULL)
-      return;
+        INITERROR;
 
     xc_error_obj = PyErr_NewException(PKG ".Error", PyExc_RuntimeError, NULL);
     if (xc_error_obj == NULL) {
         Py_DECREF(m);
-        return;
+        INITERROR;
     }
     zero = PyLongOrInt_FromLong(0);
 
@@ -2732,6 +2758,9 @@ PyMODINIT_FUNC initxc(void)
     PyModule_AddIntConstant(m, "XEN_SCHEDULER_CREDIT", XEN_SCHEDULER_CREDIT);
     PyModule_AddIntConstant(m, "XEN_SCHEDULER_CREDIT2", XEN_SCHEDULER_CREDIT2);
 
+#if PY_MAJOR_VERSION >= 3
+    return m;
+#endif
 }
 
 
diff --git a/tools/python/xen/lowlevel/xs/xs.c b/tools/python/xen/lowlevel/xs/xs.c
index b37daa9..aba5a20 100644
--- a/tools/python/xen/lowlevel/xs/xs.c
+++ b/tools/python/xen/lowlevel/xs/xs.c
@@ -946,7 +946,11 @@ static void xshandle_dealloc(XsHandle *self)
 }
 
 static PyTypeObject xshandle_type = {
+#if PY_MAJOR_VERSION >= 3
+    .ob_base = { PyObject_HEAD_INIT(NULL) },
+#else
     PyObject_HEAD_INIT(NULL)
+#endif
     .tp_name = PKG "." CLS,
     .tp_basicsize = sizeof(XsHandle),
     .tp_itemsize = 0,
@@ -960,22 +964,43 @@ static PyTypeObject xshandle_type = {
 
 static PyMethodDef xs_methods[] = { { NULL } };
 
+#if PY_MAJOR_VERSION >= 3
+static PyModuleDef xs_module = {
+    PyModuleDef_HEAD_INIT,
+    PKG,     /* name */
+    NULL,   /* docstring */
+    -1,     /* size of per-interpreter state, -1 means the module use global
+               variables */
+    xs_methods
+};
+#endif
+
+#if PY_MAJOR_VERSION >= 3
+#define INITERROR return NULL
+PyMODINIT_FUNC PyInit_xs(void)
+#else
+#define INITERROR return
 PyMODINIT_FUNC initxs(void)
+#endif
 {
     PyObject* m;
 
     if (PyType_Ready(&xshandle_type) < 0)
-        return;
+        INITERROR;
 
+#if PY_MAJOR_VERSION >= 3
+    m = PyModule_Create(&xs_module);
+#else
     m = Py_InitModule(PKG, xs_methods);
+#endif
 
     if (m == NULL)
-      return;
+        INITERROR;
 
     xs_error = PyErr_NewException(PKG ".Error", PyExc_RuntimeError, NULL);
     if (xs_error == NULL) {
         Py_DECREF(m);
-        return;
+        INITERROR;
     }
 
     Py_INCREF(&xshandle_type);
@@ -983,6 +1008,9 @@ PyMODINIT_FUNC initxs(void)
 
     Py_INCREF(xs_error);
     PyModule_AddObject(m, "Error", xs_error);
+#if PY_MAJOR_VERSION >= 3
+    return m;
+#endif
 }
 
 
-- 
2.7.4


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel

  parent reply	other threads:[~2017-02-23 10:48 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-23 10:48 [PATCH 0/8] Python 3 bindings Marek Marczykowski-Górecki
2017-02-23 10:48 ` [PATCH 1/8] python: check return value of PyErr_NewException Marek Marczykowski-Górecki
2017-02-23 10:48 ` [PATCH 2/8] python: drop tp_getattr implementation Marek Marczykowski-Górecki
2017-02-23 10:48 ` [PATCH 3/8] python: use Py_TYPE instead of looking directly into PyObject_HEAD Marek Marczykowski-Górecki
2017-02-23 10:48 ` [PATCH 4/8] python: initialize specific fields of PyTypeObject Marek Marczykowski-Górecki
2017-02-23 10:48 ` [PATCH 5/8] python: use PyBytes/PyUnicode instead of PyString Marek Marczykowski-Górecki
2017-02-23 10:48 ` [PATCH 6/8] python: use PyLong_* for constructing 'int' type in Python3 Marek Marczykowski-Górecki
2017-02-23 10:48 ` Marek Marczykowski-Górecki [this message]
2017-02-23 10:48 ` [PATCH 8/8] python: handle long type in scripts Marek Marczykowski-Górecki
2017-02-23 11:23 ` [PATCH 0/8] Python 3 bindings Wei Liu

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=1487846908-21462-8-git-send-email-marmarek@invisiblethingslab.com \
    --to=marmarek@invisiblethingslab.com \
    --cc=ian.jackson@eu.citrix.com \
    --cc=wei.liu2@citrix.com \
    --cc=xen-devel@lists.xen.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;
as well as URLs for NNTP newsgroup(s).