Mark Lodato wrote:
> you could embed the interpreter and statically link your module.
> [...]
> ----------------------------------- main.c ---------------------------
> #include <Python.h>
> 
> // For each Cython module you want to embed, you must declare an
> // init<module> function, like so:
> PyMODINIT_FUNC initmylib(void);
> 
> int
> main(int argc, char *argv[])
> {
>     // The first step is to set up the Python interpreter:
>     Py_Initialize();
>     PySys_SetArgv(argc, argv);
> 
>     // Next, we need to tell Python that our module exists.  Call each
>     // of the functions you declared above.
>     initmylib();
> 
>     // Now do some Python stuff.  The easiest thing to do is to give
>     // the interpreter a string of Python code that imports your
>     // module and calls it.
>     PyRun_SimpleString("from mylib import main\n"
>                        "main()\n");
> 
>     // When we're done, tell Python to clean up.
>     Py_Finalize();
>     return 0;
> }
> ----------------------------------------------------------------------

I wonder if it makes sense to add support for this to Cython. You could
have a command line option that adds a suitable main() function that runs
the module code in an embedded CPython interpreter (and that does the setup
correctly for Py2 and Py3).

Or maybe an additional tool like Python's "freeze" (what about naming it
"cool" or "maincyfy"? Or maybe "(em)bedcy"?) could take a list of Cython
modules and generate a main .c file like the one above, which would simply
run the module initialisation in order. That would make it trivial to write
a 'main module' (passed as last argument) that simply contains the main
program code in its body.

We might even be able to check module interdependencies here, so that the
tool can bail out if it knows that the sequential module initialisations
cannot work in an embedded setup.

Tons of cool stuff to do here ...

Stefan
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to