[IronPython] Redirecting stdout/stderr, but with context

Michael Foord fuzzyman at voidspace.org.uk
Sat Jan 31 13:58:48 CET 2009

Jeff Slutter wrote:
> I have a bit of a problem that I'm not sure how to solve. In my
> application, I have multiple documents, and each can possibly be
> performing script operations that print text out via the "print"
> function. I have one "script output" window, which, only shows the
> output of whichever is the active document's script (each document has
> its own ScriptScope).
> When you switch the active document, the "script output window"
> refreshes displaying the stdout/stderr text for its script. For each
> document, I keep a buffer of the text.
> I redirected stdout/stderr by setting sys.stdout and sys.stderr to an
> instance of a class I wrote that has the "write" function in it. The
> problem is, sys.stdout is shared across all of the scopes. So, when I
> set it for one scope, it changes for all ScriptScopes.

We solve this problem by using separate engines. That way we can set the 
output / error streams separately per engine (runtime actually I think).

The engines also share less state this way - which may or may not be 
what you want.

> I can't figure out a way to pass along the information about which
> document the stdout output should go to.
> Is there any way to solve this problem? Or will I have to ban my users
> from using print and stdout and go with a custom function?
> Note: Each ScriptScope does have a variable that is the document it
> belongs to, so I know that information in the script. The problem is,
> print just goes right to sys.stdout and sys.stdout is shared. Also, keep
> in mind it is entirely possible for an inactive document to be printing
> out something. In that case I still need to get the string to be
> printed, but I'm only storing it at that point (and skipping the output
> to my textbox).
> help!
> Thanks,
> Jeff
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com


More information about the Ironpython-users mailing list