如何使用IronPython逐步调试脚本?

时间:2015-11-05 08:31:55

标签: c# .net debugging ironpython

我正在开发一个项目,我正在创建一个依赖于IronPython的IronPython编译器, 但是我在调​​试脚本时遇到了一些问题,可以使用断点吗?你能帮我个忙吗?谢谢。我的所有代码都在那里:[https://github.com/heyxEvget/IronPython-Debugger]

  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;
        }
    }

2 个答案:

答案 0 :(得分:1)

给定此设置代码以创建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();

然后您可以使用

System.Diagnostics.Debugger.Break()

在脚本中,以使调试器中断。

example script example script debugging

答案 1 :(得分:0)

您可以使用visual studio isolated shell Visual Studio Isolated Shell