0
0
mirror of https://github.com/python/cpython.git synced 2024-11-21 12:59:38 +01:00

gh-114328: tty cbreak mode should not alter ICRNL (#114335)

The terminal CR -> NL mapping setting should be inherited in cbreak mode as OSes do not specify altering it as part of their stty cbreak mode definition.
This commit is contained in:
Gregory P. Smith 2024-01-21 15:25:52 -08:00 committed by GitHub
parent db1c18eb62
commit fd49e22670
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 30 additions and 4 deletions

View File

@ -35,8 +35,15 @@ The :mod:`tty` module defines the following functions:
Convert the tty attribute list *mode*, which is a list like the one returned
by :func:`termios.tcgetattr`, to that of a tty in cbreak mode.
This clears the ``ECHO`` and ``ICANON`` local mode flags in *mode* as well
as setting the minimum input to 1 byte with no delay.
.. versionadded:: 3.12
.. versionchanged:: 3.12.2
The ``ICRNL`` flag is no longer cleared. This matches Linux and macOS
``stty cbreak`` behavior and what :func:`setcbreak` historically did.
.. function:: setraw(fd, when=termios.TCSAFLUSH)
@ -56,9 +63,17 @@ The :mod:`tty` module defines the following functions:
:func:`termios.tcsetattr`. The return value of :func:`termios.tcgetattr`
is saved before setting *fd* to cbreak mode; this value is returned.
This clears the ``ECHO`` and ``ICANON`` local mode flags as well as setting
the minimum input to 1 byte with no delay.
.. versionchanged:: 3.12
The return value is now the original tty attributes, instead of None.
.. versionchanged:: 3.12.2
The ``ICRNL`` flag is no longer cleared. This restores the behavior
of Python 3.11 and earlier as well as matching what Linux, macOS, & BSDs
describe in their ``stty(1)`` man pages regarding cbreak mode.
.. seealso::

View File

@ -19,7 +19,6 @@ class TestTty(unittest.TestCase):
self.addCleanup(termios.tcsetattr, self.fd, termios.TCSAFLUSH, self.mode)
def check_cbreak(self, mode):
self.assertEqual(mode[0] & termios.ICRNL, 0)
self.assertEqual(mode[3] & termios.ECHO, 0)
self.assertEqual(mode[3] & termios.ICANON, 0)
self.assertEqual(mode[6][termios.VMIN], 1)
@ -56,6 +55,14 @@ class TestTty(unittest.TestCase):
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)
@ -74,6 +81,9 @@ class TestTty(unittest.TestCase):
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)

View File

@ -45,9 +45,6 @@ def cfmakeraw(mode):
def cfmakecbreak(mode):
"""Make termios mode cbreak."""
# Do not map CR to NL on input.
mode[IFLAG] &= ~(ICRNL)
# Do not echo characters; disable canonical input.
mode[LFLAG] &= ~(ECHO | ICANON)

View File

@ -0,0 +1,4 @@
The :func:`tty.setcbreak` and new :func:`tty.cfmakecbreak` no longer clears
the terminal input ICRLF flag. This fixes a regression introduced in 3.12
that no longer matched how OSes define cbreak mode in their ``stty(1)``
manual pages.