From b32a0b73dcfaceaa63a5a418e4b7766ad399b35b Mon Sep 17 00:00:00 2001 From: Michael Boyle Date: Thu, 20 Sep 2018 13:38:53 -0400 Subject: [PATCH] make cos_module_np 2/3 compatible --- .../numpy_c_api/cos_module_np.c | 37 ++++++++++++++++--- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/advanced/interfacing_with_c/numpy_c_api/cos_module_np.c b/advanced/interfacing_with_c/numpy_c_api/cos_module_np.c index b8bfaf25e..51c2a74bb 100644 --- a/advanced/interfacing_with_c/numpy_c_api/cos_module_np.c +++ b/advanced/interfacing_with_c/numpy_c_api/cos_module_np.c @@ -72,11 +72,36 @@ static PyMethodDef CosMethods[] = {NULL, NULL, 0, NULL} }; + +#if PY_MAJOR_VERSION >= 3 /* module initialization */ -PyMODINIT_FUNC -initcos_module_np(void) -{ - (void) Py_InitModule("cos_module_np", CosMethods); - /* IMPORTANT: this must be called */ - import_array(); +/* Python version 3*/ +static struct PyModuleDef cModPyDem = { + PyModuleDef_HEAD_INIT, + "cos_module", "Some documentation", + -1, + CosMethods +}; +PyMODINIT_FUNC PyInit_cos_module_np(void) { + PyObject *module; + module = PyModule_Create(&cModPyDem); + if(module==NULL) return NULL; + /* IMPORTANT: this must be called */ + import_array(); + if (PyErr_Occurred()) return NULL; + return module; } + +#else +/* module initialization */ +/* Python version 2 */ +PyMODINIT_FUNC initcos_module_np(void) { + PyObject *module; + module = Py_InitModule("cos_module_np", CosMethods); + if(module==NULL) return; + /* IMPORTANT: this must be called */ + import_array(); + return; +} + +#endif