简体   繁体   中英

How do I find the source code for pythons os.openpty() function

I am trying to better understand how to write a linux terminal emulator in python. When looking at the module source code for pty.py , it appears that pty.openpty() is a wrapper for os.openpty() .

def openpty():
"""openpty() -> (master_fd, slave_fd)
Open a pty master/slave pair, using os.openpty() if possible."""

try:
    return os.openpty()
except (AttributeError, OSError):
    pass
master_fd, slave_name = _open_terminal()
slave_fd = slave_open(slave_name)
return master_fd, 

But when I look through the os.py module source code, there is no function named openpty(). In fact, I am unable to find the source code for os.openpty() anywhere in the cpython codebase. What am I missing?

But when I look through the os.py module source code, there is no function named openpty(). In fact, I am unable to find the source code for os.openpty() anywhere in the cpython codebase. What am I missing?

You have to search for the string os.openpty in all files of the Python source distribution you should have available locally on your computer ( you will find more details and help in the comment to your question provided by wkl ).

This will give you for example with Linux grep posixmodule.c:7176:os.openpty as the result leading to:

#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
/*[clinic input]

os.openpty

Open a pseudo-terminal.

Return a tuple of (master_fd, slave_fd) containing open file descriptors
for both the master and slave ends.
[clinic start generated code]*/

static PyObject *
os_openpty_impl(PyObject *module)
/*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/

where you see that os.openpty is mapped to os_openpty_impl .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM