Debian Codegraph @

Lib/test/test_pty.py

raw
1 import unittest
2 from test.support import (
3 is_android, is_apple_mobile, is_wasm32, reap_children, verbose
4 )
5 from test.support.import_helper import import_module
6
7 # Skip these tests if termios is not available
8 import_module('termios')
9
10 if is_android or is_apple_mobile or is_wasm32:
11 raise unittest.SkipTest("pty is not available on this platform")
12
13 import errno
14 import os
15 import pty
16 import tty
17 import sys
18 import select
19 import signal
20 import socket
21 import io # readline
22 import warnings
23
24 TEST_STRING_1 = b"I wish to buy a fish license.\n"
25 TEST_STRING_2 = b"For my pet fish, Eric.\n"
26
27 _HAVE_WINSZ = hasattr(tty, "TIOCGWINSZ") and hasattr(tty, "TIOCSWINSZ")
28
29 if verbose:
30 def debug(msg):
31 print(msg)
32 else:
33 def debug(msg):
34 pass
35
36
37 # Note that os.read() is nondeterministic so we need to be very careful
38 # to make the test suite deterministic. A normal call to os.read() may
39 # give us less than expected.
40 #
41 # Beware, on my Linux system, if I put 'foo\n' into a terminal fd, I get
42 # back 'foo\r\n' at the other end. The behavior depends on the termios
43 # setting. The newline translation may be OS-specific. To make the
44 # test suite deterministic and OS-independent, the functions _readline
45 # and normalize_output can be used.
46
47 def normalize_output(data):
48 # Some operating systems do conversions on newline. We could possibly fix
49 # that by doing the appropriate termios.tcsetattr()s. I couldn't figure out
50 # the right combo on Tru64. So, just normalize the output and doc the
51 # problem O/Ses by allowing certain combinations for some platforms, but
52 # avoid allowing other differences (like extra whitespace, trailing garbage,
53 # etc.)
54
55 # This is about the best we can do without getting some feedback
56 # from someone more knowledgable.
57
58 # OSF/1 (Tru64) apparently turns \n into \r\r\n.
59 if data.endswith(b'\r\r\n'):
60 return data.replace(b'\r\r\n', b'\n')
61
62 if data.endswith(b'\r\n'):
63 return data.replace(b'\r\n', b'\n')
64
65 return data
66
67 def _readline(fd):
68 """Read one line. May block forever if no newline is read."""
69 reader = io.FileIO(fd, mode='rb', closefd=False)
70 return reader.readline()
71
72 def expectedFailureIfStdinIsTTY(fun):
73 # avoid isatty()
74 try:
75 tty.tcgetattr(pty.STDIN_FILENO)
76 return unittest.expectedFailure(fun)
77 except tty.error:
78 pass
79 return fun
80
81
82 def write_all(fd, data):
83 written = os.write(fd, data)
84 if written != len(data):
85 # gh-73256, gh-110673: It should never happen, but check just in case
86 raise Exception(f"short write: os.write({fd}, {len(data)} bytes) "
87 f"wrote {written} bytes")
88
89
90 # Marginal testing of pty suite. Cannot do extensive 'do or fail' testing
91 # because pty code is not too portable.
92 class PtyTest(unittest.TestCase):
93 def setUp(self):
94 old_sighup = signal.signal(signal.SIGHUP, self.handle_sighup)
95 self.addCleanup(signal.signal, signal.SIGHUP, old_sighup)
96
97 # Save original stdin window size.
98 self.stdin_dim = None
99 if _HAVE_WINSZ:
100 try:
101 self.stdin_dim = tty.tcgetwinsize(pty.STDIN_FILENO)
102 self.addCleanup(tty.tcsetwinsize, pty.STDIN_FILENO,
103 self.stdin_dim)
104 except tty.error:
105 pass
106
107 @staticmethod
108 def handle_sighup(signum, frame):
109 pass
110
111 @expectedFailureIfStdinIsTTY
112 def test_openpty(self):
113 try:
114 mode = tty.tcgetattr(pty.STDIN_FILENO)
115 except tty.error:
116 # Not a tty or bad/closed fd.
117 debug("tty.tcgetattr(pty.STDIN_FILENO) failed")
118 mode = None
119
120 new_dim = None
121 if self.stdin_dim:
122 try:
123 # Modify pty.STDIN_FILENO window size; we need to
124 # check if pty.openpty() is able to set pty slave
125 # window size accordingly.
126 debug("Setting pty.STDIN_FILENO window size.")
127 debug(f"original size: (row, col) = {self.stdin_dim}")
128 target_dim = (self.stdin_dim[0] + 1, self.stdin_dim[1] + 1)
129 debug(f"target size: (row, col) = {target_dim}")
130 tty.tcsetwinsize(pty.STDIN_FILENO, target_dim)
131
132 # Were we able to set the window size
133 # of pty.STDIN_FILENO successfully?
134 new_dim = tty.tcgetwinsize(pty.STDIN_FILENO)
135 self.assertEqual(new_dim, target_dim,
136 "pty.STDIN_FILENO window size unchanged")
137 except OSError as e:
138 logging.getLogger(__name__).warning(
139 "Failed to set pty.STDIN_FILENO window size.", exc_info=e,
140 )
141 pass
142
143 try:
144 debug("Calling pty.openpty()")
145 try:
146 master_fd, slave_fd, slave_name = pty.openpty(mode, new_dim,
147 True)
148 except TypeError:
149 master_fd, slave_fd = pty.openpty()
150 slave_name = None
151 debug(f"Got {master_fd=}, {slave_fd=}, {slave_name=}")
152 except OSError:
153 # " An optional feature could not be imported " ... ?
154 raise unittest.SkipTest("Pseudo-terminals (seemingly) not functional.")
155
156 # closing master_fd can raise a SIGHUP if the process is
157 # the session leader: we installed a SIGHUP signal handler
158 # to ignore this signal.
159 self.addCleanup(os.close, master_fd)
160 self.addCleanup(os.close, slave_fd)
161
162 self.assertTrue(os.isatty(slave_fd), "slave_fd is not a tty")
163
164 if mode:
165 self.assertEqual(tty.tcgetattr(slave_fd), mode,
166 "openpty() failed to set slave termios")
167 if new_dim:
168 self.assertEqual(tty.tcgetwinsize(slave_fd), new_dim,
169 "openpty() failed to set slave window size")
170
171 # Ensure the fd is non-blocking in case there's nothing to read.
172 blocking = os.get_blocking(master_fd)
173 try:
174 os.set_blocking(master_fd, False)
175 try:
176 s1 = os.read(master_fd, 1024)
177 self.assertEqual(b'', s1)
178 except OSError as e:
179 if e.errno != errno.EAGAIN:
180 raise
181 finally:
182 # Restore the original flags.
183 os.set_blocking(master_fd, blocking)
184
185 debug("Writing to slave_fd")
186 write_all(slave_fd, TEST_STRING_1)
187 s1 = _readline(master_fd)
188 self.assertEqual(b'I wish to buy a fish license.\n',
189 normalize_output(s1))
190
191 debug("Writing chunked output")
192 write_all(slave_fd, TEST_STRING_2[:5])
193 write_all(slave_fd, TEST_STRING_2[5:])
194 s2 = _readline(master_fd)
195 self.assertEqual(b'For my pet fish, Eric.\n', normalize_output(s2))
196
197 def test_fork(self):
198 debug("calling pty.fork()")
199 pid, master_fd = pty.fork()
200 self.addCleanup(os.close, master_fd)
201 if pid == pty.CHILD:
202 # stdout should be connected to a tty.
203 if not os.isatty(1):
204 debug("Child's fd 1 is not a tty?!")
205 os._exit(3)
206
207 # After pty.fork(), the child should already be a session leader.
208 # (on those systems that have that concept.)
209 debug("In child, calling os.setsid()")
210 try:
211 os.setsid()
212 except OSError:
213 # Good, we already were session leader
214 debug("Good: OSError was raised.")
215 pass
216 except AttributeError:
217 # Have pty, but not setsid()?
218 debug("No setsid() available?")
219 pass
220 except:
221 # We don't want this error to propagate, escaping the call to
222 # os._exit() and causing very peculiar behavior in the calling
223 # regrtest.py !
224 # Note: could add traceback printing here.
225 debug("An unexpected error was raised.")
226 os._exit(1)
227 else:
228 debug("os.setsid() succeeded! (bad!)")
229 os._exit(2)
230 os._exit(4)
231 else:
232 debug("Waiting for child (%d) to finish." % pid)
233 # In verbose mode, we have to consume the debug output from the
234 # child or the child will block, causing this test to hang in the
235 # parent's waitpid() call. The child blocks after a
236 # platform-dependent amount of data is written to its fd. On
237 # Linux 2.6, it's 4000 bytes and the child won't block, but on OS
238 # X even the small writes in the child above will block it. Also
239 # on Linux, the read() will raise an OSError (input/output error)
240 # when it tries to read past the end of the buffer but the child's
241 # already exited, so catch and discard those exceptions. It's not
242 # worth checking for EIO.
243 while True:
244 try:
245 data = os.read(master_fd, 80)
246 except OSError:
247 break
248 if not data:
249 break
250 sys.stdout.write(str(data.replace(b'\r\n', b'\n'),
251 encoding='ascii'))
252
253 ##line = os.read(master_fd, 80)
254 ##lines = line.replace('\r\n', '\n').split('\n')
255 ##if False and lines != ['In child, calling os.setsid()',
256 ## 'Good: OSError was raised.', '']:
257 ## raise TestFailed("Unexpected output from child: %r" % line)
258
259 (pid, status) = os.waitpid(pid, 0)
260 res = os.waitstatus_to_exitcode(status)
261 debug("Child (%d) exited with code %d (status %d)." % (pid, res, status))
262 if res == 1:
263 self.fail("Child raised an unexpected exception in os.setsid()")
264 elif res == 2:
265 self.fail("pty.fork() failed to make child a session leader.")
266 elif res == 3:
267 self.fail("Child spawned by pty.fork() did not have a tty as stdout")
268 elif res != 4:
269 self.fail("pty.fork() failed for unknown reasons.")
270
271 ##debug("Reading from master_fd now that the child has exited")
272 ##try:
273 ## s1 = os.read(master_fd, 1024)
274 ##except OSError:
275 ## pass
276 ##else:
277 ## raise TestFailed("Read from master_fd did not raise exception")
278
279 def test_master_read(self):
280 # XXX(nnorwitz): this test leaks fds when there is an error.
281 debug("Calling pty.openpty()")
282 master_fd, slave_fd = pty.openpty()
283 debug(f"Got master_fd '{master_fd}', slave_fd '{slave_fd}'")
284
285 self.addCleanup(os.close, master_fd)
286
287 debug("Closing slave_fd")
288 os.close(slave_fd)
289
290 debug("Reading from master_fd")
291 try:
292 data = os.read(master_fd, 1)
293 except OSError: # Linux
294 data = b""
295
296 self.assertEqual(data, b"")
297
298 def test_spawn_doesnt_hang(self):
299 # gh-140482: Do the test in a pty.fork() child to avoid messing
300 # with the interactive test runner's terminal settings.
301 pid, fd = pty.fork()
302 if pid == pty.CHILD:
303 pty.spawn([sys.executable, '-c', 'print("hi there")'])
304 os._exit(0)
305
306 try:
307 buf = bytearray()
308 try:
309 while (data := os.read(fd, 1024)) != b'':
310 buf.extend(data)
311 except OSError as e:
312 if e.errno != errno.EIO:
313 raise
314
315 (pid, status) = os.waitpid(pid, 0)
316 self.assertEqual(status, 0)
317 self.assertEqual(bytes(buf), b"hi there\r\n")
318 finally:
319 os.close(fd)
320
321 class SmallPtyTests(unittest.TestCase):
322 """These tests don't spawn children or hang."""
323
324 def setUp(self):
325 self.orig_stdin_fileno = pty.STDIN_FILENO
326 self.orig_stdout_fileno = pty.STDOUT_FILENO
327 self.orig_pty_close = pty.close
328 self.orig_pty__copy = pty._copy
329 self.orig_pty_fork = pty.fork
330 self.orig_pty_select = pty.select
331 self.orig_pty_setraw = pty.setraw
332 self.orig_pty_tcgetattr = pty.tcgetattr
333 self.orig_pty_tcsetattr = pty.tcsetattr
334 self.orig_pty_waitpid = pty.waitpid
335 self.fds = [] # A list of file descriptors to close.
336 self.files = []
337 self.select_input = []
338 self.select_output = []
339 self.tcsetattr_mode_setting = None
340
341 def tearDown(self):
342 pty.STDIN_FILENO = self.orig_stdin_fileno
343 pty.STDOUT_FILENO = self.orig_stdout_fileno
344 pty.close = self.orig_pty_close
345 pty._copy = self.orig_pty__copy
346 pty.fork = self.orig_pty_fork
347 pty.select = self.orig_pty_select
348 pty.setraw = self.orig_pty_setraw
349 pty.tcgetattr = self.orig_pty_tcgetattr
350 pty.tcsetattr = self.orig_pty_tcsetattr
351 pty.waitpid = self.orig_pty_waitpid
352 for file in self.files:
353 try:
354 file.close()
355 except OSError:
356 pass
357 for fd in self.fds:
358 try:
359 os.close(fd)
360 except OSError:
361 pass
362
363 def _pipe(self):
364 pipe_fds = os.pipe()
365 self.fds.extend(pipe_fds)
366 return pipe_fds
367
368 def _socketpair(self):
369 socketpair = socket.socketpair()
370 self.files.extend(socketpair)
371 return socketpair
372
373 def _mock_select(self, rfds, wfds, xfds):
374 # This will raise IndexError when no more expected calls exist.
375 self.assertEqual((rfds, wfds, xfds), self.select_input.pop(0))
376 return self.select_output.pop(0)
377
378 def _make_mock_fork(self, pid):
379 def mock_fork():
380 return (pid, 12)
381 return mock_fork
382
383 def _mock_tcsetattr(self, fileno, opt, mode):
384 self.tcsetattr_mode_setting = mode
385
386 def test__copy_to_each(self):
387 """Test the normal data case on both master_fd and stdin."""
388 read_from_stdout_fd, mock_stdout_fd = self._pipe()
389 pty.STDOUT_FILENO = mock_stdout_fd
390 mock_stdin_fd, write_to_stdin_fd = self._pipe()
391 pty.STDIN_FILENO = mock_stdin_fd
392 socketpair = self._socketpair()
393 masters = [s.fileno() for s in socketpair]
394
395 # Feed data. Smaller than PIPEBUF. These writes will not block.
396 write_all(masters[1], b'from master')
397 write_all(write_to_stdin_fd, b'from stdin')
398
399 # Expect three select calls, the last one will cause IndexError
400 pty.select = self._mock_select
401 self.select_input.append(([mock_stdin_fd, masters[0]], [], []))
402 self.select_output.append(([mock_stdin_fd, masters[0]], [], []))
403 self.select_input.append(([mock_stdin_fd, masters[0]], [mock_stdout_fd, masters[0]], []))
404 self.select_output.append(([], [mock_stdout_fd, masters[0]], []))
405 self.select_input.append(([mock_stdin_fd, masters[0]], [], []))
406
407 with self.assertRaises(IndexError):
408 pty._copy(masters[0])
409
410 # Test that the right data went to the right places.
411 rfds = select.select([read_from_stdout_fd, masters[1]], [], [], 0)[0]
412 self.assertEqual([read_from_stdout_fd, masters[1]], rfds)
413 self.assertEqual(os.read(read_from_stdout_fd, 20), b'from master')
414 self.assertEqual(os.read(masters[1], 20), b'from stdin')
415
416 def test__restore_tty_mode_normal_return(self):
417 """Test that spawn resets the tty mode no when _copy returns normally."""
418
419 # PID 1 is returned from mocked fork to run the parent branch
420 # of code
421 pty.fork = self._make_mock_fork(1)
422
423 status_sentinel = object()
424 pty.waitpid = lambda _1, _2: [None, status_sentinel]
425 pty.close = lambda _: None
426
427 pty._copy = lambda _1, _2, _3: None
428
429 mode_sentinel = object()
430 pty.tcgetattr = lambda fd: mode_sentinel
431 pty.tcsetattr = self._mock_tcsetattr
432 pty.setraw = lambda _: None
433
434 self.assertEqual(pty.spawn([]), status_sentinel, "pty.waitpid process status not returned by pty.spawn")
435 self.assertEqual(self.tcsetattr_mode_setting, mode_sentinel, "pty.tcsetattr not called with original mode value")
436
437
438 def tearDownModule():
439 reap_children()
440
441
442 if __name__ == "__main__":
443 unittest.main()