Skip to content

Commit 644dbe8

Browse files
Andy Groverstratakis
authored andcommitted
python3: Handle differences in module initialization
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@redhat.com>
1 parent 027bb6a commit 644dbe8

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

python-ethtool/ethtool.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -899,19 +899,34 @@ static struct PyMethodDef PyEthModuleMethods[] = {
899899
{ .ml_name = NULL, },
900900
};
901901

902+
#if PY_MAJOR_VERSION >= 3
903+
#define MOD_ERROR_VAL NULL
904+
#define MOD_SUCCESS_VAL(val) val
905+
#define MOD_INIT(name) PyMODINIT_FUNC PyInit_##name(void)
906+
#define MOD_DEF(ob, name, doc, methods) \
907+
static struct PyModuleDef moduledef = { \
908+
PyModuleDef_HEAD_INIT, name, doc, -1, methods, }; \
909+
ob = PyModule_Create(&moduledef);
910+
#else
911+
#define MOD_ERROR_VAL
912+
#define MOD_SUCCESS_VAL(val)
913+
#define MOD_INIT(name) void init##name(void)
914+
#define MOD_DEF(ob, name, doc, methods) \
915+
ob = Py_InitModule3(name, methods, doc);
916+
#endif
902917

903-
PyMODINIT_FUNC initethtool(void)
918+
MOD_INIT(ethtool)
904919
{
905920
PyObject *m;
906-
m = Py_InitModule3("ethtool", PyEthModuleMethods, "Python ethtool module");
921+
MOD_DEF(m, "ethtool", "Python ethtool module", PyEthModuleMethods);
907922

908923
// Prepare the ethtool.etherinfo class
909924
if (PyType_Ready(&PyEtherInfo_Type) < 0)
910-
return;
925+
return MOD_ERROR_VAL;
911926

912927
// Prepare the ethtool IPv6 and IPv4 address types
913928
if (PyType_Ready(&ethtool_netlink_ip_address_Type))
914-
return;
929+
return MOD_ERROR_VAL;
915930

916931
// Setup constants
917932
PyModule_AddIntConstant(m, "IFF_UP", IFF_UP); /* Interface is up. */
@@ -933,6 +948,8 @@ PyMODINIT_FUNC initethtool(void)
933948
PyModule_AddIntConstant(m, "AF_INET", AF_INET); /* IPv4 interface */
934949
PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IPv6 interface */
935950
PyModule_AddStringConstant(m, "version", "python-ethtool v" VERSION);
951+
952+
return MOD_SUCCESS_VAL(m);
936953
}
937954

938955

0 commit comments

Comments
 (0)