Search code examples
pythonmacosttypty

Python crashes when getting ttyname


I'm creating a pseudo terminal pair in Python, on Mac OS, and then trying to get their names.

import os
import pty

master, slave = pty.openpty()

print(os.ttyname(slave))
print(os.ttyname(master))

I can get the slave one but Python crashes when trying to get the master one.

$ python --version
Python 3.9.10
$ python shim/tunnel.py
/dev/ttys009
Traceback (most recent call last):
  File "/Users/tamlyn/projects/shim/tunnel.py", line 7, in <module>
    print(os.ttyname(master))
OSError: [Errno 34] Result too large

Why is this happening?


Solution

  • pty.openpty calls os.openpty

    os.openpty()

    Open a new pseudo-terminal pair. Return a pair of file descriptors (master, slave) for the pty and the tty, respectively. The new file descriptors are non-inheritable. For a (slightly) more portable approach, use the pty module.

    The master end is represented by a file descriptor only, whereas the slave end is represented by a file in /dev.