Python 3.11+Linux · macOSosfunction
os.login_tty
os — pseudo-terminals (pty)
Make a file descriptor the controlling terminal of the process. Availability: Unix.
Syntax
os.login_tty(...)
It makes the given descriptor the current process's controlling terminal and standard streams: it creates a new session and redirects 0, 1 and 2 to it. It is called in a child after fork. The original descriptor is closed. Added in Python 3.11.
Examples
import os
master, slave = os.openpty() # только Unix: пара дескрипторов псевдотерминала
pid = os.fork() # только Unix: 0 в потомке, pid потомка в родителе
if pid == 0: os.login_tty(slave); print('hello'); os._exit(0) # → ? только Unix: потомок стал лидером сессии, slave стал его управляющим терминалом и stdin/stdout/stderr (и сам дескриптор закрыт), печать уходит в псевдотерминал
print(os.read(master, 1024)) # → b'hello\r\n' — родитель читает вывод потомка, терминал заменяет \n на \r\n
r, w = os.pipe() # обычный канал, не терминал
print(os.login_tty(r)) # → OSError — годится только дескриптор терминала
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy