Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating Python modules in C for Python3 #7

Open
arunppsg opened this issue Feb 5, 2024 · 0 comments
Open

Updating Python modules in C for Python3 #7

arunppsg opened this issue Feb 5, 2024 · 0 comments

Comments

@arunppsg
Copy link

arunppsg commented Feb 5, 2024

The post on Python modules in C is one of the best introductory post which I have ever seen on that topic. It will be great if it could be updated to work with Python3. To make it work, we need to do the following changes:

  • Use PyModuleDef to define the module in _chi2.c
static PyModuleDef chi2_module = {
    PyModuleDef_HEAD_INIT,
    .m_name = "_chi2",
    .m_doc = module_docstring,
    .m_size = -1,
    .m_methods = module_methods,
};
  • Use PyInit__chi2 instead of init_chi2, update the function to return NULL when the module does not get initialized and PyModule_Create instead of Py_InitModule3. The updated version:
PyMODINIT_FUNC PyInit__chi2(void)
{ 
  PyObject *m = PyModule_Create(&chi2_module);
  if (m == NULL) {
    return NULL; 
  }
  
  /* Load numpy functionality */
  import_array();

  return m;
}

Thank you for the wonderful post.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant