Skip to main content

Modules/termios.c (part 2)

Source:

cpython 3.14 @ ab2d84fe1023/Modules/termios.c

This annotation covers terminal control functions beyond attribute get/set. See modules_termios_detail for tcgetattr, tcsetattr, and the termios constants.

Map

LinesSymbolRole
1-60tcdrainWait until all output has been transmitted
61-120tcflushDiscard queued input or output
121-180tcsendbreakSend a break condition (zero bits) for a duration
181-260tcgetpgrp / tcsetpgrpGet/set foreground process group for a terminal
261-400openpty / forkptyCreate a pseudo-terminal pair

Reading

tcdrain

// CPython: Modules/termios.c:310 termios_tcdrain_impl
static PyObject *
termios_tcdrain_impl(PyObject *module, int fd)
{
/* Block until all pending output has been written to the terminal */
int ret;
Py_BEGIN_ALLOW_THREADS
ret = tcdrain(fd);
Py_END_ALLOW_THREADS
if (ret != 0)
return PyErr_SetFromErrno(PyExc_OSError);
Py_RETURN_NONE;
}

Used before closing a serial port to ensure data is fully transmitted.

tcflush

// CPython: Modules/termios.c:340 termios_tcflush_impl
static PyObject *
termios_tcflush_impl(PyObject *module, int fd, int queue)
{
/* queue: TCIFLUSH (input), TCOFLUSH (output), TCIOFLUSH (both) */
if (tcflush(fd, queue) == -1)
return PyErr_SetFromErrno(PyExc_OSError);
Py_RETURN_NONE;
}

tcgetpgrp / tcsetpgrp

// CPython: Modules/termios.c:390 termios_tcgetpgrp_impl
static PyObject *
termios_tcgetpgrp_impl(PyObject *module, int fd)
{
/* Return the foreground process group ID for the terminal */
pid_t pgid = tcgetpgrp(fd);
if (pgid < 0)
return PyErr_SetFromErrno(PyExc_OSError);
return PyLong_FromPid(pgid);
}

tcgetpgrp(0) returns the process group of the foreground job on the controlling terminal. Used by shell job control.

openpty

// CPython: Modules/termios.c:180 (in pty.py, backed by os.openpty)
/* os.openpty() opens a master/slave pseudo-terminal pair.
Returns (master_fd, slave_fd).
Used by pty.fork() and subprocess to create interactive sessions. */

openpty is actually in posixmodule.c but is part of the terminal control family exposed via os and pty.

gopy notes

termios is in module/termios/module.go. tcdrain, tcflush, tcgetpgrp, tcsetpgrp use syscall.Syscall on Linux and golang.org/x/sys/unix on macOS. openpty uses unix.Openpty. All blocking calls release the GIL equivalent in gopy (i.e. allow other goroutines to run via runtime.Gosched()).