From 41a78397e3a5263808c018963c6af9f812baef3b Mon Sep 17 00:00:00 2001 From: Blake Harnden Date: Sat, 4 May 2019 22:44:41 -0700 Subject: [PATCH] initial changes to support python2/3 building for netns --- netns/netnsmodule.c | 36 +++++++++++++++++++++++++++++++++--- netns/vcmdmodule.c | 40 +++++++++++++++++++++++++++++++--------- 2 files changed, 64 insertions(+), 12 deletions(-) diff --git a/netns/netnsmodule.c b/netns/netnsmodule.c index 93222322..f491af16 100644 --- a/netns/netnsmodule.c +++ b/netns/netnsmodule.c @@ -82,7 +82,11 @@ static PyObject *netns_nsexecvp(PyObject *self, PyObject *args) if (pid < 0) return PyErr_SetFromErrno(PyExc_OSError); else +#if PY_MAJOR_VERSION >= 3 + return PyLong_FromLong(pid); +#else return PyInt_FromLong(pid); +#endif } static PyObject *netns_nsfork(PyObject *self, PyObject *args) @@ -100,7 +104,11 @@ static PyObject *netns_nsfork(PyObject *self, PyObject *args) if (pid == 0) /* child */ PyOS_AfterFork(); +#if PY_MAJOR_VERSION >= 3 + return PyLong_FromLong(pid); +#else return PyInt_FromLong(pid); +#endif } static PyMethodDef netns_methods[] = { @@ -120,13 +128,35 @@ static PyMethodDef netns_methods[] = { {NULL, NULL, 0, NULL}, }; +#if PY_MAJOR_VERSION >= 3 +#define INITERROR return NULL + static struct PyModuleDef moduledef = { + PyModuleDef_HEAD_INIT, + "netns", /* m_name */ + "netns module", /* m_doc */ + -1, /* m_size */ + netns_methods, /* m_methods */ + NULL, /* m_reload */ + NULL, /* m_traverse */ + NULL, /* m_clear */ + NULL, /* m_free */ + }; +#else +#define INITERROR return +#endif + PyMODINIT_FUNC initnetns(void) { PyObject *m; - m = Py_InitModule("netns", netns_methods); + #if PY_MAJOR_VERSION >= 3 + m = PyModule_Create(&moduledef); + #else + m = Py_InitModule("netns", netns_methods); + #endif + if (m == NULL) - return; + INITERROR; #define MODADDINT(x) \ do { \ @@ -142,5 +172,5 @@ PyMODINIT_FUNC initnetns(void) #undef MODADDINT - return; + INITERROR; } diff --git a/netns/vcmdmodule.c b/netns/vcmdmodule.c index ed76bccd..7f0faada 100644 --- a/netns/vcmdmodule.c +++ b/netns/vcmdmodule.c @@ -236,7 +236,7 @@ static void VCmdWait_dealloc(VCmdWait *self) if (self->_vcmd != NULL) Py_DECREF(self->_vcmd); - self->ob_type->tp_free((PyObject *)self); + Py_TYPE(self)->tp_free((PyObject *)self); return; } @@ -363,7 +363,7 @@ static PyMethodDef VCmdWait_methods[] = { }; static PyTypeObject vcmd_VCmdWaitType = { - PyObject_HEAD_INIT(NULL) + PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "vcmd.VCmdWait", .tp_basicsize = sizeof(VCmdWait), .tp_dealloc = (destructor)VCmdWait_dealloc, @@ -491,7 +491,7 @@ static void VCmd_dealloc(VCmd *self) call_asyncfunc(async_delclientreq, &delclreq); } - self->ob_type->tp_free((PyObject *)self); + Py_TYPE(self)->tp_free((PyObject *)self); return; } @@ -902,7 +902,7 @@ static PyMethodDef VCmd_methods[] = { }; static PyTypeObject vcmd_VCmdType = { - PyObject_HEAD_INIT(NULL) + PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "vcmd.VCmd", .tp_basicsize = sizeof(VCmd), .tp_dealloc = (destructor)VCmd_dealloc, @@ -932,19 +932,41 @@ static PyMethodDef vcmd_methods[] = { {NULL, NULL, 0, NULL}, }; +#if PY_MAJOR_VERSION >= 3 +#define INITERROR return NULL + static struct PyModuleDef moduledef = { + PyModuleDef_HEAD_INIT, + "vcmd", /* m_name */ + "vcmd module that does stuff...", /* m_doc */ + -1, /* m_size */ + vcmd_methods, /* m_methods */ + NULL, /* m_reload */ + NULL, /* m_traverse */ + NULL, /* m_clear */ + NULL, /* m_free */ + }; +#else +#define INITERROR return +#endif + PyMODINIT_FUNC initvcmd(void) { PyObject *m; if (PyType_Ready(&vcmd_VCmdType) < 0) - return; + INITERROR; if (PyType_Ready(&vcmd_VCmdWaitType) < 0) - return; + INITERROR; - m = Py_InitModule3("vcmd", vcmd_methods, "vcmd module that does stuff..."); + #if PY_MAJOR_VERSION >= 3 + m = PyModule_Create(&moduledef); + #else + m = Py_InitModule3("vcmd", vcmd_methods, "vcmd module that does stuff..."); + #endif + if (!m) - return; + INITERROR; Py_INCREF(&vcmd_VCmdType); PyModule_AddObject(m, "VCmd", (PyObject *)&vcmd_VCmdType); @@ -952,5 +974,5 @@ PyMODINIT_FUNC initvcmd(void) Py_INCREF(&vcmd_VCmdWaitType); PyModule_AddObject(m, "VCmdWait", (PyObject *)&vcmd_VCmdWaitType); - return; + INITERROR; }