Forgetful Interpreter

Dave Kuhlman dkuhlman at rexx.com
Fri Oct 17 18:46:10 EDT 2003


Bill Orcutt wrote:

> 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_SimpleString inside of a child process, but I can't get much
> further than this.

Take a look at Python-2.3.2/Demo/embed/demo.c in the Python source
code distribution for clues.  That program calls
PyRun_SimpleString() several times with the same context.  If you
do not switch interpreters (thread state, whatever) or
re-initialize etc, then each call to PyRun_SimpleString() 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
http://www.rexx.com/~dkuhlman
dkuhlman at rexx.com




More information about the Python-list mailing list