Python 获取 ttyname 时崩溃

Python crashes when getting ttyname

我正在 Python、Mac OS 中创建伪终端对,然后尝试获取它们的名称。

import os
import pty

master, slave = pty.openpty()

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

我可以获取从机,但 Python 在尝试获取主机时崩溃。

$ 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

为什么会这样?

pty.openpty 呼叫 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.

master端只用文件描述符表示,slave端用/dev中的文件表示。