Added a fflush(stdout) after each printf and, as I expected....still only the first 2 prints.
On Fri, Mar 25, 2011 at 1:47 PM, MRAB <pyt...@mrabarnett.plus.com> wrote: > On 25/03/2011 17:37, Eric Frederich wrote: >> >> So.... I found that if I type ctrl-d then the other lines will print. >> >> It must be a bug then that the exit() function doesn't do the same thing. >> The documentation says "The return value will be the integer passed to >> the sys.exit() function" but clearly nothing is returned since the >> call to Py_Main exits rather than returning (even when calling >> sys.exit instead of just exit). >> >> In the mean time is there a way to redefine the exit function in >> Python to do the same behavior as "ctrl-d?" >> I realize that in doing that (if its even possible) still won't >> provide a way to pass a value back from the interpreter via sys.exit. >> > You could flush stdout after each print or turn off buffering on stdout > with: > > setvbuf(stdout, NULL, _IONBF, 0); > >> Thanks, >> ~Eric >> >> >> >> On Fri, Mar 25, 2011 at 12:02 PM, Eric Frederich >> <eric.freder...@gmail.com> wrote: >>> >>> I am able to embed the interactive Python interpreter in my C program >>> except that when the interpreter exits, my entire program exits. >>> >>> #include<stdio.h> >>> #include<Python.h> >>> >>> int main(int argc, char *argv[]){ >>> printf("line %d\n", __LINE__); >>> Py_Initialize(); >>> printf("line %d\n", __LINE__); >>> Py_Main(argc, argv); >>> printf("line %d\n", __LINE__); >>> Py_Finalize(); >>> printf("line %d\n", __LINE__); >>> return 0; >>> } >>> >>> When I run the resulting binary I get the following.... >>> >>> $ ./embedded_python >>> line 5 >>> line 7 >>> Python 2.7.1 (r271:86832, Mar 25 2011, 11:56:07) >>> [GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2 >>> Type "help", "copyright", "credits" or "license" for more information. >>>>>> >>>>>> print 'hi' >>> >>> hi >>>>>> >>>>>> exit() >>> >>> >>> I never see line 9 or 11 printed. >>> I need to embed python in an application that needs to do some cleanup >>> at the end so I need that code to execute. >>> What am I doing wrong? >>> >>> Is there something else I should call besides "exit()" from within the >>> interpreter? >>> Is there something other than Py_Main that I should be calling? >>> >>> Thanks, >>> ~Eric >>> > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list