1

So i'm trying to make a python binding module in C, but confused on what is the naming rule of the module init function, which is declared by PyMODINIT_FUNC, i've seen init_foo, and initfoo, and someone else is using PyInit_foo, does the name matters? which one should I follow?

PyMODINIT_FUNC void init_foo(void) {...}
PyMODINIT_FUNC void initfoo(void) {...}
PyMODINIT_FUNC void PyInit_foo(void) {...}
fluter
  • 13,238
  • 8
  • 62
  • 100

1 Answers1

1

From the Extending Python with C or C++ docs(Python3),

The initialization function must be named PyInit_name(), where name is the name of the module, and should be the only non-static item defined in the module file:

PyMODINIT_FUNC
PyInit_spam(void)
{
    return PyModule_Create(&spammodule);
}

You can also find the same pattern all over the cpython source code(python3)

Whereas in Extending Python with C or C++ docs(Python2)

The initialization function must be named initname(), where name is the name of the module, and should be the only non-static item defined in the module file:

PyMODINIT_FUNC
initspam(void)
{
    (void) Py_InitModule("spam", SpamMethods);
}

So to answer your question the name matters. If you are writing an extension compatible to both python2 and python3 you have to implement the module init like this,

#if PY_MAJOR_VERSION < 3
    PyMODINIT_FUNC
    init_modulename(void)
    {
        return yourmoduleinit();
    }
#else
    PyMODINIT_FUNC
    PyInit_modulename(void)
    {
        return yourmoduleinit();
    }
#endif

Note: PyMODINIT_FUNC void init_foo(void) {...} this one is actually python2 version and the module name used to import this c-extension would be _fooConvention used to denote [internal c-extension]

Abdul Niyas P M
  • 18,035
  • 2
  • 25
  • 46