简体   繁体   English

我可以进入Python库代码吗?

[英]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.

相关问题 如何进入 PyCharm 中的内部 Python 代码? - How can I step into internal Python code in PyCharm? 如何查看 python 库的源代码? - How can I see the source code for a python library? 我可以使用Jython将Java库与我的Python代码链接吗 - Can I link a Java library using Jython with my code in Python 如何查看 Python 库“键盘”的源代码? - How can I see the source code for the Python library “keyboard”? 如何逐步检查python代码以查找何时修改了变量? - How can I step through python code to find when a variable is modified? Python代码逐步了解代码 - Python Code Understanding the code step by step 如何跳转到 python 中 pdb 中的代码而不是库代码? - How can I jump into my code in pdb in python rather than library code? 如何配置 VS Code 以便能够进入调试 Python 脚本时加载的共享库 (.so)? - How to configure VS Code to be able to step into a shared library (.so) that is loaded when debugging a Python script? 在没有库源代码的情况下,Python SWIG可以绑定库吗? - Can python SWIG bind a library in the absence of the library's source code? 为什么我不能使用一个步骤来更改Python列表中的项目? - Why can't I change items in a Python list using a step?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM