2015-11-05 29 views
0

我正在創建一個項目,我在IronPython中創建一個IronPython編譯器,依賴於IronPython, 但是我在調​​試腳本時可能會遇到一些問題,並且可以使用斷點?你能給我一些幫助嗎?謝謝。我的代碼有:https://github.com/heyxEvget/IronPython-Debugger]如何使用IronPython逐步調試腳本?

public ScriptEngine GetEngine() 
    { 
     if (_engine != null) 
      return _engine; 
     _engine = Python.CreateEngine(); 
     _engine.Runtime.IO.SetOutput(_stream, Encoding.UTF8); 
     _engine.Runtime.IO.SetErrorOutput(_stream, Encoding.UTF8); 
     string path = Environment.CurrentDirectory; 
     string ironPythonLibPath = string.Format(@"{0}\IronPythonLib.zip", path); 
     var paths = _engine.GetSearchPaths() as List<string> ?? new List<string>(); 
     paths.Add(path); 
     paths.Add(ironPythonLibPath); 
     path = Environment.GetEnvironmentVariable("IRONPYTHONPATH"); 
     if (!string.IsNullOrEmpty(path)) 
     { 
      var pathStrings = path.Split(';'); 
      paths.AddRange(pathStrings.Where(p => p.Length > 0)); 
     } 
     _engine.SetSearchPaths(paths.ToArray()); 
     return _engine; 
    } 

    private void GetPythonVarsInfo(ScriptScope scope) 
    { 
     _varList.Clear(); 
     var items = scope.GetItems(); 
     foreach (var item in items) 
     { 
      _varList.Add(new VarValue 
      { 
       VarName = item.Key, 
       Value = item.Value 
      }); 
     } 
     valueListView.ItemsSource = _varList; 
    } 

    private void OnExecuteButtonClick(object sender, ItemClickEventArgs e) 
    { 
     string outPutString = string.Empty; 
     outPutString = "*************************************" + 
      "Excute Date: " + DateTime.Now.ToLocalTime().ToString(CultureInfo.InvariantCulture); 
     ExeceutePython(document, outPutString); 
     TabControl.SelectedIndex = 2; 
    } 

    private void ExeceutePython(EditorDocument document, string outPutString) 
    { 
     ScriptEngine engine = GetEngine(); 
     string script = document.Text; 
     ScriptSource source = engine.CreateScriptSourceFromString(script); 
     ScriptScope scope = _engine.CreateScope(); 
     try 
     { 
      source.Compile(); 
      OutputTextBox.AppendText(outPutString + Environment.NewLine); 
      var result = source.Execute(scope); 
      if (result != null) 
      { 
       OutputTextBox.AppendText(engine.Operations.Format(result)); 
      } 
      OutputTextBox.AppendText(Environment.NewLine); 
      GetPythonVarsInfo(scope); 
     } 
     catch (Exception ex) 
     { 
      var eo = engine.GetService<ExceptionOperations>(); 
      var eoString = eo.FormatException(ex); 
      OutputTextBox.AppendText(eoString); 
      return; 
     } 
    } 
+1

你應該提供您的問題更多的信息,包括一些代碼,你已經嘗試了什麼。但據我所知,你不能在c#中調試腳本。 mAybe以下鏈接也將有所幫助:http://stackoverflow.com/questions/31369279/is-there-any-way-to-debug-python-code-embedded-in-c-sharp-with-visual-studio-和 – HimBromBeere

+0

嗨,我剛剛開始學習IronPython及其幫助文檔,並且我想創建一個Python編譯器白色的C#代碼依賴於Ironpython引擎,我還沒發現Python引擎有任何方法來調試腳本。所以我要求有人在那裏尋求幫助。 – Heyx

+0

我使用syntaxeditor作爲我的編輯控件,通過Actiprosoftware.so創建我只需要編譯後面用於調試的代碼。 – Heyx

回答

0

您可以使用該 Visual Studio Isolated Shell

+0

我很抱歉,我的意思是我想用C#代碼創建一個依賴於IronPython的Python腳本編譯器。 – Heyx

0

鑑於此安裝程序代碼來創建IronPython的腳本引擎

var engine = IronPython.Hosting.Python.CreateEngine(new Dictionary<string, object> { { "Debug", ScriptingRuntimeHelpers.True }}); 
Debug.Assert(engine.Runtime.Setup.DebugMode); 

var source = engine.CreateScriptSourceFromFile("script.py"); 
dynamic result = source.Execute(); 

然後,您可以Visual Studio的隔離外殼使用

System.Diagnostics.Debugger.Break() 

insid e你的腳本讓調試器中斷。

example script example script debugging

+0

嗨,我很抱歉,也許我沒有清楚地解釋我的問題。我只想使用IronPython提供的一些Dll來創建Python腳本調試器。我使用Actiprosoftware提供的Control(Named SyntaxEditor)作爲我的前臺,並使用IronPython DLLS來刪除已插入斷點的代碼。謝謝大家一樣 – Heyx

+0

'ScriptEngine engine = GetEngine(); string script = document.Text; ScriptSource source = engine.CreateScriptSourceFromString(script); ScriptScope scope = _engine.CreateScope(); source.Compile(); OutputTextBox.AppendText(outPutString + Environment.NewLine); var result = source.Execute(scope);' – Heyx

+0

你可以看到我的主代碼:[link](https://github.com/heyxEvget/IronPython-Debugger) – Heyx