Capture python print statements in C# -
i writing c# component takes ironpython function parameter.
def test(): x=x+1 print "test"
c#:
var v = engine.operations.invoke(scope.getvariable("test"));
var v
returns null
print
statements. works if have return(x)
. can capture print statements using ironpython?
comments , links appreciated. also, can capture using commandline?
this works me:
var script = @"import sys def test(): print sys.version print >> sys.stderr, 'this goes stderr' return 42"; var scriptengine = python.createengine(); var scriptscope = scriptengine.createscope(); scriptengine.execute(script, scriptscope); var testfn = scriptscope.getvariable("test"); var streamout = new memorystream(); var streamerr = new memorystream(); scriptengine.runtime.io.setoutput(streamout, encoding.default); scriptengine.runtime.io.seterroroutput(streamerr, encoding.default); scriptengine.operations.invoke(testfn); console.writeline("returned: {0}", scriptengine.operations.invoke(testfn)); console.writeline("captured out:\n{0}", encoding.default.getstring(streamout.toarray())); console.writeline("captured err:\n{0}", encoding.default.getstring(streamerr.toarray()));
output:
returned: 42 captured out: 2.7.5b2 (ironpython 2.7.5b2 (2.7.5.0) on .net 2.0.50727.5477 (64-bit)) captured err: goes stderr
Comments
Post a Comment