- Notifications
You must be signed in to change notification settings - Fork748
Various debugging scenarios of embedded CPython
denfromufa edited this pageJul 5, 2017 ·25 revisions
These 3 debugging scenarios below are based on 2 articles from PTVS docs:
https://docs.microsoft.com/en-us/visualstudio/python/debugging-mixed-modehttps://docs.microsoft.com/en-us/visualstudio/python/debugging-cross-platform-remote
Important pre-requisites:
Install CPython with debugging symbols and binaries, like described in PTVS docs
Enable native code debugging in Visual Studio project settings
Python.NET 2.3.0+ and Visual Studio 2017, ptvsd 3.1.0+, Python 3.5+ are tested for these 3 scenarios
- Attach mixed-mode cross-language debugger (Python, Managed, Native) to pythonnet:
- switch to
DEBUG=1
. - select Managed (4.0+) + Native + Python debug engines manually.
- Attach Python debugger engine to Python code embedded with pythonnet:
- switch to
DEBUG=2
. - select Python debug engine manually.
- Attach Python remote debugger (ptvsd) to Python code embedded with pythonnet:
- switch to
DEBUG=3
. - select Python Remote Debugging option (ptvsd) in Connection Type.
- specify transport string in Connection target:
tcp://localhost@clr:5678
Program.cs
usingSystem;usingPython.Runtime;usingSystem.Diagnostics;usingSystem.Threading;namespacemixedmode{classProgram{staticvoidMain(string[]args){dynamicres;using(Py.GIL()){Console.WriteLine("embedded Python engine");dynamicpymod=Py.Import("mixedmode");Console.WriteLine("Imported testing module");if((int)pymod.DEBUG==1){AttachDebugger();// enable this for mixed-mode debugging}res=pymod.pyfunc();}Console.WriteLine(res);}staticvoidAttachDebugger(){Console.WriteLine("waiting for .NET debugger to attach");while(!Debugger.IsAttached){Thread.Sleep(100);}Console.WriteLine(".NET debugger is attached");}}}
mixedmode.py
DEBUG=2# 1 - mixed-mode cross-language debugger# 2 - attach Python debugger# 3 - ptvsd remote debuggingimportclrifDEBUG==2:importsysimporttimewhilenotsys.gettrace():time.sleep(0.1)elifDEBUG==3:importptvsdptvsd.enable_attach('clr')ptvsd.wait_for_attach()defpyfunc(args=None):ifargs:returnargselse:return"testing"