Forgetful Interpreter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bill Orcutt

    Forgetful Interpreter

    Having seen the number of lost souls asking questions about embedding
    Python in the archives of this group, I hesitate to add myself to
    their number, but I've hit a problem I can't quite get my head around.

    I have a simple C program that embeds a python interpreter to execute
    python commands and return stdout, and to a point everything works as
    intended. The problem that each command seems to be executed in its
    own context and knows nothing of what's come before. So if enter
    "print 2+2", I get back "4", but if I enter "a=2+2" then enter "print
    a", I get back the error, "a is undefined." My guess is that this has
    something to calling PyRun_SimpleStr ing inside of a child process, but
    I can't get much further than this.

    Any assistance is appreciated.

    thanks

    -Bill

    //call this once at the beginning to initialize
    void * nyptho_doinit(t _gogo* x, short flag)
    {
    Py_Initialize() ;
    char capture_stdout[] ="import sys,
    StringIO\n\sys. stderr=sys._cap ture=StringIO.S tringIO()\n";
    int rc = PyRun_SimpleStr ing(capture_std out);
    return x;
    }

    //this executes the command
    FILE * nyptho_exec(t_g ogo* x, char * cmd, short bflag, short fflag)
    {
    int pfd[2],i,z=0;
    pid_t pid;
    FILE *fp;
    extern int errno;

    if (pipe(pfd) < 0)
    return(NULL); // errno set by pipe()

    if ( (pid = fork()) < 0)
    return(NULL); // errno set by fork()

    else if (pid == 0) // child
    {
    close(pfd[0]);

    // close all descriptors in childpid[]
    if (x->childpid[0] > 0)
    close(0);

    PyRun_SimpleStr ing(cmd);

    //PyRun_SimpleStr ing("import sys");
    PyObject* sysmod = PyImport_Import Module("sys");
    PyObject* capobj =
    PyObject_GetAtt rString(sysmod, "_capture") ;
    PyObject* strres =
    PyObject_CallMe thod(capobj,"ge tvalue",0);
    char * dodo = PyString_AsStri ng(strres);
    strcpy(globalBu f,dodo);

    write(pfd[1], globalBuf, (long)strlen(gl obalBuf));
    globalBuf[0]='\0';

    _exit(127); //child exits
    }
    // parent
    close(pfd[1]);

    if ( (fp = fdopen(pfd[0], "r")) == NULL)
    return(NULL);

    x->childpid[fileno(fp)] = pid; // remember child pid for this
    fd
    return(fp);

    }
  • Dave Kuhlman

    #2
    Re: Forgetful Interpreter

    Bill Orcutt wrote:
    [color=blue]
    > Having seen the number of lost souls asking questions about
    > embedding Python in the archives of this group, I hesitate to add
    > myself to their number, but I've hit a problem I can't quite get
    > my head around.
    >
    > I have a simple C program that embeds a python interpreter to
    > execute python commands and return stdout, and to a point
    > everything works as intended. The problem that each command seems
    > to be executed in its own context and knows nothing of what's come
    > before. So if enter "print 2+2", I get back "4", but if I enter
    > "a=2+2" then enter "print a", I get back the error, "a is
    > undefined." My guess is that this has something to calling
    > PyRun_SimpleStr ing inside of a child process, but I can't get much
    > further than this.[/color]

    Take a look at Python-2.3.2/Demo/embed/demo.c in the Python source
    code distribution for clues. That program calls
    PyRun_SimpleStr ing() several times with the same context. If you
    do not switch interpreters (thread state, whatever) or
    re-initialize etc, then each call to PyRun_SimpleStr ing() should
    have the same global variables etc.

    Why are you calling fork()? Someone else will have to tell you
    the consequences of doing that.

    Dave

    [snip]

    --
    Dave Kuhlman

    dkuhlman@rexx.c om

    Comment

    • Bill Orcutt

      #3
      Calling PyRun_SimpleStr ing in a Child Process. [Was Forgetful Interpreter]

      Thanks for your reply. I agree that PyRun_SimpleStr ing should be
      running in the same context-- its worked that way for me in the past--
      The only thing that's different this time is fork() and that's what
      I'm hoping someone will tell me how to deal with, since I'm stuck with
      it this time.

      I'm updating the subject line to something more descriptive.

      thanks

      -Bill

      Dave Kuhlman <dkuhlman@rexx. com> wrote in message news:<bmprfj$pe m2q$1@ID-139865.news.uni-berlin.de>...[color=blue]
      > Bill Orcutt wrote:
      >[color=green]
      > > Having seen the number of lost souls asking questions about
      > > embedding Python in the archives of this group, I hesitate to add
      > > myself to their number, but I've hit a problem I can't quite get
      > > my head around.
      > >
      > > I have a simple C program that embeds a python interpreter to
      > > execute python commands and return stdout, and to a point
      > > everything works as intended. The problem that each command seems
      > > to be executed in its own context and knows nothing of what's come
      > > before. So if enter "print 2+2", I get back "4", but if I enter
      > > "a=2+2" then enter "print a", I get back the error, "a is
      > > undefined." My guess is that this has something to calling
      > > PyRun_SimpleStr ing inside of a child process, but I can't get much
      > > further than this.[/color]
      >
      > Take a look at Python-2.3.2/Demo/embed/demo.c in the Python source
      > code distribution for clues. That program calls
      > PyRun_SimpleStr ing() several times with the same context. If you
      > do not switch interpreters (thread state, whatever) or
      > re-initialize etc, then each call to PyRun_SimpleStr ing() should
      > have the same global variables etc.
      >
      > Why are you calling fork()? Someone else will have to tell you
      > the consequences of doing that.
      >
      > Dave
      >
      > [snip][/color]

      Comment

      • Jeff Epler

        #4
        Re: Forgetful Interpreter

        What you are doing won't work -- fork() behaves as though it makes a
        copy of all the data in the current process's address spaces, so changes
        in the child aren't mirrored in the parent.

        Example C program:

        #include <sys/types.h>
        #include <sys/wait.h>
        #include <unistd.h>
        #include <stdio.h>

        int i = 0;

        void increment_i_in_ forked_subproce ss() {
        pid_t pid = fork();
        if (pid == -1) {
        perror("fork"); exit(1);
        }
        if (pid == 0) {
        i++;
        printf("In subprocess %d, i=%d\n", getpid(), i);
        exit(0);
        }
        wait(NULL);
        printf("Subproc ess exited\n");
        }

        int main(void) {
        int j;
        for(j=0; j<10; j++) {
        increment_i_in_ forked_subproce ss();
        }
        return 0;
        }

        $ gcc orcutt.c && ./a.out
        In subprocess 16632, i=1
        Subprocess exited
        In subprocess 16633, i=1
        Subprocess exited
        In subprocess 16634, i=1
        Subprocess exited
        In subprocess 16635, i=1
        Subprocess exited
        In subprocess 16636, i=1
        Subprocess exited
        In subprocess 16637, i=1
        Subprocess exited
        In subprocess 16638, i=1
        Subprocess exited
        In subprocess 16639, i=1
        Subprocess exited
        In subprocess 16640, i=1
        Subprocess exited
        In subprocess 16641, i=1
        Subprocess exited


        Comment

        Working...