On Apr 20, 2009, at 11:48 AM, Robert Bradshaw wrote:
> On Apr 19, 2009, at 7:46 AM, Stefan Behnel wrote:
>
>> 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).
>
> +1, I think that's an excellent idea. http://trac.cython.org/
> cython_trac/ticket/289
There's an --embed command line option now, which generates an
appropriate main() method. One then compiles the resulting C file
into an executable (which still needs to be linked against the Python
library).
- Robert
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev