I am following this doc on extending c++ to python. As far as what was demonstrated there, the PyObject *spam_system(PyObject *self, PyObject *args) {...}
works fine when passing to array of PyModuleDef
but if I want to add more args to spam_system
, it results in an invaid conversation following
error: invalid conversion from ‘PyObject* (*)()’ {aka ‘_object* (*)()’} to ‘PyCFunction’ {aka ‘_object* (*)(_object*, _object*)’} [-fpermissive]
for more example, I want to define a method that takes no args, and returns a PyLong_FromLong
from long
/int
PyObject test() {return PyLong_FromLong(1);}
static PyMethodDef SpamMethods[] = {
{"system", spam_system, METH_VARARGS,
"Execute a shell command."},
{"test", test , METH_NOARGS,
//^ the error happens here
"test."},
{NULL, NULL, 0, NULL} /* Sentinel */
};
How can I define a method that takes no args or more than two args? (adding two args to test
works fine).
Since it's C++, which is more strict about function pointer casting than C, you'll have to cast to
(PyCFunction)
, or, IIRC,(void*)
also works.