The test below fails (see stacktrace). If I take the exact same code and execute from a running program,it works just fine. Any suggestions,
TIA Matt B. [Test] public void Statements() { var e = new IPEngine(); e.Scope.SetVariable("x","testing"); var script = "import System\nfrom System import *\nx = 'kosher'\nprint 'test'"; e.Execute(script); Assert.AreEqual("kosher",e.Scope.GetVariable<string>("x")); } failed: System.InvalidProgramException : Common Language Runtime detected an invalid program. at Microsoft.Scripting.Actions.MatchCaller.Call6[T0,T1,T2,T3,T4,T5,TRet](Func`8 target, CallSite site, Object[] args) at Microsoft.Scripting.Actions.CallSite`1.UpdateAndExecute(Object[] args) at Microsoft.Scripting.Actions.UpdateDelegates.Update6[T,T0,T1,T2,T3,T4,T5,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5) at IronPython.Runtime.Importer.Import(CodeContext context, String fullName, PythonTuple from, Int32 level) at IronPython.Runtime.Operations.PythonOps.ImportTop(CodeContext context, String fullName, Int32 level) at <module>$1##1(Closure , Scope , LanguageContext ) at Microsoft.Scripting.Runtime.OptimizedScriptCode.InvokeTarget(LambdaExpression code, Scope scope) at Microsoft.Scripting.ScriptCode.Run(Scope scope) at Microsoft.Scripting.SourceUnit.Execute(Scope scope, ErrorSink errorSink) at Microsoft.Scripting.Hosting.ScriptSource.Execute(ScriptScope scope) public class IPEngine { #region Member Vars private readonly ScriptEngine engine; private readonly ScriptScope scope; private MemoryStream ms; #endregion Member Vars public IPEngine() { engine = Python.CreateEngine(); engine.ImportModule("clr"); engine.Runtime.LoadAssembly(typeof(System.String).Assembly); scope = Engine.CreateScope(); } public ScriptEngine Engine { get { return engine; } } public ScriptScope Scope { get { return scope; } } public void SetupOutput() { ms = new MemoryStream(); engine.Runtime.IO.SetOutput(ms, Encoding.UTF8); //engine.Runtime.IO.SetErrorOutput(ms, Encoding.UTF8); } /// <summary> /// Evaluates a single expressoin /// </summary> /// <typeparam name="T">Return Type</typeparam> /// <param name="expression">Expressions to Eval</param> /// <returns>T</returns> public T Eval<T>(string expression) { SetupOutput(); var source = Engine.CreateScriptSourceFromString(expression, SourceCodeKind.Expression); return source.Execute<T>(Scope); } public void Execute(string statements) { SetupOutput(); var source = engine.CreateScriptSourceFromString(statements, SourceCodeKind.Statements); source.Execute(scope); } public string Output { get { using (var sr = new StreamReader(ms)) { ms.Position = 0; return sr.ReadToEnd(); } } } }
_______________________________________________ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com