Extended functions in embedded code
Ervin Hegedüs
airween at gmail.com
Thu Oct 15 11:40:37 EDT 2015
Hi,
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:
https://code.activestate.com/recipes/579110-add-function-to-__builtin__-module-through-c-api/
Hope this helps, and many thanks for all help.
Cheers,
a.
ps: after I've done, I realized, that will not good for me :). Nevermind,
this was a funny work.
On Tue, Oct 13, 2015 at 1:36 PM, Ervin Hegedüs <airween at gmail.com> wrote:
> 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/20151015/c5e9c495/attachment.html>
More information about the Python-list
mailing list