[IronPython] Redirecting stdout/stderr, but with context

Jeff Slutter jslutter at reactorzero.com
Sat Jan 31 03:02:26 CET 2009


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.

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



More information about the Ironpython-users mailing list