代码之家  ›  专栏  ›  技术社区  ›  Daren Thomas

如何关闭执行脚本的IronPython引擎的输入流?

  •  0
  • Daren Thomas  · 技术社区  · 15 年前

    var engine = IronPython.Hosting.Python.CreateEngine();                
    var scope = engine.CreateScope();
    
    // my implementation of System.IO.Stream
    var stream = new ScriptOutputStream(engine);
    engine.Runtime.IO.SetOutput(stream, Encoding.UTF8);
    engine.Runtime.IO.SetErrorOutput(stream, Encoding.UTF8);
    engine.Runtime.IO.SetInput(stream, Encoding.UTF8);
    
    var script = engine.CreateScriptSourceFromString(source, SourceCodeKind.Statements);
    script.Execute(scope);
    

    变量 source 是包含以下内容(python语句)的字符串:

    import code
    code.interact(None, None, 
            {
                '__name__' :  '__console__', 
                '__doc__'  :   None, 
            })
    

    Read

        /// <summary>
        /// Read from the _inputBuffer, block until a new line has been entered...
        /// </summary>
        public override int Read(byte[] buffer, int offset, int count)
        {
    
            if (_gui.IsDisposed)
            {
                return 0; // msdn says this indicates the stream is closed
            }
    
            while (_completedLines.Count < 1)
            {
                // wait for user to complete a line
                Application.DoEvents();
                Thread.Sleep(10);
            }
            var line = _completedLines.Dequeue();
            return line.Read(buffer, offset, count);
        }
    

    成员变量 _completedLines 排队等候 MemoryStream 表示用户迄今为止输入的线的对象。 _gui 是对windows窗体的引用—当它被释放时,我希望IronPython引擎停止执行 code.interact() .

    返回 0 阅读 方法不起作用( 阅读 刚刚又打了一次电话)。从 documentation of Read 阅读

    我也试过回来 ^Z (0x1a)和 ^D (0x04)英寸 的缓冲区,因为它们在控制台上用于退出解释器,但这根本不起作用。。。

    1 回复  |  直到 15 年前
        1
  •  1
  •   Jeff Hardy    15 年前

    code.interact 预计将从 raw_input issue #22140 .

    推荐文章