0

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?

5
  • The python source is at github.com/python. You'll find .py and .c files with the stuff you want. Commented Aug 21, 2022 at 20:52
  • It's not calledopenpty in the CPython source, and it's not implemented in Python. It's C code in posixmodule.c :: os_openpty_impl Commented Aug 21, 2022 at 20:55
  • @wkl Thanks for this. For my future reference, how would I have walked the code from os.openpty() to os_openpty_impl. How does the code know that this mapping exists? Also, oddly, despite the keyword os.openpty clearly existing in the codebase, the search function in github does not return this file as a result. Commented Aug 21, 2022 at 21:06
  • As above, some built-in functions are actually implemented in C. Many of them are wrappers on Posix/libc functions and have the same name. On linux, man openpty will give you more information. Commented Aug 21, 2022 at 21:08
  • If you want to get familiar wth CPython source - read the Python Dev Guide, especially the Internals section - some of the links in there has in-depth explanations by some contributors to the language. I'd suggest keeping a copy of the source locally, and while you're getting familiar grep through the code if you're trying to find something. Also understand that some of the code is generated (you'll see stuff like clinic in the source). Also see: docs.python.org/3/c-api/index.html Commented Aug 21, 2022 at 21:14

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.