How does built-in open() map to its C implementation?

Viewed 150

Trying to reason about the CPython source and was curious about the built-in open() method.

This method is defined in _pyio.py and returns a FileIO object, so I dug through source and found that (on Windows) there is a call to _wopen (source line). Interestingly enough, I stumbled into fileutils.c where _Py_open is defined and subsequently _Py_open_impl. The latter makes a call to open (source line) which has a different signature than _wopen which I presume is referencing _wfopen; however, below that there are _Py_wfopen, _Py_fopen and _Py_fopen_obj. Their comment lines seem to indicate that they are wrappers around the C functions provided from #include's, so I know they're calling the originals and extending their functionality.

I'm not a C person by any means, mostly I can dig around code for debugging. This, however, has me lost. How are all these methods tied together (on Windows)? So far I have:

open() -> io.py -> _pyio.py (_io) -> _iomodule.c -> ?

Not seeing where _Py_fopen or _Py_wfopen are called explicitly called (or used to wrap library functions) other than in main.c for startup file operations.

1 Answers

The latter makes a call to open (source line) which has a different signature than _wopen which I presume is referencing _wfopen

What you mean is not clear but the call to open is referencing the unix open(2) syscall, nothing to do with Windows.

open() -> io.py -> _pyio.py (_io) -> _iomodule.c -> ?

_iomodule.c defines _io_open_impl which instantiates PyFileIO_Type from fileio.c.

That actually opens a file in _io_FileIO___init___impl which, after some faffing around, simply calls _wopen on windows and open(2) elsewhere: https://github.com/python/cpython/blob/master/Modules/_io/fileio.c#L383

Related