I am transitioning to removing the boost-python dependencies in my code, and I have made to to the "final step" of this transition (I removed all other boost dependencies, and when I comment out the boost code below I get ImportError: dynamic module does not define init function (initMy_module
).
Here is the code as it stands
#include <boost/python.hpp>
namespace bpn = boost::python::numeric;
using namespace boost::python;
BOOST_PYTHON_MODULE(My_module)
{
using namespace bpn;
import_array();
array::set_module_and_type("numpy", "ndarray");
register_exception_translator<int>(&translator);
def("run_My_module", run_My_module, "run my moduule");
def("run_My_module_another_way", run_My_module_another_way, "run my module another way");
}
From my understanding of the python/C API, I believe that the following code should superficially link my C and Python code.
static PyMethodDef myModule_methods[] = {
{"run_My_module", run_My_module, METH_VARARGS},
{"run_My_module_another_way", run_My_module_another_way, METH_VARARGS},
{NULL, NULL}
};
void initmyModule(void)
{
// Init module.
(void) Py_InitModule("myModule", myModule_methods);
}
However, this produces the following errors:
myModule_python_binding.cpp:126:5: error: invalid conversion from 'int (*)(char*, char*, char*, PyObject*) {aka int (*)(char*, char*, char*, _object*)}' to 'PyCFunction {aka _object* (*)(_object*, _object*)}' [-fpermissive]
};
^
myModule_python_binding.cpp:126:5: error: invalid conversion from 'int (*)(char*, ompi_communicator_t*&, char*, char*, PyObject*) {aka int (*)(char*, ompi_communicator_t*&, char*, char*, _object*)}' to 'PyCFunction {aka _object* (*)(_object*, _object*)}' [-fpermissive]
both functions are of this form. They both return integers indicating their level of success.
int run_myModule(char *infile, char *outfile, char *errfile, pyObject *exc)
{...};
int run_myModule_another_way(char *infile, int &_exvar, char *outfile, char *errfile, pyObject *exc)
{...};
Why is my "superficial" connection failing?
You are getting the compile time error because the function pointers that you are using in
myModule_methods[]
are of the wrong type/signature. The functions that you want to call directly from Python need to have the following signature:So if you want to call your
run_my_Module
andrun_my_Module_another_way
functions from Python, you need to create the necessary "glue" between your functions and Python by wrapping them in a function that has the above signature. For example:See Parsing Arguments for more info on parsing arguments passed in from Python. Plus Extending and Embedding the Python Interpreter for all of the general info you need.