[IronPython] retreiving output
Larry O'Brien
lobrien at knowing.net
Tue Jan 25 22:20:42 CET 2005
I sent the original of this offlist...
What I did in Pynk (article and source at
http://www.devx.com/TabletPC/Article/26666) was create a buffer class and
redirect Python's stdout to it:
q = new OutputQueue();
sys.stdout = q.pythonOut;
...snip...
public class OutputQueue
{
MemoryStream outStr = new MemoryStream(1024);
StreamReader outRdr;
public PythonFile pythonOut;
public OutputQueue()
{
outRdr = new StreamReader(outStr);
pythonOut = new PythonFile(outStr, "rw", false);
}
public string ReadToEnd()
{
pythonOut.flush();
//Reset and read
outStr.Position = 0;
string retval = outRdr.ReadToEnd();
outRdr.DiscardBufferedData();
//Null it out
outStr.Position = 0;
byte[] nulls = new byte[retval.Length];
outStr.Write(nulls,0,nulls.Length);
//Reset for writing
outStr.Position = 0;
return retval;
}
}
Cheers,
Larry
More information about the Ironpython-users
mailing list