gh-110392: Fix tty functions (GH-110642)

* tty.setraw() and tty.setcbreak() previously returned partially modified
  list of the original tty attributes. Now they return the correct list of
  the original tty attributes

* tty.cfmakeraw() and tty.cfmakecbreak() now make a copy of the list of
  special characters before modifying it.
This commit is contained in:
Serhiy Storchaka 2023-10-14 08:50:41 +03:00 committed by GitHub
parent 7284e0ef84
commit 84e2096fbd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 2 deletions

View File

@ -58,7 +58,9 @@ class TestTty(unittest.TestCase):
self.assertEqual(mode[5], self.mode[5]) self.assertEqual(mode[5], self.mode[5])
def test_setraw(self): def test_setraw(self):
mode = tty.setraw(self.fd) mode0 = termios.tcgetattr(self.fd)
mode1 = tty.setraw(self.fd)
self.assertEqual(mode1, mode0)
mode2 = termios.tcgetattr(self.fd) mode2 = termios.tcgetattr(self.fd)
self.check_raw(mode2) self.check_raw(mode2)
mode3 = tty.setraw(self.fd, termios.TCSANOW) mode3 = tty.setraw(self.fd, termios.TCSANOW)
@ -67,7 +69,9 @@ class TestTty(unittest.TestCase):
tty.setraw(fd=self.fd, when=termios.TCSANOW) tty.setraw(fd=self.fd, when=termios.TCSANOW)
def test_setcbreak(self): def test_setcbreak(self):
mode = tty.setcbreak(self.fd) mode0 = termios.tcgetattr(self.fd)
mode1 = tty.setcbreak(self.fd)
self.assertEqual(mode1, mode0)
mode2 = termios.tcgetattr(self.fd) mode2 = termios.tcgetattr(self.fd)
self.check_cbreak(mode2) self.check_cbreak(mode2)
mode3 = tty.setcbreak(self.fd, termios.TCSANOW) mode3 = tty.setcbreak(self.fd, termios.TCSANOW)

View File

@ -39,6 +39,7 @@ def cfmakeraw(mode):
# Case B: MIN>0, TIME=0 # Case B: MIN>0, TIME=0
# A pending read shall block until MIN (here 1) bytes are received, # A pending read shall block until MIN (here 1) bytes are received,
# or a signal is received. # or a signal is received.
mode[CC] = list(mode[CC])
mode[CC][VMIN] = 1 mode[CC][VMIN] = 1
mode[CC][VTIME] = 0 mode[CC][VTIME] = 0
@ -54,6 +55,7 @@ def cfmakecbreak(mode):
# Case B: MIN>0, TIME=0 # Case B: MIN>0, TIME=0
# A pending read shall block until MIN (here 1) bytes are received, # A pending read shall block until MIN (here 1) bytes are received,
# or a signal is received. # or a signal is received.
mode[CC] = list(mode[CC])
mode[CC][VMIN] = 1 mode[CC][VMIN] = 1
mode[CC][VTIME] = 0 mode[CC][VTIME] = 0

View File

@ -0,0 +1,4 @@
Fix :func:`tty.setraw` and :func:`tty.setcbreak`: previously they returned
partially modified list of the original tty attributes.
:func:`tty.cfmakeraw` and :func:`tty.cfmakecbreak` now make a copy of the
list of special characters before modifying it.