how to get the output of embedded python?

David Bolen db3l at fitlinxx.com
Tue Feb 13 13:28:33 EST 2001


Jean-Luc Fontaine <jfontain at winealley.com> writes:

> With python, according to my research on the subject (which may be wrong 
> since I am new in python), there does not seem to be any simple way to 
> gather the output of a script passed for example to PyRun_SimpleString. I 
> also looked at the huge piece of code that handles the interactive part of 
> python so that typing 1+1 at the prompt prints 2.

You may need to be a little more clear on what you mean by "output" -
is it anything the script generates, or are you looking for
information that is otherwise sent to sys.stdout?  The former may be
impossible, given that scripts may write files or do various
operations.  The latter isn't too bad, since you can just redirect
sys.stdout before executing the code, unless you need a highly secure
or guaranteed environment.

I took a fairly low tech approach in one of my embedded applications.
First, I just implemented some functions that expected to be called
with any data being generated by the script.  In my case the
application was a simulator and script output was just placed into a
main scrolling log window (highlighted if it was to stderr).  Then,
after instantiating an interpreter (and you could do this for multiple
interpreters although you'd need to separate their output in your
embedded processing function), I first forced the execution of an
initialization script that rebound sys.stdout/stderr to my code, e.g.:

    import sys

    class StdoutCatcher:
	def write(self, stuff):
	    ecna.stdout(stuff)

    class StderrCatcher:
	def write(self, stuff):
	    ecna.stderr(stuff)

    sys.stdout = StdoutCatcher()
    sys.stderr = StderrCatcher()

(in the above, the "ecna." functions are those already registered by
the embedding application)

And after that, any scripts that I executed within that same
interpreter would send output normally destined for sys.stdout/stderr
to my internal function which could do whatever it wanted with it.  In
your case, you could just accumulate it until some other part of your
application requested it.

Note that this doesn't preclude the script being run from trying to
reset sys.stdout, and it does involve an extra "init" script (hidden
though it was from the main script).  You could be a bit more
self-contained by actually implementing the catcher classes directly
in your embedding application, but for simply embedding typical
scripts that you want to trap the output of, this should work well.

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list