Search code examples
pythonbuilt-ininspect

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


Why can not I inspect the source code of the __builtins__ module?

>>> 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.


Solution

  • You can only inspect Python source this way. 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. (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.)

    Notice that directly above the function you linked in the docs, getsourcefile says:

    This will fail with a TypeError if the object is a built-in module, class, or function.

    And, as you can probably guess (or can verify by looking at inspect.py, linked from the docs), getsource uses getsourcefile under the hood.


    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.

    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. (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. 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.


    So, the best option is to clone the repo and look things up manually, or browse directly on github.