2

Can I get python to print the source code for __builtins__ directly?

OR (more preferably):

What is the pathname of the source code for __builtins__?


I at least know the following things:

  • __builtins__ is a module, by typing type(__builtins__).

  • I have tried the best-answer-suggestions to a more general case of this SO question: "Finding the source code for built-in Python functions?". But no luck:

    • print inspect.getdoc(__builtins__) just gives me a description.

    • inspect.getfile(__builtins__) just gives me an error: TypeError: <module '__builtin__' (built-in)> is a built-in module

    • https://hg.python.org/cpython/file/c6880edaf6f3/# does not seem to contain an entry for __builtins__. I've tried "site:" search and browsed several of the directories but gave up after a few.

Community
  • 1
  • 1
  • @MooingRawr I have tried the answers in that original post –  Nov 28 '16 at 19:25
  • should have mention that... then .... – MooingRawr Nov 28 '16 at 19:26
  • @MooingRawr, cheers, my bad will edit –  Nov 28 '16 at 19:27
  • Note: the `__builtins__` name is an implementation detail, and `__builtins__` does not behave as consistently as you might expect. For example, it's not always a module! If you want to access the module where built-in names are defined, you should `import __builtin__` (no s), or `import builtins` (no underscores) on Python 3. – user2357112 Nov 28 '16 at 21:23

2 Answers2

6

The __builtin__ module is implemented in Python/bltinmodule.c, a rather unusual location for a rather unusual module.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • Cheers, how did you know this? or how did you work it out? –  Nov 28 '16 at 19:26
  • 1
    @Oracle: I've just spent a lot of time looking through the Python source code, so I'm familiar with where a lot of things are. – user2357112 Nov 28 '16 at 19:29
  • Respect. At least I know there isn't some tool I'm missing out on (maybe). –  Nov 28 '16 at 19:29
-2

I can't try it right now, but python default ide is able to open core modules easily (I tried with math and some more)

https://docs.python.org/2/library/idle.html

On menus. Open module.

David P.
  • 125
  • 9
  • 1
    Could you give a example of what you mean? – Christian Dean Nov 28 '16 at 19:33
  • I don't understand: "I can try it right now" (try what?), "but" (are you sure you mean but?), "open core modules easily" (yes I bet it can, but how? what line of code?) –  Nov 28 '16 at 19:38
  • I'm writing on the phone, and sadly I can't use idle here :) I edited to clarify what I mean. – David P. Nov 28 '16 at 19:39
  • Ah IDLE, well I probably have IDLE installed (I use sublime) but it would really be nice to do this without a GUI and I'm guessing it would just give me a huge list to choose from. Because my sys.path is quite huge. –  Nov 28 '16 at 19:41
  • Does this display the real source code? I've seen [something similar](http://stackoverflow.com/questions/38384206/why-some-python-build-in-functions-only-have-pass) in PyCharm that just fabricates code that kind of looks like it could have been the source code. – user2357112 Nov 28 '16 at 20:16
  • Now I have doubts, but probably you are right, idle shouldn't handle c file's – David P. Nov 28 '16 at 23:42