Skip to content

Commit f421909

Browse files
gpsheadaisk
authored andcommitted
pythongh-114328: tty cbreak mode should not alter ICRNL (python#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.
1 parent 491bde6 commit f421909

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

Doc/library/tty.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,15 @@ The :mod:`tty` module defines the following functions:
3535
Convert the tty attribute list *mode*, which is a list like the one returned
3636
by :func:`termios.tcgetattr`, to that of a tty in cbreak mode.
3737

38+
This clears the ``ECHO`` and ``ICANON`` local mode flags in *mode* as well
39+
as setting the minimum input to 1 byte with no delay.
40+
3841
.. versionadded:: 3.12
3942

43+
.. versionchanged:: 3.12.2
44+
The ``ICRNL`` flag is no longer cleared. This matches Linux and macOS
45+
``stty cbreak`` behavior and what :func:`setcbreak` historically did.
46+
4047

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

@@ -56,9 +63,17 @@ The :mod:`tty` module defines the following functions:
5663
:func:`termios.tcsetattr`. The return value of :func:`termios.tcgetattr`
5764
is saved before setting *fd* to cbreak mode; this value is returned.
5865

66+
This clears the ``ECHO`` and ``ICANON`` local mode flags as well as setting
67+
the minimum input to 1 byte with no delay.
68+
5969
.. versionchanged:: 3.12
6070
The return value is now the original tty attributes, instead of None.
6171

72+
.. versionchanged:: 3.12.2
73+
The ``ICRNL`` flag is no longer cleared. This restores the behavior
74+
of Python 3.11 and earlier as well as matching what Linux, macOS, & BSDs
75+
describe in their ``stty(1)`` man pages regarding cbreak mode.
76+
6277

6378
.. seealso::
6479

Lib/test/test_tty.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ def setUp(self):
1919
self.addCleanup(termios.tcsetattr, self.fd, termios.TCSAFLUSH, self.mode)
2020

2121
def check_cbreak(self, mode):
22-
self.assertEqual(mode[0] & termios.ICRNL, 0)
2322
self.assertEqual(mode[3] & termios.ECHO, 0)
2423
self.assertEqual(mode[3] & termios.ICANON, 0)
2524
self.assertEqual(mode[6][termios.VMIN], 1)
@@ -56,6 +55,14 @@ def test_cfmakecbreak(self):
5655
self.assertEqual(mode[2], self.mode[2])
5756
self.assertEqual(mode[4], self.mode[4])
5857
self.assertEqual(mode[5], self.mode[5])
58+
mode[tty.IFLAG] |= termios.ICRNL
59+
tty.cfmakecbreak(mode)
60+
self.assertEqual(mode[tty.IFLAG] & termios.ICRNL, termios.ICRNL,
61+
msg="ICRNL should not be cleared by cbreak")
62+
mode[tty.IFLAG] &= ~termios.ICRNL
63+
tty.cfmakecbreak(mode)
64+
self.assertEqual(mode[tty.IFLAG] & termios.ICRNL, 0,
65+
msg="ICRNL should not be set by cbreak")
5966

6067
def test_setraw(self):
6168
mode0 = termios.tcgetattr(self.fd)
@@ -74,6 +81,9 @@ def test_setcbreak(self):
7481
self.assertEqual(mode1, mode0)
7582
mode2 = termios.tcgetattr(self.fd)
7683
self.check_cbreak(mode2)
84+
ICRNL = termios.ICRNL
85+
self.assertEqual(mode2[tty.IFLAG] & ICRNL, mode0[tty.IFLAG] & ICRNL,
86+
msg="ICRNL should not be altered by cbreak")
7787
mode3 = tty.setcbreak(self.fd, termios.TCSANOW)
7888
self.assertEqual(mode3, mode2)
7989
tty.setcbreak(self.stream)

Lib/tty.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ def cfmakeraw(mode):
4545

4646
def cfmakecbreak(mode):
4747
"""Make termios mode cbreak."""
48-
# Do not map CR to NL on input.
49-
mode[IFLAG] &= ~(ICRNL)
50-
5148
# Do not echo characters; disable canonical input.
5249
mode[LFLAG] &= ~(ECHO | ICANON)
5350

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
The :func:`tty.setcbreak` and new :func:`tty.cfmakecbreak` no longer clears
2+
the terminal input ICRLF flag. This fixes a regression introduced in 3.12
3+
that no longer matched how OSes define cbreak mode in their ``stty(1)``
4+
manual pages.

0 commit comments

Comments
 (0)