Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Update tty + added the test from v3.13.10
  • Loading branch information
terryluan12 committed Jan 3, 2026
commit 4f1c2f09b62a89da1894d5d81670a98633d67113
94 changes: 94 additions & 0 deletions Lib/test/test_tty.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import os
import unittest
from test.support.import_helper import import_module

termios = import_module('termios')
tty = import_module('tty')


@unittest.skipUnless(hasattr(os, 'openpty'), "need os.openpty()")
class TestTty(unittest.TestCase):

def setUp(self):
master_fd, self.fd = os.openpty()
self.addCleanup(os.close, master_fd)
self.stream = self.enterContext(open(self.fd, 'wb', buffering=0))
self.fd = self.stream.fileno()
self.mode = termios.tcgetattr(self.fd)
self.addCleanup(termios.tcsetattr, self.fd, termios.TCSANOW, self.mode)
self.addCleanup(termios.tcsetattr, self.fd, termios.TCSAFLUSH, self.mode)

def check_cbreak(self, mode):
self.assertEqual(mode[3] & termios.ECHO, 0)
self.assertEqual(mode[3] & termios.ICANON, 0)
self.assertEqual(mode[6][termios.VMIN], 1)
self.assertEqual(mode[6][termios.VTIME], 0)

def check_raw(self, mode):
self.check_cbreak(mode)
self.assertEqual(mode[0] & termios.ISTRIP, 0)
self.assertEqual(mode[0] & termios.ICRNL, 0)
self.assertEqual(mode[1] & termios.OPOST, 0)
self.assertEqual(mode[2] & termios.PARENB, termios.CS8 & termios.PARENB)
self.assertEqual(mode[2] & termios.CSIZE, termios.CS8 & termios.CSIZE)
self.assertEqual(mode[2] & termios.CS8, termios.CS8)
self.assertEqual(mode[3] & termios.ECHO, 0)
self.assertEqual(mode[3] & termios.ICANON, 0)
self.assertEqual(mode[3] & termios.ISIG, 0)
self.assertEqual(mode[6][termios.VMIN], 1)
self.assertEqual(mode[6][termios.VTIME], 0)

def test_cfmakeraw(self):
mode = termios.tcgetattr(self.fd)
self.assertEqual(mode, self.mode)
tty.cfmakeraw(mode)
self.check_raw(mode)
self.assertEqual(mode[4], self.mode[4])
self.assertEqual(mode[5], self.mode[5])

def test_cfmakecbreak(self):
mode = termios.tcgetattr(self.fd)
self.assertEqual(mode, self.mode)
tty.cfmakecbreak(mode)
self.check_cbreak(mode)
self.assertEqual(mode[1], self.mode[1])
self.assertEqual(mode[2], self.mode[2])
self.assertEqual(mode[4], self.mode[4])
self.assertEqual(mode[5], self.mode[5])
mode[tty.IFLAG] |= termios.ICRNL
tty.cfmakecbreak(mode)
self.assertEqual(mode[tty.IFLAG] & termios.ICRNL, termios.ICRNL,
msg="ICRNL should not be cleared by cbreak")
mode[tty.IFLAG] &= ~termios.ICRNL
tty.cfmakecbreak(mode)
self.assertEqual(mode[tty.IFLAG] & termios.ICRNL, 0,
msg="ICRNL should not be set by cbreak")

def test_setraw(self):
mode0 = termios.tcgetattr(self.fd)
mode1 = tty.setraw(self.fd)
self.assertEqual(mode1, mode0)
mode2 = termios.tcgetattr(self.fd)
self.check_raw(mode2)
mode3 = tty.setraw(self.fd, termios.TCSANOW)
self.assertEqual(mode3, mode2)
tty.setraw(self.stream)
tty.setraw(fd=self.fd, when=termios.TCSANOW)

