|
2 | 2 | import os |
3 | 3 | import sys |
4 | 4 | import unittest |
| 5 | +import errno |
| 6 | +import termios |
5 | 7 | from functools import partial |
6 | 8 | from test.support import os_helper, force_not_colorized_test_class |
7 | 9 |
|
8 | 10 | from unittest import TestCase |
9 | | -from unittest.mock import MagicMock, call, patch, ANY |
| 11 | +from unittest.mock import MagicMock, call, patch, ANY, Mock |
10 | 12 |
|
11 | 13 | from .support import handle_all_events, code_to_events |
12 | 14 |
|
@@ -303,3 +305,54 @@ def test_getheightwidth_with_invalid_environ(self, _os_write): |
303 | 305 | self.assertIsInstance(console.getheightwidth(), tuple) |
304 | 306 | os.environ = [] |
305 | 307 | 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