capi.py
import myModule as capi
print(capi.Message("Hallo Japan"))
Lauf
capi>python capi.py
Hallo Japan
Hallo 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);
}
** Aufmerksamkeitspunkt **
char* str;
//Argumente erfassen
if (!PyArg_ParseTuple(args, "s",&str)){
return NULL;
}
//Rückgabewert erstellen
return Py_BuildValue("s", str);
--PyArg_ParseTuple (args, "s", & str) liest die Argumentzeichenfolge "s" in str.
Recommended Posts