Robert, your main() implementation needs more work IMHO. 1) For Py2, you need to if(PyErr_Occurred()) PyErr_WriteUnraisable(PyErr_Occurred()).
2) In Py3, you have to save the return of PyInit_<modname>, if NULL, report error (WriteUnraisable again?), if not, decref it. 3) In case of errors, process return value should match what core CPython returns en case of unhandled execptions. 3) However, a SystemExit exceptions should need some special consideration, as you should extract the process return value from it, as it could be 0 (zero). On Tue, Apr 21, 2009 at 6:16 AM, Robert Bradshaw <[email protected]> wrote: > 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 > -- Lisandro Dalcín --------------- Centro Internacional de Métodos Computacionales en Ingeniería (CIMEC) Instituto de Desarrollo Tecnológico para la Industria Química (INTEC) Consejo Nacional de Investigaciones Científicas y Técnicas (CONICET) PTLC - Güemes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 _______________________________________________ Cython-dev mailing list [email protected] http://codespeak.net/mailman/listinfo/cython-dev
