[英]Can I step into Python library code?
When I run my Python debugger, I can step into functions that I write. 当我运行我的Python调试器时,我可以进入我编写的函数。 But if I try to step into a library function like
os.mkdir("folder")
, for example, it "steps over" it instead. 但是,如果我尝试进入像
os.mkdir("folder")
这样的库函数,那么它就会“跨越”它。 Is there a way to step into builtin library functions to see what Python is doing under the hood? 有没有办法进入内置库函数,看看Python正在做什么?
Ideally there'd be a way to do this in PyPy so that you could keep drilling down into Python code. 理想情况下,有一种方法可以在PyPy中执行此操作,以便您可以继续深入研究Python代码。
pdb
, the Python Debugger, cannot step into C functions like os.mkdir
, but gdb
can. pdb
,Python调试器,不能进入像os.mkdir
这样的C函数,但是gdb
可以。 Try this: 尝试这个:
gdb --args python whatever.py ...
Then: 然后:
start
break posix_mkdir
continue
You should see it stop inside Python's implementation of os.mkdir, as detailed here: https://stackoverflow.com/a/16617835/4323 您应该看到它在Python的os.mkdir实现中停止,详见此处: https ://stackoverflow.com/a/16617835/4323
os.mkdir()
is implemented in C code and pdb
cannot step into that function. os.mkdir()
在C代码中实现, pdb
无法进入该函数。
You are limited to debugging pure Python code only; 您仅限于调试纯Python代码; it doesn't matter if that code is part of the standard library or not.
该代码是否是标准库的一部分并不重要。 You can step into the
shutil
module, or os.path
just fine, for example. 例如,您可以单步进入
shutil
模块或os.path
。
os.mkdir()
has to call into native code because it interacts with the OS; os.mkdir()
必须调用本机代码,因为它与操作系统交互; even PyPy has to defer to the underlying (host-Python) os.mkdir()
call to handle that part, so you cannot step into it with pdb
even in PyPy. 即使PyPy必须
os.mkdir()
底层(host-Python) os.mkdir()
调用来处理该部分,因此即使在PyPy中也无法使用pdb
进入它。 In fact, just like in CPython, that part of the standard library is part of the RPython runtime and not seen as 'native Python code' by PyPy either, just like the built-in types are part of the runtime environment. 事实上,就像在CPython中一样,标准库的那部分是RPython运行时的一部分,并且不被PyPy视为“本机Python代码”,就像内置类型是运行时环境的一部分一样。
You could run the PyPy interpreter untranslated (so not statically compile the RPython code but have Python run the PyPy interpreter directly), but that'll only give you access to the RPython code paths, not the os.mkdir()
C code. 您可以运行PyPy解释器未翻译 (因此不能静态编译RPython代码,但让Python直接运行PyPy解释器),但这只能让您访问RPython代码路径,而不是
os.mkdir()
C代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.