As described here:
http://python3porting.com/cextensions.html#module-initialization
Use macros to mask the differences in the return value type of the
module init function, and invocation of either Py_InitModule3 or
PyModule_Create.
Signed-off-by: Andy Grover <agrover(a)redhat.com>
---
python-ethtool/ethtool.c | 25 +++++++++++++++++++++----
1 file changed, 21 insertions(+), 4 deletions(-)
diff --git a/python-ethtool/ethtool.c b/python-ethtool/ethtool.c
index f2a5094..69e406b 100644
--- a/python-ethtool/ethtool.c
+++ b/python-ethtool/ethtool.c
@@ -899,19 +899,34 @@ static struct PyMethodDef PyEthModuleMethods[] = {
{ .ml_name = NULL, },
};
+#if PY_MAJOR_VERSION >= 3
+ #define MOD_ERROR_VAL NULL
+ #define MOD_SUCCESS_VAL(val) val
+ #define MOD_INIT(name) PyMODINIT_FUNC PyInit_##name(void)
+ #define MOD_DEF(ob, name, doc, methods) \
+ static struct PyModuleDef moduledef = { \
+ PyModuleDef_HEAD_INIT, name, doc, -1, methods, }; \
+ ob = PyModule_Create(&moduledef);
+#else
+ #define MOD_ERROR_VAL
+ #define MOD_SUCCESS_VAL(val)
+ #define MOD_INIT(name) void init##name(void)
+ #define MOD_DEF(ob, name, doc, methods) \
+ ob = Py_InitModule3(name, methods, doc);
+#endif
-PyMODINIT_FUNC initethtool(void)
+MOD_INIT(ethtool)
{
PyObject *m;
- m = Py_InitModule3("ethtool", PyEthModuleMethods, "Python ethtool
module");
+ MOD_DEF(m, "ethtool", "Python ethtool module", PyEthModuleMethods);
// Prepare the ethtool.etherinfo class
if (PyType_Ready(&PyEtherInfo_Type) < 0)
- return;
+ return MOD_ERROR_VAL;
// Prepare the ethtool IPv6 and IPv4 address types
if (PyType_Ready(ðtool_netlink_ip_address_Type))
- return;
+ return MOD_ERROR_VAL;
// Setup constants
PyModule_AddIntConstant(m, "IFF_UP", IFF_UP); /* Interface is up. */
@@ -933,6 +948,8 @@ PyMODINIT_FUNC initethtool(void)
PyModule_AddIntConstant(m, "AF_INET", AF_INET); /* IPv4
interface */
PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IPv6
interface */
PyModule_AddStringConstant(m, "version", "python-ethtool v"
VERSION);
+
+ return MOD_SUCCESS_VAL(m);
}
--
1.9.3