Skip to content

Commit d37d318

Browse files
committed
fix: address comments and add tests
Signed-off-by: yihong0618 <zouzou0208@gmail.com>
1 parent 5ff6b9b commit d37d318

2 files changed

Lines changed: 55 additions & 11 deletions

File tree

Lib/_pyrepl/unix_console.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -340,10 +340,6 @@ def prepare(self):
340340
tcsetattr(self.input_fd, termios.TCSADRAIN, raw)
341341
except termios.error as e:
342342
if e.args[0] != errno.EIO:
343-
# gh-135329
344-
# When running under external programs (like strace),
345-
# tcsetattr may fail with EIO. We can safely ignore this
346-
# and continue with default terminal settings.
347343
raise
348344

349345
# In macOS terminal we need to deactivate line wrap via ANSI escape code
@@ -380,8 +376,7 @@ def restore(self):
380376
tcsetattr(self.input_fd, termios.TCSADRAIN, self.__svtermstate)
381377
except termios.error as e:
382378
if e.args[0] != errno.EIO:
383-
# gh-135329
384-
# When running under external programs (like strace),
379+
# gh-135329: when running under external programs (like strace),
385380
# tcsetattr may fail with EIO. We can safely ignore this
386381
# and continue with default terminal settings.
387382
raise
@@ -424,10 +419,6 @@ def get_event(self, block: bool = True) -> Event | None:
424419
else:
425420
continue
426421
elif err.errno == errno.EIO:
427-
# gh-135329
428-
# When running under external programs (like strace),
429-
# os.read may fail with EIO. In this case, we should
430-
# exit gracefully to avoid infinite error loops.
431422
import sys
432423
sys.exit(errno.EIO)
433424
else:

Lib/test/test_pyrepl/test_unix_console.py

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
import os
33
import sys
44
import unittest
5+
import errno
6+
import termios
57
from functools import partial
68
from test.support import os_helper, force_not_colorized_test_class
79

810
from unittest import TestCase
9-
from unittest.mock import MagicMock, call, patch, ANY
11+
from unittest.mock import MagicMock, call, patch, ANY, Mock
1012

1113
from .support import handle_all_events, code_to_events
1214

@@ -303,3 +305,54 @@ def test_getheightwidth_with_invalid_environ(self, _os_write):
303305
self.assertIsInstance(console.getheightwidth(), tuple)
304306
os.environ = []
305307
self.assertIsInstance(console.getheightwidth(), tuple)
308+
309+
310+
class TestUnixConsoleEIOHandling(TestCase):
311+
312+
@patch('_pyrepl.unix_console.tcsetattr')
313+
@patch('_pyrepl.unix_console.tcgetattr')
314+
def test_eio_error_handling_in_prepare(self, mock_tcgetattr, mock_tcsetattr):
315+
mock_termios = Mock()
316+
mock_termios.iflag = 0
317+
mock_termios.oflag = 0
318+
mock_termios.cflag = 0
319+
mock_termios.lflag = 0
320+
mock_termios.cc = [0] * 32
321+
mock_termios.copy.return_value = mock_termios
322+
mock_tcgetattr.return_value = mock_termios
323+
324+
mock_tcsetattr.side_effect = termios.error(errno.EIO, "Input/output error")
325+
326+
console = UnixConsole(term="xterm")
327+
328+
try:
329+
console.prepare()
330+
except termios.error as e:
331+
if e.args[0] == errno.EIO:
332+
self.fail("EIO error should have been handled gracefully in prepare()")
333+
raise
334+
335+
@patch('_pyrepl.unix_console.tcsetattr')
336+
@patch('_pyrepl.unix_console.tcgetattr')
337+
def test_eio_error_handling_in_restore(self, mock_tcgetattr, mock_tcsetattr):
338+
339+
mock_termios = Mock()
340+
mock_termios.iflag = 0
341+
mock_termios.oflag = 0
342+
mock_termios.cflag = 0
343+
mock_termios.lflag = 0
344+
mock_termios.cc = [0] * 32
345+
mock_termios.copy.return_value = mock_termios
346+
mock_tcgetattr.return_value = mock_termios
347+
348+
console = UnixConsole(term="xterm")
349+
console.prepare()
350+
351+
mock_tcsetattr.side_effect = termios.error(errno.EIO, "Input/output error")
352+
353+
try:
354+
console.restore()
355+
except termios.error as e:
356+
if e.args[0] == errno.EIO:
357+
self.fail("EIO error should have been handled gracefully in restore()")
358+
raise

0 commit comments

Comments
 (0)