|
#include <stdio.h> |
|
#include <Python.h> |
|
|
|
// Module method definitions |
|
static PyObject* hello_world(PyObject *self, PyObject *args) { |
|
printf("Hello, world!\n"); |
|
Py_RETURN_NONE; |
|
} |
|
|
|
static PyObject* hello(PyObject *self, PyObject *args) { |
|
const char* name; |
|
if (!PyArg_ParseTuple(args, "s", &name)) { |
|
return NULL; |
|
} |
|
|
|
printf("Hello, %s!\n", name); |
|
Py_RETURN_NONE; |
|
} |
|
|
|
// Method definition object for this extension, these argumens mean: |
|
// ml_name: The name of the method |
|
// ml_meth: Function pointer to the method implementation |
|
// ml_flags: Flags indicating special features of this method, such as |
|
// accepting arguments, accepting keyword arguments, being a |
|
// class method, or being a static method of a class. |
|
// ml_doc: Contents of this method's docstring |
|
static PyMethodDef hello_methods[] = { |
|
{ |
|
"hello_world", hello_world, METH_NOARGS, |
|
"Print 'hello world' from a method defined in a C extension." |
|
}, |
|
{ |
|
"hello", hello, METH_VARARGS, |
|
"Print 'hello xxx' from a method defined in a C extension." |
|
}, |
|
{NULL, NULL, 0, NULL} |
|
}; |
|
|
|
// Module definition |
|
// The arguments of this structure tell Python what to call your extension, |
|
// what it's methods are and where to look for it's method definitions |
|
static struct PyModuleDef hello_definition = { |
|
PyModuleDef_HEAD_INIT, |
|
"hello", |
|
"A Python module that prints 'hello world' from C code.", |
|
-1, |
|
hello_methods |
|
}; |
|
|
|
// Module initialization |
|
// Python calls this function when importing your extension. It is important |
|
// that this function is named PyInit_[[your_module_name]] exactly, and matches |
|
// the name keyword argument in setup.py's setup() call. |
|
PyMODINIT_FUNC PyInit_hello(void) { |
|
Py_Initialize(); |
|
return PyModule_Create(&hello_definition); |
|
} |
Just for the records, I solved it:
#include <stdio.h> #include <Python.h> const char* pystart = "print('Start')\n\ import eumat\n\ print(dir(eumat))\n\ print(eumat.test())"; static PyObject* ftest (PyObject* self, PyObject* args) { return PyLong_FromLong(1000); } static PyMethodDef EmbMethods[] = { { "test", (PyCFunction)ftest, METH_NOARGS,"Test Function" }, {NULL, NULL, 0, NULL} }; int main() { printf("Test Programm for Python 3.8\n"); Py_Initialize(); static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, "eumat", PyDoc_STR("EMT Module"), -1, EmbMethods }; PyObject* eumat = PyImport_AddModule("eumat"); //create main module if (!eumat) { printf("Could not add the module eumat in Python."); return 0; } PyModule_AddFunctions(eumat, EmbMethods); if (PyRun_SimpleString(pystart) == -1) { printf("Could not run pystart."); return 0; } return 1; }