简体   繁体   English

如何在pycharm中查看python的内置函数的实现?

[英]How to view the implementation of python's built-in functions in pycharm?

When I try to view the built-in function all() in PyCharm, I could just see "pass" in the function body. 当我尝试在PyCharm中查看内置函数all()时,我只能在函数体中看到“pass”。 How to view the actual implementation so that I could know what exactly the built-in function is doing? 如何查看实际的实现,以便我可以知道内置函数究竟在做什么?

def all(*args, **kwargs): # real signature unknown
    """
    Return True if bool(x) is True for all values x in the iterable.

    If the iterable is empty, return True.
    """
    pass

Assuming you're using the usual CPython interpreter, all is a builtin function object, which just has a pointer to a compiled function statically linked into the interpreter (or libpython). 假设您正在使用通常的CPython解释器, all都是内置函数对象,它只有一个指向静态链接到解释器(或libpython)的编译函数的指针。 Showing you the x86_64 machine code at that address probably wouldn't be very useful to the vast majority of people. 向您显示该地址的x86_64机器代码可能对绝大多数人来说不是很有用。


Try running your code in PyPy instead of CPython. 尝试在PyPy中运行代码而不是CPython。 Many things that are builtins in CPython are plain old Python code in PyPy. 在CPython中构建的许多东西都是PyPy中的普通旧Python代码。 1 Of course that isn't always an option (eg, PyPy doesn't support 3.7 features yet, there are a few third-party extension modules that are still way too slow to use, it's harder to build yourself if you're on some uncommon platform…), so let's go back to CPython. 1当然,这并不总是一个选择(例如,PyPy还不支持3.7功能,有一些第三方扩展模块仍然太慢而无法使用,如果你开的话,你自己更难建立一些不寻常的平台......),让我们回到CPython。


The actual C source for that function isn't too hard to find online. 该功能的实际C源并不难在线查找。 It's in bltinmodule.c . 它在bltinmodule.c But, unlike the source code to the Python modules in your standard library, you probably don't have these files around. 但是,与标准库中的Python模块的源代码不同,您可能没有这些文件。 Even if you do have them, the only way to connect the binary to the source is through debugging output emitted when you compiled CPython from that source, which you probably didn't do. 即使你确实拥有它们,将二进制文件连接到源的唯一方法是通过调试从该源编译CPython时发出的输出,这可能是你没有做到的。 But if you're thinking that sounds like a great idea—it is. 但如果你认为这听起来像个好主意 - 那就是。 Build CPython yourself (you may want a Py_DEBUG build), and then you can just run it in your C source debugger/IDE and it can handle all the clumsy bits. 自己构建CPython(您可能需要Py_DEBUG构建),然后您可以在C源代码调试器/ IDE中运行它,它可以处理所有笨拙的位。

But if that sounds more scary than helpful, even though you can read basic C code and would like to find it… 但是,如果这听起来更有害而不是有用,即使你可以阅读基本的C代码,并希望找到它......


How did I know where to find that code on GitHub? 我怎么知道在GitHub上找到代码的位置? Well, I know where the repo is; 好吧,我知道回购的地方; I know the basic organization of the source into Python, Objects, Modules, etc.; 我知道Python,对象,模块等源的基本组织; I know how module names usually map to C source file names; 我知道模块名称通常如何映射到C源文件名; I know that builtins is special in a few ways… 我知道内置在某些方面很特别......

That's all pretty simple stuff. 这都是非常简单的事情。 Couldn't you just program all that knowledge into a script, which you could then build a PyCharm plugin out of? 难道你不能将所有这些知识编程到一个脚本中,然后你可以构建一个PyCharm插件吗?

You can do the first 50% or so in a quick evening hack, and such things litter the shores of GitHub. 你可以在一个快速的晚上黑客中做前50%左右,这样的东西乱扔在GitHub的海岸。 But actually doing it right requires handling a ton of special cases, parsing some ugly C code, etc. And for anyone capable of writing such a thing, it's easier to just lldb Python than to write it. 但实际上做得正确需要处理大量特殊情况,解析一些丑陋的C代码等等。对于任何能够编写这样的东西的人来说,只比lldb Python更容易编写它。


1. Also, even the things that are builtins are written in a mix of Python and a Python subset called RPython, which you might find easier to understand than C—then again, it's often even harder to find that source, and the multiple levels that all look like Python can be hard to keep straight. 1.此外,即使内置的东西都是用Python和一个名为RPython的Python子集混合编写的,你可能会发现它比C更容易理解,然后再次找到源代码,以及多个级别所有看起来像Python都很难保持直线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM