コード例 #1
0
ファイル: PythonInterpreter.cs プロジェクト: 424f/lebowski
        /// <summary>
        /// Executes the code and redirects the results into a writer object.
        /// </summary>
        /// <param name="code">Source code to be executed.</param>
        /// <param name="writer">The result is written here.</param>
        /// <returns>Object that encapsulates the compiles code.</returns>
        public object ExecuteCode(string code, PythonWriter writer)
        {
            ScriptSource source = engine.CreateScriptSourceFromString(code, SourceCodeKind.Statements);
            Object o = null;
            try
            {
                CompiledCode compiled = source.Compile();
                object sys = runtime.GetSysModule();
                engine.Operations.SetMember(sys, "stdout", writer);
                engine.Operations.SetMember(sys, "stderr", writer);

                o = compiled.Execute(scope);

                // TODO: allow canceling operation
            }
            catch (Exception e)
            {
                ExceptionOperations eo = engine.GetService<ExceptionOperations>();
                string error = eo.FormatException(e);
                writer.write(error);
            }
            return o;
        }