0

I'm working on a project in which I am creating a IronPython compiler depend on IronPython , But I have some problem on debugging the Script and can use breakpoint ? could you please give me some help ? thanks. all my code is there: [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;
        }
    }
Heyx
  • 1
  • 1
  • 2
  • 1
    You should provide much more information on your problem, including some code and what you have tried. However as far as I know you cannot debug a script within c#. mAybe the following link will also help: http://stackoverflow.com/questions/31369279/is-there-any-way-to-debug-python-code-embedded-in-c-sharp-with-visual-studio-and – MakePeaceGreatAgain Nov 05 '15 at 08:33
  • hi,I am just starting learn IronPython with its help document, and I want to create a Python compiler whit C# Code depend on Ironpython engine and I havn't find that the Python Engine has any method to debug the script. so I ask someone for help there. – Heyx Nov 05 '15 at 08:38
  • I use syntaxeditor as my editting control which Create by Actiprosoftware.so I just need complate the code behind which is used for debugging. – Heyx Nov 05 '15 at 08:47

2 Answers2

4

Given this setup code to create the IronPython script engine

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();

You can then use

System.Diagnostics.Debugger.Break()

inside your scripts to get the debugger to break.

example script example script debugging

Michael Baker
  • 3,338
  • 26
  • 29
  • hi, I am sorry that maybe I have not explain my problem clearly. I want Crate a Python Script Debugger just using some Dlls Which provided by IronPython. I use the Control (Named SyntaxEditor) which provided by Actiprosoftware as my foreground,and use IronPython DLLS to Debuge the code which I have Inserted breakpoint. thanks all the same – Heyx Nov 09 '15 at 08:27
  • `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 Nov 09 '15 at 09:22
  • you can see my main code there:[link](https://github.com/heyxEvget/IronPython-Debugger) – Heyx Nov 10 '15 at 08:22
  • this excellent older example still works, and the screenshots explain how to use the call stack window. this setup also mostly works with the watch window. However, I've been wondering - is there a way to make Visual Studio's IronPython Interactive window work with variables in the script.py (i.e. make it connect to same debugging session)? e.g. If I startup Python Interactive window from scratch, I can get import clr and get VS completions on clr. (e.g. AddReference, etc). But neither when writing script.py nor when inspecting variables in script.py in watch window - do I get completions. – Joe Dec 21 '19 at 20:30
  • yes, I am. and to clarify, I am looking for completions not only on built-in libraries such as clr, but also on any custom objects/classes, etc, which is why I am trying to "connect" the Python Interactive window that PTVS provides to the debugging session of the engine that I have in memory as created in your example above. – Joe Dec 22 '19 at 16:21
  • I posted this as a separate question ... https://stackoverflow.com/questions/59449916/can-vss-ironpython-interactive-window-work-with-variables-in-py-file-invoked-f – Joe Dec 23 '19 at 03:39
0

You can use visual studio isolated shell for that Visual Studio Isolated Shell

Prasanth V J
  • 1,126
  • 14
  • 32
  • I am sorry, I mean that I want create a Python Script Compiler depend on IronPython by using C# code. – Heyx Nov 06 '15 at 07:49