Example #1
0
        public IronPythonResponse Run(IronPythonScript script)
        {
            object          actual       = null;
            SimpleException ex           = null;
            MemoryStream    outputStream = new MemoryStream();
            StringWriter    outputWriter = new StringWriter();

            MemoryStream errorStream = new MemoryStream();
            StringWriter errorWriter = new StringWriter();

            try
            {
                var engine = Python.CreateEngine();

                if (SynchronizationContext.Current == null)
                {
                    SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
                }

                AddAssemblyReferences(engine);
                engine.Runtime.IO.SetOutput(outputStream, outputWriter);
                engine.Runtime.IO.SetErrorOutput(errorStream, errorWriter);

                var scope = engine.CreateScope();
                scope.SetVariable("scenario", Scenario);

                if (ProjectHandler == null)
                {
                    ProjectHandler = ProjectManager.Instance?.ProjectHandler;
                }
                scope.SetVariable("project_handler", ProjectHandler);
                var sourceCode = engine.CreateScriptSourceFromString(script.Script);
                actual = sourceCode.Execute <object>(scope);
                if (scope.ContainsVariable("result"))
                {
                    actual = scope.GetVariable("result");
                }
            }
            catch (Exception e)
            {
                ex = new SimpleException(e);
            }

            return(new IronPythonResponse()
            {
                Response = AsKnownDataContract(actual),
                StandardError = errorWriter.ToString(),
                StandardOut = outputWriter.ToString(),
                Exception = ex
            });
        }
Example #2
0
        public IronPythonResponse Run(IronPythonScript script)
        {
            object actual = null;
            SimpleException ex = null;
            MemoryStream outputStream = new MemoryStream();
            StringWriter outputWriter = new StringWriter();

            MemoryStream errorStream = new MemoryStream();
            StringWriter errorWriter = new StringWriter();

            try
            {
                var engine = Python.CreateEngine();

                if(SynchronizationContext.Current==null)
                    SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());

                AddAssemblyReferences(engine);
                engine.Runtime.IO.SetOutput(outputStream, outputWriter);
                engine.Runtime.IO.SetErrorOutput(errorStream, errorWriter);

                var scope = engine.CreateScope();
                scope.SetVariable("scenario", Scenario);

                if (ProjectHandler == null)
                {
                    ProjectHandler = ProjectManager.Instance.ProjectHandler;
                }
                scope.SetVariable("project_handler", ProjectHandler);
                var sourceCode = engine.CreateScriptSourceFromString(script.Script);
                actual = sourceCode.Execute<object>(scope);
                if (scope.ContainsVariable("result"))
                    actual = scope.GetVariable("result");
            }
            catch (Exception e)
            {
                ex = new SimpleException(e);
            }

            return new IronPythonResponse()
            {
                Response = AsKnownDataContract(actual),
                StandardError = errorWriter.ToString(),
                StandardOut = outputWriter.ToString(),
                Exception = ex
            };
        }
Example #3
0
 public IronPythonResponse RunIronPython(IronPythonScript script)
 {
     if (!AllowScript)
     {
         Log(String.Format("Attempt to run IronPython script, but AllowScript=false. Script:\n{0}", script.Script));
         WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.Forbidden;
         return null;
     }
     Log(String.Format("Running IronyPython script:\n{0}",(script.Script.Length>80)?(script.Script.Substring(0,75)+"..."):script.Script));
     scriptRunner.Scenario = Scenario;
     scriptRunner.ProjectHandler = ProjectHandler;
     return scriptRunner.Run(script);
 }