简体   繁体   English

为什么我们不能检查`__builtins__`模块的源代码?

[英]Why can not we inspect the source code of the `__builtins__` module?

Why can not I inspect the source code of the __builtins__ module? 为什么我不能检查__builtins__模块的源代码?

>>> import inspect
>>> inspect.getsource(__builtins__)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/inspect.py", line 701, in getsource
    lines, lnum = getsourcelines(object)
  File "/usr/lib/python2.7/inspect.py", line 690, in getsourcelines
    lines, lnum = findsource(object)
  File "/usr/lib/python2.7/inspect.py", line 526, in findsource
    file = getfile(object)
  File "/usr/lib/python2.7/inspect.py", line 403, in getfile
    raise TypeError('{!r} is a built-in module'.format(object))
TypeError: <module '__builtin__' (built-in)> is a built-in module

I went through this talk but it did not help. 我经历了这个谈话,但没有帮助。

This should not happen, if I interpret this well: 如果我能很好地解释这一点,那就不会发生:

>>> help(inspect.getsource)
Help on function getsource in module inspect:

getsource(object)
    Return the text of the source code for an object.

    The argument may be a module, class, method, function, traceback, frame,
    or code object.  The source code is returned as a single string.  An
    IOError is raised if the source code cannot be retrieved.

Is there a way to overcome this other than browsing the Python GitHub repository as described here and there . 有没有一种方法来克服与浏览描述了Python的GitHub库这等在这里那里

You can only inspect Python source this way. 您只能以这种方式检查Python源。 Builtin modules, and extension modules written with the C API, do not have any source in them, so there's no way to inspect it. 内置模块和使用C API编写的扩展模块中没有任何源,因此无法对其进行检查。 (When you compile C code, the result may have some debugging information, including the local pathnames of the files used to build it, but it doesn't include the actual source text.) (当编译C代码时,结果可能包含一些调试信息,包括用于构建它的文件的本地路径名,但其中不包括实际的源文本。)

Notice that directly above the function you linked in the docs, getsourcefile says: 请注意,直接在文档中链接的函数上方, getsourcefile表示:

This will fail with a TypeError if the object is a built-in module, class, or function. 如果对象是内置模块,类或函数,则将失败并显示TypeError

And, as you can probably guess (or can verify by looking at inspect.py , linked from the docs), getsource uses getsourcefile under the hood. 而且,您可能会猜到(或可以通过查看docs链接的inspect.py进行验证), getsourcefilegetsource使用了getsourcefile


If you built Python locally on your machine, and left the source code there after building, there is a project that can find the C sources used to build each module, but I can't find it (I think it was on a now-long-dead Berlios or Sourceforge), and I don't think it was ever updated beyond around the 2.4 days. 如果您是在计算机上本地构建Python,并在构建后将源代码保留在那里,那么有一个项目可以找到用于构建每个模块的C源代码,但我找不到它(我认为它位于现在-长死不救的Berlios或Sourceforge),而且我认为它不会在2.4天后更新。

It probably wouldn't be too hard to write your own module to look up the source in the github repo—or, maybe better, in your own local clone of the github repo. 编写您自己的模块以在github存储库中查找源代码可能并不难,或者更好的是,在您自己的github存储库本地副本中查找源代码。 (Which would be far better than relying on a locally-built Python…) You could maybe even extend it to use setuptools information to find source for pip -installed extension modules that follow certain common patterns. (这比依赖于本地构建的Python要好得多……)您甚至可以扩展它,以使用setuptools信息来查找遵循某些常见模式的pip安装的扩展模块的源。 But, as far as I know, nobody's published such a module. 但是据我所知,没有人发布过这样的模块。

If you want to build something like this yourself, see this quick&dirty proof of concept . 如果您想自己构建类似的东西,请参阅此概念的快速和肮脏的证明 Although you'd probably want to use either git or the Github API instead of scraping, and you'd want to be able to search a local repo (maybe cloning it if not found) and/or cache things between runs, and so on, this shows both how easy it is and how much special-casing it requires. 尽管您可能想使用git或Github API而不是抓取,并且希望能够搜索本地存储库(如果找不到它,则可以克隆它)和/或在运行之间缓存内容,等等。 ,这既显示了它的简单性,又显示了它需要多少特殊外壳。


So, the best option is to clone the repo and look things up manually, or browse directly on github. 因此,最好的选择是克隆存储库并手动查找内容,或者直接在github上浏览。

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

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