embedding interactive python interpreter

Discussion in 'Python' started by Eric Frederich, Mar 25, 2011.

  1. 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
     
    Eric Frederich, Mar 25, 2011
    #1
    1. Advertising

  2. 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.

    On Sun, Mar 27, 2011 at 5:59 PM, eryksun () <> wrote:
    > On Friday, March 25, 2011 12:02:16 PM UTC-4, Eric Frederich wrote:
    >>
    >> 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?

    >
    > Does PyRun_InteractiveLoop also have this problem?
     
    Eric Frederich, Mar 28, 2011
    #2
    1. Advertising

  3. Eric Frederich

    Mark Hammond Guest

    On 28/03/2011 2:06 PM, Eric Frederich wrote:
    > 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
     
    Mark Hammond, Mar 29, 2011
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gabriel Cooper
    Replies:
    1
    Views:
    1,031
    vincent wehren
    Apr 27, 2004
  2. Simon Eves
    Replies:
    0
    Views:
    353
    Simon Eves
    Dec 16, 2006
  3. ThomasC
    Replies:
    6
    Views:
    361
  4. Ryan K
    Replies:
    0
    Views:
    302
    Ryan K
    Jun 5, 2007
  5. Eric Frederich
    Replies:
    0
    Views:
    119
    Eric Frederich
    Sep 11, 2013
Loading...

Share This Page