Extended functions in embedded code

Ervin Hegedüs airween at gmail.com
Tue Oct 13 07:36:19 EDT 2015


Hello there,

I'm interesting for the embeding of Python code - the examples and docs are
very helpfully. The main code, which embeds the Python interpreter, had
written in C. There are several functions, what I have to use in embedded
(Python) code, so I must to write them as Python extension.

That's no problem - I found a solution for that, I don't need to made (and
I don't _want_) a separated .so file (a new Python module): the extension
in same C source, where the embedded code exists, like this:

#include <stdio.h>
#include <Python.h>

/* python mymodule */
static PyObject*
mymodule_usleep(PyObject *self, PyObject *args)
{
...
}

...
static PyMethodDef mymodule_methods[] = {
    {"usleep",    mymodule_usleep,    METH_VARARGS,    mymodule_usleep_doc},
    {NULL, NULL}
};

PyMODINIT_FUNC
initmymodule(void)
{
    (void) Py_InitModule("mymodule", mymodule_methods);
}

/* python mymodule */


/* python embedding */
void foo() {
    Py_Initialize();
    initmymodule();
    ....
    Py_Finalize();
}

/* python embedding */


Then I have a Python file, which the code above calls:

import mymodule

def bar(d)
    do_something()
    mymodule.usleep(10000)
    return something


Just my "2 cents" question: is there any way to make the extension without
"import" keyword? Or is there a way to leave that?


Thanks,


a.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20151013/32e0fa19/attachment.html>


More information about the Python-list mailing list