66

I wanted to see how a math.py function was implemented, but when I opened the file in PyCharm I found that all the functions are empty and there is a simple pass. For example:

def ceil(x): # real signature unknown; restored from __doc__
    """
    ceil(x)

    Return the ceiling of x as a float.
    This is the smallest integral value >= x.
    """
    pass

I guess it is because the functions being used are actually from the C standard library. How does it work?

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
edgarstack
  • 1,096
  • 1
  • 10
  • 18
  • 1
    `math.py`? What `math.py`? I don't think Python comes with such a file. The `math` module is implemented in [`Modules/mathmodule.c`](https://hg.python.org/cpython/file/2.7/Modules/mathmodule.c), a file that probably doesn't make its way into your Python installation. – user2357112 Jul 14 '16 at 21:06
  • 4
    Googling "Python real signature unknown" suggests that this is some sort of PyCharm functionality and PyCharm is lying to you. – user2357112 Jul 14 '16 at 21:09
  • Exactly, pycharm tells me that `math.py` exists. And its a file with all the math functions that are empty like the `ceil` I put in my question. – edgarstack Jul 14 '16 at 21:11
  • Related: [Finding the source code for built-in Python functions?](https://stackoverflow.com/q/8608587/3357935) – Stevoisiak Mar 14 '18 at 20:05

2 Answers2

86

PyCharm is lying to you. The source code you're looking at is a fake that PyCharm has created. PyCharm knows what functions should be there, and it can guess at their signatures using the function docstrings, but it has no idea what the function bodies should look like.

If you want to see the real source code, you can look at it in the official Github repository in Modules/mathmodule.c. A lot of the functions in there are macro-generated thin wrappers around C functions from math.h, but there's also a bunch of manually-written code to handle things like inconsistent or insufficient standard library implementations, functions with no math.h equivalent, and customization hooks like __ceil__.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • 40
    You can identify fake source files by the file path in the title bar. Real: ``\Python\Python\Lib\``. Fake: ``\PyCharm\system\python_stubs\`` – Stevoisiak Mar 14 '18 at 19:53
  • Since we could use them by execute them( such as the ceil function), why can't we find the source in our local envirnment? Is it because it is already binary format(if this is the case, where to find this binary file)? – Luk Aron Dec 14 '20 at 22:30
  • 1
    @LukAron: On Windows, `math` is compiled directly into the Python executable. On other platforms, you can check `math.__file__` to find the `.so` file. – user2357112 Dec 15 '20 at 02:21
  • @user2357112supportsMonica thanks so much for the reply, in windows, is there any way to find where this executable locates? – Luk Aron Dec 22 '20 at 03:07
  • 7
    but why pycharm lies? – T.M15 Mar 07 '21 at 10:53
  • 1
    is there a way to link the actual files? – MikeSchem Apr 22 '22 at 19:14
0

There are no source code written in python for some python libraries. These libraries was written on C or another languages. They are not presented by .py files and PyCharm or any another IDE cannot open their sources in readable view.

You can check sources in ...\AppData\Local\Programs\Python\Python310\Lib\site-packages. Most likely in your-package folder there will be .pui, .pud, .dll and other similiar files

fedotsoldier
  • 190
  • 1
  • 16