程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Steps and cases of dynamic debugging Python source code

編輯:Python

1. introduce

python Of c Language version of the source code cpython It can be downloaded from github Download the ( See reference 1).

How to run and debug the source code ?

2. debugging cpython Steps for

The author is in Windows 10 On the use of Visual Studio Community2019 debug , Steps are as follows :

  1. download cpython Source code , The author from the reference 2 Downloaded python3.10.4 Source code

  2. double-click ( use Visual Studio) Open the source directory PCbuild/pcbuild.sln file

  3. VS Set in the for Debug,Win32 Pattern

  4. stay Solution, Right click , choice properties, choice Configuration Properties, choice Configuration, How to choose Build Select only python and pythoncore, The details are shown in the following figure

  5. And then run directly debug You can debug . The interface obtained after operation and direct operation python The command gets exactly the same interface , As shown in the figure below :

3. Simple debugging case

We know , stay python shell in , Enter a statement , Will execute and get results , Then enter another statement and execute it to get the result , Like an endless loop, you can always input 、 perform . How does this process work in the source code ?

  1. First find main Where is the function , according to VS Of Startup Project( Black font ) Can find ,C Linguistic main Function in Program/python.c in
  2. from main Function into , Add a breakpoint , Step by step debugging , You can get into Python/pythonrun.c, The following will simplify the source code , See reference 3
int _PyRun_InteractiveLoopObject()
{

do {
// In this cycle , Run interactive statements 
ret = PyRun_InteractiveOneObjectEx(fp, filename, flags);
} while (ret != E_EOF);
return err;
}

4. summary

stay Windows 10 On the use of Visual Studio Community2019, Can debug python Source code ,python The source code also comes with a suitable VS Open taxi .sln file , Very convenient for debugging .

Reference resources

  1. https://github.com/python/cpython
  2. https://github.com/python/cpython/releases/tag/v3.10.4
  3. https://github.com/python/cpython/blob/8a0d9a6bb77a72cd8b9ece01b7c1163fff28029a/Python/pythonrun.c#L136

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved