Hosting IronPyton in C# and Debugging

Ravi Dhoble

Member
Joined
Dec 11, 2019
Messages
13
Programming Experience
1-3
Hello,

I am working on building a GUI based script editor application (Thought of using blockly) to get the python code from blocks. It's a combination of C# and IronPython. I am using IronPython as a background engine to execute python scripts created by the user. Below, the first screenshot shows the integration of IronPython in CSharp.
I can execute the script in one go and get the result at the end of execution in the console window but I want to control the flow of execution. I want to add a breakpoint, step into, step out etc operation while the py script is running (during runtime). For reference, here is the 2nd screenshot of NI TestStand software, in which the execution of the script is done step by step and the next step in the script waits for the previous step to execute completely. So, using IronPython API how can I implement debugging features? and Can I use Microsoft.Scripting.Debugger classes, to have a debugging option in my software?
I am new to C# and Python and learning along the way. So, I have searched a lot about attaching the debugger process but not sure how to implement it.
Few more queries -
1. Here, can I use PDB python debugger to implement debugging features in the IronPython?
2. How to attach the debugger to the IronPython engine?
3. I read about Microsoft.System.Diagnostic.debug classes or is there any other approach? Hope to get guidance from this community forum.

CSharp_IronPython_App-

IronPython_Csharp.png


NI Teststand...
NI Teststand.png
 
Last edited:
I've never had to host DLR that required stepping/debugging. It was always load-and-execute, so take the following with ahuge grain of salt.

It looks like you are on the right track. The following blog post dangles a lot of hints:

Based on how I read things there, if you are hosting, you have pdb like capabilities exposed to you so you don't have to run pdb separately.
 
Thanks for providing that link. Ideally speaking I want to use the debugger program from C# environment so that, I can have easy control over attaching breakpoint, step in/out process while the python file is in execution mode under IronPython engine.
Right now am not sure how I will integrate different things here..like Ironpython engine + Attaching C# debugger process and then showing breakpoint, step by step execution pointer on GUI level with Blockly component..? But I am confident that I will find all the ways to make this work. Further points -
1. I am exploring, how to use Microsoft.Scripting.Debugging class methods to add debugging features. But didn't find related documentation in details.
2. I also found another tool MDbg.exe Which is also an external debugger program.. I Download it.. and extract the contents.. and I can see the entire VS code sln implementation of the debugger. I believe this can help me to integrate into my script editor program. @Skydiver Would you pls look at it once? It would be great to hear your thoughts on this.

Creating such a script editor seems a big task.. and currently am totally in the research phase. Hope to get more responses from different experienced developers. Thanks! Happy weekend. :)
 
I think you'll find this more edifying for how to implement a Python script debugger:

and the corresponding source code for the debugger:
Yeah..I found a couple of stuff on debugging. working on that. But here is a small thing I am facing a problem with.
Currently, I am able to execute a Testscript.py. I'm stuck because I'm not able to access all the properties I need. But when I'm debugging in Visual Studio and explore the object "CalcObj" Or "Scope" or "engine", I can clearly see the properties and non-public members objects (Key-value pair) that I want to access for example like functions defined in PY file (Add, sub etc), arguments (first, second).
I want to extract all these key pair values in c# and stored in array or list to use later in code. I tried a couple of ways but have no clue. Any idea?
 

Attachments

  • AccessAddProperties.png
    AccessAddProperties.png
    414.6 KB · Views: 59
  • First_Second_variables.png
    First_Second_variables.png
    243.7 KB · Views: 46
  • UnableToAccessObjectProperties.png
    UnableToAccessObjectProperties.png
    356.9 KB · Views: 47
Back
Top Bottom