capi.py
import myModule as capi
print(capi.Message("Hello, Japan"))
Execute
capi>python capi.py
Hello, Japan
Hello, Japan
capilib.c
#include <Python.h>
static PyObject* Message(PyObject* self, PyObject* args){
char* str;
if (!PyArg_ParseTuple(args, "s",&str)){
return NULL;
}
printf("%s\n",str);
return Py_BuildValue("s", str);
}
// Function Definition struct
static PyMethodDef myMethods[] = {
{ "Message", Message, METH_VARARGS, "Prints Message"},
{ NULL }
};
static struct PyModuleDef myModule = {
PyModuleDef_HEAD_INIT,"myModule","C API Module",-1,myMethods
};
PyMODINIT_FUNC PyInit_myModule(void){
return PyModule_Create(&myModule);
}
** Attention point **
char* str;
//Capture arguments
if (!PyArg_ParseTuple(args, "s",&str)){
return NULL;
}
//Creating a return value
return Py_BuildValue("s", str);
--PyArg_ParseTuple (args, "s", & str) reads the argument "s" string into str. --The return value "s" represents a string. -Example https://docs.python.org/2.0/ext/buildValue.html
Recommended Posts