embedding interactive python interpreter

E

Eric Frederich

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.

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
 
E

Eric Frederich

I'm not sure that I know how to run this function in such a way that
it gives me an interactive session.
I passed in stdin as the first parameter and NULL as the second and
I'd get seg faults when running exit() or even imnport sys.

I don't want to pass a file. I want to run some C code, start an
interactive session, then run some more C code once the session is
over, but I cannot find a way to start an interactive Python session
within C that won't exit pre-maturely before I have a chance to run my
cleanup code in C.
 
M

Mark Hammond

I'm not sure that I know how to run this function in such a way that
it gives me an interactive session.
I passed in stdin as the first parameter and NULL as the second and
I'd get seg faults when running exit() or even imnport sys.

I don't want to pass a file. I want to run some C code, start an
interactive session, then run some more C code once the session is
over, but I cannot find a way to start an interactive Python session
within C that won't exit pre-maturely before I have a chance to run my
cleanup code in C.

Instead of calling Py_Main, arrange for the following code to be executed:

import code
try:
code.interact()
except SystemExit:
pass
print "Done!"

If you save that as a script and run it, you will see that when you call
quit() or use Ctrl+D, the "Done!" is printed (so the exception is
caught) and things will return normally (albeit without any return code
that may have been specified in the SystemExit exception). If you
arrange to call that code in your app (either by importing it as a
module of even by calling PyRun_SimpleString) things should work as you
need.

HTH,

Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top