<div dir="ltr"><div><div><div><div><div>Hi,<br><br></div>I've read many docs and examples, then I made a usable test version. If anybody interested about this (and for the mailing list archives), then it could be found here:<br><br><a href="https://code.activestate.com/recipes/579110-add-function-to-__builtin__-module-through-c-api/">https://code.activestate.com/recipes/579110-add-function-to-__builtin__-module-through-c-api/</a><br><br></div>Hope this helps, and many thanks for all help.<br><br></div>Cheers,<br><br></div>a.<br><br></div>ps: after I've done, I realized, that will not good for me :). Nevermind, this was a funny work.<br><br><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Oct 13, 2015 at 1:36 PM, Ervin Hegedüs <span dir="ltr"><<a href="mailto:airween@gmail.com" target="_blank">airween@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div><div><div>Hello there,<br><br></div>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.<br><br></div>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:<br><br>#include <stdio.h><br>#include <Python.h><br><br>/* python mymodule */<br>static PyObject*<br>mymodule_usleep(PyObject *self, PyObject *args)<br>{<br>...<br>}<br><br>...<br>static PyMethodDef mymodule_methods[] = {<br>    {"usleep",    mymodule_usleep,    METH_VARARGS,    mymodule_usleep_doc},<br>    {NULL, NULL}<br>};<br><br>PyMODINIT_FUNC<br>initmymodule(void)<br>{<br>    (void) Py_InitModule("mymodule", mymodule_methods);<br>}<br><br></div>/* python mymodule */<br><br><br></div>/* python embedding */<br>void foo() {<br>    Py_Initialize();<br></div>    initmymodule();<br><div>    ....<br>    Py_Finalize();<br>}<br><br>/* python embedding */<br><div><div><br><br></div><div>Then I have a Python file, which the code above calls:<br><br></div><div>import mymodule<br><br></div><div>def bar(d)<br></div><div>    do_something()<br></div><div>    mymodule.usleep(10000)<br></div><div>    return something<br><br><br></div><div>Just my "2 cents" question: is there any way to make the extension without "import" keyword? Or is there a way to leave that?<br><br><br></div><div>Thanks,<br><br><br></div><div>a.<br></div></div></div></div>
</blockquote></div><br></div>