Skip to content

Commit

Permalink
pythongh-114328: tty cbreak mode should not alter ICRNL.
Browse files Browse the repository at this point in the history
CR -> NL mapping should be inherited in cbreak mode as OSes do not
specify altering it as part of their `stty` cbreak mode definition.
  • Loading branch information
gpshead committed Jan 19, 2024
1 parent 681e9e8 commit 0302768
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
8 changes: 8 additions & 0 deletions Doc/library/tty.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ The :mod:`tty` module defines the following functions:

.. versionadded:: 3.12

.. versionchanged:: 3.12.2
The ``ICRNL`` flag is no longer cleared.


.. function:: setraw(fd, when=termios.TCSAFLUSH)

Expand All @@ -59,6 +62,11 @@ The :mod:`tty` module defines the following functions:
.. 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 matches both the behavior
of 3.11 and earlier as well as what Linux, macOS, and BSDs describe in
their ``stty(1)`` man pages regarding cbreak mode.


.. seealso::

Expand Down
12 changes: 11 additions & 1 deletion Lib/test/test_tty.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ def setUp(self):
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)
Expand Down Expand Up @@ -56,6 +55,14 @@ def test_cfmakecbreak(self):
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)
Expand All @@ -74,6 +81,9 @@ def test_setcbreak(self):
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)
Expand Down
3 changes: 0 additions & 3 deletions Lib/tty.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 0302768

Please sign in to comment.