private void DoRunScript(object sender, PythonScriptEditor.RunScriptEventArgs e) { try { ScriptEngine engine = Python.CreateEngine(); engine.Runtime.LoadAssembly(Assembly.GetExecutingAssembly()); engine.Runtime.IO.SetOutput(new MemoryStream(), new ConsoleTextWriter(this, false)); engine.Runtime.IO.SetErrorOutput(new MemoryStream(), new ConsoleTextWriter(this, true)); ICollection <string> paths = engine.GetSearchPaths(); paths.Add(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "PythonLib")); engine.SetSearchPaths(paths); ScriptErrorListener listener = new ScriptErrorListener(); ScriptSource source = engine.CreateScriptSourceFromString(e.ScriptText); CompiledCode code = source.Compile(listener); if (listener.Errors.Count == 0) { // Just create the global scope, don't execute it yet ScriptScope scope = engine.CreateScope(); scope.SetVariable("obj", COMUtilities.IsComImport(m_dispType) ? new DynamicComObjectWrapper(m_registry, m_dispType, m_pObject) : m_pObject); scope.SetVariable("disp", m_pObject); dynamic host = new ExpandoObject(); host.openobj = new Action <DynamicComObjectWrapper>(o => { OpenObjectViewer(o); }); scope.SetVariable("host", host); code.Execute(scope); } } catch (Exception ex) { TargetInvocationException tex = ex as TargetInvocationException; if (tex != null) { ex = tex.InnerException; } AddText(ex.Message + Environment.NewLine); } }
private void toolStripButtonRun_Click(object sender, EventArgs e) { try { ScriptEngine engine = Python.CreateEngine(); engine.Runtime.LoadAssembly(Assembly.GetExecutingAssembly()); engine.Runtime.IO.SetOutput(new MemoryStream(), new ConsoleTextWriter(this, false)); engine.Runtime.IO.SetErrorOutput(new MemoryStream(), new ConsoleTextWriter(this, true)); ICollection<string> paths = engine.GetSearchPaths(); paths.Add(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "PythonLib")); engine.SetSearchPaths(paths); ScriptErrorListener listener = new ScriptErrorListener(); ScriptSource source = engine.CreateScriptSourceFromString(textEditorControl.Text); CompiledCode code = source.Compile(listener); if (listener.Errors.Count == 0) { // Just create the global scope, don't execute it yet ScriptScope scope = engine.CreateScope(); scope.SetVariable("obj", COMUtilities.IsComImport(m_dispType) ? new DynamicComObjectWrapper(m_registry, m_dispType, m_pObject) : m_pObject); scope.SetVariable("disp", m_pObject); dynamic host = new ExpandoObject(); host.openobj = new Action<DynamicComObjectWrapper>(o => { OpenObjectViewer(o); }); scope.SetVariable("host", host); code.Execute(scope); } } catch (Exception ex) { TargetInvocationException tex = ex as TargetInvocationException; if (tex != null) { ex = tex.InnerException; } AddText(ex.Message + Environment.NewLine); } }