def test_setcbreak(self):
mode0 = termios.tcgetattr(self.fd)
mode1 = tty.setcbreak(self.fd)
self.assertEqual(mode1, mode0)
mode2 = termios.tcgetattr(self.fd)
self.check_cbreak(mode2)
ICRNL = termios.ICRNL
self.assertEqual(mode2[tty.IFLAG] & ICRNL, mode0[tty.IFLAG] & ICRNL,
msg="ICRNL should not be altered by cbreak")
mode3 = tty.setcbreak(self.fd, termios.TCSANOW)
self.assertEqual(mode3, mode2)
tty.setcbreak(self.stream)
tty.setcbreak(fd=self.fd, when=termios.TCSANOW)


if __name__ == '__main__':
unittest.main()
69 changes: 53 additions & 16 deletions Lib/tty.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

from termios import *

__all__ = ["setraw", "setcbreak"]
__all__ = ["cfmakeraw", "cfmakecbreak", "setraw", "setcbreak"]

# Indexes for termios list.
# Indices for termios list.
IFLAG = 0
OFLAG = 1
CFLAG = 2
Expand All @@ -15,22 +15,59 @@
OSPEED = 5
CC = 6

def setraw(fd, when=TCSAFLUSH):
"""Put terminal into a raw mode."""
mode = tcgetattr(fd)
mode[IFLAG] = mode[IFLAG] & ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON)
mode[OFLAG] = mode[OFLAG] & ~(OPOST)
mode[CFLAG] = mode[CFLAG] & ~(CSIZE | PARENB)
mode[CFLAG] = mode[CFLAG] | CS8
mode[LFLAG] = mode[LFLAG] & ~(ECHO | ICANON | IEXTEN | ISIG)
def cfmakeraw(mode):
"""Make termios mode raw."""
# Clear all POSIX.1-2017 input mode flags.
# See chapter 11 "General Terminal Interface"
# of POSIX.1-2017 Base Definitions.
mode[IFLAG] &= ~(IGNBRK | BRKINT | IGNPAR | PARMRK | INPCK | ISTRIP |
INLCR | IGNCR | ICRNL | IXON | IXANY | IXOFF)

# Do not post-process output.
mode[OFLAG] &= ~OPOST

# Disable parity generation and detection; clear character size mask;
# let character size be 8 bits.
mode[CFLAG] &= ~(PARENB | CSIZE)
mode[CFLAG] |= CS8

# Clear all POSIX.1-2017 local mode flags.
mode[LFLAG] &= ~(ECHO | ECHOE | ECHOK | ECHONL | ICANON |
IEXTEN | ISIG | NOFLSH | TOSTOP)

# POSIX.1-2017, 11.1.7 Non-Canonical Mode Input Processing,
# Case B: MIN>0, TIME=0
# A pending read shall block until MIN (here 1) bytes are received,
# or a signal is received.
mode[CC] = list(mode[CC])
mode[CC][VMIN] = 1
mode[CC][VTIME] = 0
tcsetattr(fd, when, mode)

def setcbreak(fd, when=TCSAFLUSH):
"""Put terminal into a cbreak mode."""
mode = tcgetattr(fd)
mode[LFLAG] = mode[LFLAG] & ~(ECHO | ICANON)
def cfmakecbreak(mode):
"""Make termios mode cbreak."""
# Do not echo characters; disable canonical input.
mode[LFLAG] &= ~(ECHO | ICANON)

# POSIX.1-2017, 11.1.7 Non-Canonical Mode Input Processing,
# Case B: MIN>0, TIME=0
# A pending read shall block until MIN (here 1) bytes are received,
# or a signal is received.
mode[CC] = list(mode[CC])
mode[CC][VMIN] = 1
mode[CC][VTIME] = 0
tcsetattr(fd, when, mode)

def setraw(fd, when=TCSAFLUSH):
"""Put terminal into raw mode."""
mode = tcgetattr(fd)
new = list(mode)
cfmakeraw(new)
tcsetattr(fd, when, new)
return mode

def setcbreak(fd, when=TCSAFLUSH):
"""Put terminal into cbreak mode."""
mode = tcgetattr(fd)
new = list(mode)
cfmakecbreak(new)
tcsetattr(fd, when, new)
return mode