On Apr 21, 2009, at 7:31 PM, Lisandro Dalcin wrote:

> On Tue, Apr 21, 2009 at 3:33 PM, Robert Bradshaw
> <[email protected]> wrote:
>> On Apr 21, 2009, at 6:17 AM, Lisandro Dalcin wrote:
>>
>>> Robert, your main() implementation needs more work IMHO.
>>
>> Thanks for the feedback. I just did the basic embedding, as on David
>> McNab's site.
>
>>>> import this
> ...
> Now is better than never.
> Although never is often better than *right* now.
> ...
>
> Or perhaps Cython should have its own Zen ;-) ???

:-) I was actually just working on this *right* now.

>
>> It would be good to have it behave more like python -m
>> http://trac.cython.org/cython_trac/ticket/291
>
> Mmm... I´m not sure python -m will be equivalent to your embeding...
> Py_Main() does a lot of things other thanInitialize() && SetArgv(), I
> do not remember right now, but IIRC some environ vars are inspected in
> Py_Main()

Hmm... I'd like to see a list of what happens before we decide to opt  
for python -m semantics or simplicity. I'm going to leave the ticket  
open for now.

>
>>
>>> 1) For Py2, you need to if(PyErr_Occurred())
>>> PyErr_WriteUnraisable(PyErr_Occurred()).
>>
>> Actually, it might be nice to dump the whole traceback here.
>
> OK, of course... you want the equivalent to "import traceback;
> traceback.print_exc()". I do not remember right now if there is a
> direct C-API call for this.

There's a C-API call for this.

>>> 2) In Py3, you have to save the return of PyInit_<modname>, if NULL,
>>> report error (WriteUnraisable again?), if not, decref it.
>>
>> Is PyErr_Occurred not a good enough check here? Yes, I should
>> probably get and decref the module (though it won't matter much as I
>> call Py_Finalize next.
>
> Well, I would ask to maintain Cython-generated code valgrind-friendly
> if possible :-)

Me too. Done.

>
>>> 3) In case of errors, process return value should match what core
>>> CPython returns en case of unhandled execptions.
>>
>> Do you know of a good list? (Otherwise, I'll just try some.)
>>
>
> Do not remember right now, but I rememberd "-1" as a hard-wired return
> value in core CPython (likely in pythonrun.c).

I'm getting 1 unless otherwise specified. I've seen -1 hard coded,  
but never seen it come out (both on mac and linux).

>
>>> 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).
>>
>> Good point.
>>
>
> In short, I would check for SystemExit and honor its return code, or
> just return "-1"

Interestingly enough, the C-API call PyErr_PrintEx does this for you.  
(Yeah, I might call it a bit hackish, but that's what Python seems to  
use).

- Robert


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

Reply via email to