Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

  1. Attach mixed-mode cross-language debugger (Python, Managed, Native) to pythonnet:
  • switch toDEBUG=1.
  • select Managed (4.0+) + Native + Python debug engines manually.

  1. Attach Python debugger engine to Python code embedded with pythonnet:
  • switch toDEBUG=2.
  • select Python debug engine manually.

  1. Attach Python remote debugger (ptvsd) to Python code embedded with pythonnet:
  • switch toDEBUG=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"
Clone this wiki locally

[8]ページ先頭

©2009-2025 Movatter.jp