Search code examples
pythoncpython

Where is os.stat() defined in CPython?


I've looked through the CPython sources (Hg online here), but I can't seem to find where os.stat() is defined. It looks like stat is magically a global in the os module (Lib/os.py, ca. line 139).

Can someone point me to the appropriate file(s)?


Solution

  • Looks like line 51 (for UNIX; there are similar lines for other platforms):

    from posix import *
    

    Tracing it back, we find out we reach that point through the return value of line 29:

    _names = sys.builtin_module_names
    

    And posix_stat is defined in Modules/posixmodule.c:2301, which is ultimately called by os:

    static PyObject * posix_lstat(PyObject *self, PyObject *args, PyObject *kwargs) // ...