如何在C#中重定向IronPython的标准输出?

问题描述 投票:15回答:3

我有以下内容:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button3_Click(object sender, EventArgs e)
    {
        try
        {
            var strExpression = @"
            import sys
            sys.stdout=my.write
            print 'ABC'
            ";
            var engine = Python.CreateEngine();
            var scope = engine.CreateScope();
            var sourceCode = engine.CreateScriptSourceFromString(strExpression);
            scope.SetVariable("my", this);
            var actual = sourceCode.Execute<string>(scope);
            textBox1.Text += actual;
        }
        catch (System.Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

    public void write(string s)
    {
        textBox1.Text += s;
    }
}

但我得到一个Exception说没有write

我做错了什么?

c# redirect ironpython stdout
3个回答
25
投票

您可以直接从c#设置流和文本编写器:

engine.Runtime.IO.SetOutput(stream, txtWriter);
engine.Runtime.IO.SetErrorOutput(stream, txtWriter);

例如,要重定向输出,您可以使用文本框上的新文本覆盖TextWriter类。


EG

在我的应用程序中,我做了一个StreamWriter类的重写,当在流上写入某些内容时,这会引发事件(这里只是代码的一部分):

public class MyEvtArgs<T> : EventArgs
{
    public T Value
    {
        get;
        private set;
    }
    public MyEvtArgs(T value)
    {
        this.Value = value;
    }
}


public class EventRaisingStreamWriter : StreamWriter
{
    #region Event
    public event EventHandler<MyEvtArgs<string>> StringWritten;
    #endregion

    #region CTOR
    public EventRaisingStreamWriter(Stream s):base(s)
    { }
    #endregion

    #region Private Methods
    private void LaunchEvent(string txtWritten)
    {
        if (StringWritten != null)
        {
            StringWritten(this, new MyEvtArgs<string>(txtWritten));
        }
    }
    #endregion


    #region Overrides

    public override void Write(string value)
    {
        base.Write(value);
        LaunchEvent(value);
    }
    public override void Write(bool value)
    {
        base.Write(value);
        LaunchEvent(value.ToString());
    }
    // here override all writing methods...

    #endregion
}

然后在您的应用程序中,您应该执行以下操作:

    MemoryStream ms = new MemoryStream();

    EventRaisingStreamWriter outputWr = new EventRaisingStreamWriter(ms);
    outputWr.StringWritten += new EventHandler<MyEvtArgs<string>>(sWr_StringWritten);

    var engine = Python.CreateEngine();
    engine.Runtime.IO.SetOutput(ms, outputWr);
    engine.CreateScriptSourceFromString("print 'hello world!'").Execute();


    void sWr_StringWritten(object sender, MyEvtArgs<string> e)
    {
        textBox1.Text += e.Value;
    }

3
投票

你的例子接近工作。

你看到的问题是因为sys.stdout=my.write应该是sys.stdout=my。 Python似乎也希望找到一个布尔softspace属性。

我在下面的代码中做了这两个更改。希望现在应该按照您的预期工作。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button3_Click(object sender, EventArgs e)
    {
        try
        {
            var strExpression = @"
import sys
sys.stdout=my
print 'ABC' ";

            var engine = Python.CreateEngine();
            var scope = engine.CreateScope();
            var sourceCode = engine.CreateScriptSourceFromString(strExpression);
            scope.SetVariable("my", this);
            var actual = sourceCode.Execute(scope);
            textBox1.Text += actual;
        } catch (System.Exception ex) {
            MessageBox.Show(ex.ToString());
        }
    }

    public bool softspace;

    public void write(string s)
    {
        textBox1.Text += s;
    }
}

0
投票

这对我来说很好

        pyRuntime = Python.CreateRuntime();
        pyRuntime.IO.SetOutput(Console.OpenStandardOutput(), new UTF8Encoding(true, false));
© www.soinside.com 2019 - 2024. All rights reserved.