annotate Lib/test/test_pty.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents f889458c65cc
children 8c2635afbfe1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
53302
de63b7e8500d Restore skips of posix and pty tests on Windows by calling the
R. David Murray <rdmurray@bitdance.com>
parents: 52355
diff changeset
1 from test.test_support import verbose, run_unittest, import_module
de63b7e8500d Restore skips of posix and pty tests on Windows by calling the
R. David Murray <rdmurray@bitdance.com>
parents: 52355
diff changeset
2
de63b7e8500d Restore skips of posix and pty tests on Windows by calling the
R. David Murray <rdmurray@bitdance.com>
parents: 52355
diff changeset
3 #Skip these tests if either fcntl or termios is not available
de63b7e8500d Restore skips of posix and pty tests on Windows by calling the
R. David Murray <rdmurray@bitdance.com>
parents: 52355
diff changeset
4 fcntl = import_module('fcntl')
de63b7e8500d Restore skips of posix and pty tests on Windows by calling the
R. David Murray <rdmurray@bitdance.com>
parents: 52355
diff changeset
5 import_module('termios')
de63b7e8500d Restore skips of posix and pty tests on Windows by calling the
R. David Murray <rdmurray@bitdance.com>
parents: 52355
diff changeset
6
41550
938c5ae8dc31 This gets the test working on Solaris. It seems a little hokey to me,
Neal Norwitz <nnorwitz@gmail.com>
parents: 41456
diff changeset
7 import errno
41058
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
8 import pty
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
9 import os
41456
534e4c707e91 Port r54805 from python25-maint branch:
Barry Warsaw <barry@python.org>
parents: 41133
diff changeset
10 import sys
74979
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
11 import select
41058
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
12 import signal
74979
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
13 import socket
41058
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
14 import unittest
13254
ff0974e8821b Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
15
17138
7243deb020e8 Allow the process of reading back what we wrote to a pty to transform
Thomas Wouters <thomas@python.org>
parents: 16518
diff changeset
16 TEST_STRING_1 = "I wish to buy a fish license.\n"
7243deb020e8 Allow the process of reading back what we wrote to a pty to transform
Thomas Wouters <thomas@python.org>
parents: 16518
diff changeset
17 TEST_STRING_2 = "For my pet fish, Eric.\n"
13254
ff0974e8821b Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
18
ff0974e8821b Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
19 if verbose:
ff0974e8821b Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
20 def debug(msg):
ff0974e8821b Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
21 print msg
ff0974e8821b Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
22 else:
ff0974e8821b Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
23 def debug(msg):
ff0974e8821b Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
24 pass
ff0974e8821b Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
25
41058
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
26
37415
09b8e5a12146 Fix test_pty on OSF/1 (Tru64). The problem is that the newline gets
Neal Norwitz <nnorwitz@gmail.com>
parents: 36436
diff changeset
27 def normalize_output(data):
09b8e5a12146 Fix test_pty on OSF/1 (Tru64). The problem is that the newline gets
Neal Norwitz <nnorwitz@gmail.com>
parents: 36436
diff changeset
28 # Some operating systems do conversions on newline. We could possibly
09b8e5a12146 Fix test_pty on OSF/1 (Tru64). The problem is that the newline gets
Neal Norwitz <nnorwitz@gmail.com>
parents: 36436
diff changeset
29 # fix that by doing the appropriate termios.tcsetattr()s. I couldn't
09b8e5a12146 Fix test_pty on OSF/1 (Tru64). The problem is that the newline gets
Neal Norwitz <nnorwitz@gmail.com>
parents: 36436
diff changeset
30 # figure out the right combo on Tru64 and I don't have an IRIX box.
37492
09990661226d whitespace normalisation
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37415
diff changeset
31 # So just normalize the output and doc the problem O/Ses by allowing
37415
09b8e5a12146 Fix test_pty on OSF/1 (Tru64). The problem is that the newline gets
Neal Norwitz <nnorwitz@gmail.com>
parents: 36436
diff changeset
32 # certain combinations for some platforms, but avoid allowing other
09b8e5a12146 Fix test_pty on OSF/1 (Tru64). The problem is that the newline gets
Neal Norwitz <nnorwitz@gmail.com>
parents: 36436
diff changeset
33 # differences (like extra whitespace, trailing garbage, etc.)
09b8e5a12146 Fix test_pty on OSF/1 (Tru64). The problem is that the newline gets
Neal Norwitz <nnorwitz@gmail.com>
parents: 36436
diff changeset
34
09b8e5a12146 Fix test_pty on OSF/1 (Tru64). The problem is that the newline gets
Neal Norwitz <nnorwitz@gmail.com>
parents: 36436
diff changeset
35 # This is about the best we can do without getting some feedback
09b8e5a12146 Fix test_pty on OSF/1 (Tru64). The problem is that the newline gets
Neal Norwitz <nnorwitz@gmail.com>
parents: 36436
diff changeset
36 # from someone more knowledgable.
09b8e5a12146 Fix test_pty on OSF/1 (Tru64). The problem is that the newline gets
Neal Norwitz <nnorwitz@gmail.com>
parents: 36436
diff changeset
37
09b8e5a12146 Fix test_pty on OSF/1 (Tru64). The problem is that the newline gets
Neal Norwitz <nnorwitz@gmail.com>
parents: 36436
diff changeset
38 # OSF/1 (Tru64) apparently turns \n into \r\r\n.
09b8e5a12146 Fix test_pty on OSF/1 (Tru64). The problem is that the newline gets
Neal Norwitz <nnorwitz@gmail.com>
parents: 36436
diff changeset
39 if data.endswith('\r\r\n'):
37988
8839ea93b6d7 Fix this test on Solaris. There can be embedded \r, so don't just replace
Neal Norwitz <nnorwitz@gmail.com>
parents: 37977
diff changeset
40 return data.replace('\r\r\n', '\n')
37415
09b8e5a12146 Fix test_pty on OSF/1 (Tru64). The problem is that the newline gets
Neal Norwitz <nnorwitz@gmail.com>
parents: 36436
diff changeset
41
09b8e5a12146 Fix test_pty on OSF/1 (Tru64). The problem is that the newline gets
Neal Norwitz <nnorwitz@gmail.com>
parents: 36436
diff changeset
42 # IRIX apparently turns \n into \r\n.
09b8e5a12146 Fix test_pty on OSF/1 (Tru64). The problem is that the newline gets
Neal Norwitz <nnorwitz@gmail.com>
parents: 36436
diff changeset
43 if data.endswith('\r\n'):
37988
8839ea93b6d7 Fix this test on Solaris. There can be embedded \r, so don't just replace
Neal Norwitz <nnorwitz@gmail.com>
parents: 37977
diff changeset
44 return data.replace('\r\n', '\n')
37415
09b8e5a12146 Fix test_pty on OSF/1 (Tru64). The problem is that the newline gets
Neal Norwitz <nnorwitz@gmail.com>
parents: 36436
diff changeset
45
09b8e5a12146 Fix test_pty on OSF/1 (Tru64). The problem is that the newline gets
Neal Norwitz <nnorwitz@gmail.com>
parents: 36436
diff changeset
46 return data
09b8e5a12146 Fix test_pty on OSF/1 (Tru64). The problem is that the newline gets
Neal Norwitz <nnorwitz@gmail.com>
parents: 36436
diff changeset
47
41058
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
48
13254
ff0974e8821b Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
49 # Marginal testing of pty suite. Cannot do extensive 'do or fail' testing
ff0974e8821b Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
50 # because pty code is not too portable.
41550
938c5ae8dc31 This gets the test working on Solaris. It seems a little hokey to me,
Neal Norwitz <nnorwitz@gmail.com>
parents: 41456
diff changeset
51 # XXX(nnorwitz): these tests leak fds when there is an error.
41058
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
52 class PtyTest(unittest.TestCase):
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
53 def setUp(self):
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41058
diff changeset
54 # isatty() and close() can hang on some platforms. Set an alarm
41058
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
55 # before running the test to make sure we don't hang forever.
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
56 self.old_alarm = signal.signal(signal.SIGALRM, self.handle_sig)
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
57 signal.alarm(10)
13254
ff0974e8821b Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
58
41058
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
59 def tearDown(self):
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
60 # remove alarm, restore old alarm handler
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
61 signal.alarm(0)
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
62 signal.signal(signal.SIGALRM, self.old_alarm)
17138
7243deb020e8 Allow the process of reading back what we wrote to a pty to transform
Thomas Wouters <thomas@python.org>
parents: 16518
diff changeset
63
41058
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
64 def handle_sig(self, sig, frame):
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
65 self.fail("isatty hung")
13254
ff0974e8821b Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
66
41058
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
67 def test_basic(self):
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
68 try:
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
69 debug("Calling master_open()")
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
70 master_fd, slave_name = pty.master_open()
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
71 debug("Got master_fd '%d', slave_name '%s'" %
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
72 (master_fd, slave_name))
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
73 debug("Calling slave_open(%r)" % (slave_name,))
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
74 slave_fd = pty.slave_open(slave_name)
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
75 debug("Got slave_fd '%d'" % slave_fd)
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
76 except OSError:
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
77 # " An optional feature could not be imported " ... ?
52355
228cfb6e2900 fix incorrect auto-translation of TestSkipped -> unittest.SkipTest
Benjamin Peterson <benjamin@python.org>
parents: 52353
diff changeset
78 raise unittest.SkipTest, "Pseudo-terminals (seemingly) not functional."
13254
ff0974e8821b Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
79
41058
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
80 self.assertTrue(os.isatty(slave_fd), 'slave_fd is not a tty')
17138
7243deb020e8 Allow the process of reading back what we wrote to a pty to transform
Thomas Wouters <thomas@python.org>
parents: 16518
diff changeset
81
41550
938c5ae8dc31 This gets the test working on Solaris. It seems a little hokey to me,
Neal Norwitz <nnorwitz@gmail.com>
parents: 41456
diff changeset
82 # Solaris requires reading the fd before anything is returned.
938c5ae8dc31 This gets the test working on Solaris. It seems a little hokey to me,
Neal Norwitz <nnorwitz@gmail.com>
parents: 41456
diff changeset
83 # My guess is that since we open and close the slave fd
938c5ae8dc31 This gets the test working on Solaris. It seems a little hokey to me,
Neal Norwitz <nnorwitz@gmail.com>
parents: 41456
diff changeset
84 # in master_open(), we need to read the EOF.
938c5ae8dc31 This gets the test working on Solaris. It seems a little hokey to me,
Neal Norwitz <nnorwitz@gmail.com>
parents: 41456
diff changeset
85
938c5ae8dc31 This gets the test working on Solaris. It seems a little hokey to me,
Neal Norwitz <nnorwitz@gmail.com>
parents: 41456
diff changeset
86 # Ensure the fd is non-blocking in case there's nothing to read.
938c5ae8dc31 This gets the test working on Solaris. It seems a little hokey to me,
Neal Norwitz <nnorwitz@gmail.com>
parents: 41456
diff changeset
87 orig_flags = fcntl.fcntl(master_fd, fcntl.F_GETFL)
938c5ae8dc31 This gets the test working on Solaris. It seems a little hokey to me,
Neal Norwitz <nnorwitz@gmail.com>
parents: 41456
diff changeset
88 fcntl.fcntl(master_fd, fcntl.F_SETFL, orig_flags | os.O_NONBLOCK)
41507
f5526d2eb654 Recommit r54805:
Barry Warsaw <barry@python.org>
parents: 41468
diff changeset
89 try:
41550
938c5ae8dc31 This gets the test working on Solaris. It seems a little hokey to me,
Neal Norwitz <nnorwitz@gmail.com>
parents: 41456
diff changeset
90 s1 = os.read(master_fd, 1024)
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 53302
diff changeset
91 self.assertEqual('', s1)
41550
938c5ae8dc31 This gets the test working on Solaris. It seems a little hokey to me,
Neal Norwitz <nnorwitz@gmail.com>
parents: 41456
diff changeset
92 except OSError, e:
938c5ae8dc31 This gets the test working on Solaris. It seems a little hokey to me,
Neal Norwitz <nnorwitz@gmail.com>
parents: 41456
diff changeset
93 if e.errno != errno.EAGAIN:
938c5ae8dc31 This gets the test working on Solaris. It seems a little hokey to me,
Neal Norwitz <nnorwitz@gmail.com>
parents: 41456
diff changeset
94 raise
938c5ae8dc31 This gets the test working on Solaris. It seems a little hokey to me,
Neal Norwitz <nnorwitz@gmail.com>
parents: 41456
diff changeset
95 # Restore the original flags.
938c5ae8dc31 This gets the test working on Solaris. It seems a little hokey to me,
Neal Norwitz <nnorwitz@gmail.com>
parents: 41456
diff changeset
96 fcntl.fcntl(master_fd, fcntl.F_SETFL, orig_flags)
40736
037abca817b6 [Patch #783050 from Patrick Lynch] The emulation of forkpty() is incorrect;
Georg Brandl <georg@python.org>
parents: 37988
diff changeset
97
41058
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
98 debug("Writing to slave_fd")
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
99 os.write(slave_fd, TEST_STRING_1)
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
100 s1 = os.read(master_fd, 1024)
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 53302
diff changeset
101 self.assertEqual('I wish to buy a fish license.\n',
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 53302
diff changeset
102 normalize_output(s1))
13254
ff0974e8821b Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
103
41058
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
104 debug("Writing chunked output")
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
105 os.write(slave_fd, TEST_STRING_2[:5])
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
106 os.write(slave_fd, TEST_STRING_2[5:])
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
107 s2 = os.read(master_fd, 1024)
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 53302
diff changeset
108 self.assertEqual('For my pet fish, Eric.\n', normalize_output(s2))
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41058
diff changeset
109
41058
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
110 os.close(slave_fd)
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
111 os.close(master_fd)
40736
037abca817b6 [Patch #783050 from Patrick Lynch] The emulation of forkpty() is incorrect;
Georg Brandl <georg@python.org>
parents: 37988
diff changeset
112
037abca817b6 [Patch #783050 from Patrick Lynch] The emulation of forkpty() is incorrect;
Georg Brandl <georg@python.org>
parents: 37988
diff changeset
113
41058
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
114 def test_fork(self):
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
115 debug("calling pty.fork()")
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
116 pid, master_fd = pty.fork()
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
117 if pid == pty.CHILD:
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
118 # stdout should be connected to a tty.
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
119 if not os.isatty(1):
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
120 debug("Child's fd 1 is not a tty?!")
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
121 os._exit(3)
13254
ff0974e8821b Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
122
41058
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
123 # After pty.fork(), the child should already be a session leader.
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
124 # (on those systems that have that concept.)
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
125 debug("In child, calling os.setsid()")
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
126 try:
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
127 os.setsid()
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
128 except OSError:
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
129 # Good, we already were session leader
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
130 debug("Good: OSError was raised.")
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
131 pass
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
132 except AttributeError:
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
133 # Have pty, but not setsid()?
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
134 debug("No setsid() available?")
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
135 pass
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
136 except:
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
137 # We don't want this error to propagate, escaping the call to
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
138 # os._exit() and causing very peculiar behavior in the calling
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
139 # regrtest.py !
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
140 # Note: could add traceback printing here.
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
141 debug("An unexpected error was raised.")
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
142 os._exit(1)
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
143 else:
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
144 debug("os.setsid() succeeded! (bad!)")
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
145 os._exit(2)
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
146 os._exit(4)
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
147 else:
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
148 debug("Waiting for child (%d) to finish." % pid)
41456
534e4c707e91 Port r54805 from python25-maint branch:
Barry Warsaw <barry@python.org>
parents: 41133
diff changeset
149 # In verbose mode, we have to consume the debug output from the
534e4c707e91 Port r54805 from python25-maint branch:
Barry Warsaw <barry@python.org>
parents: 41133
diff changeset
150 # child or the child will block, causing this test to hang in the
534e4c707e91 Port r54805 from python25-maint branch:
Barry Warsaw <barry@python.org>
parents: 41133
diff changeset
151 # parent's waitpid() call. The child blocks after a
534e4c707e91 Port r54805 from python25-maint branch:
Barry Warsaw <barry@python.org>
parents: 41133
diff changeset
152 # platform-dependent amount of data is written to its fd. On
534e4c707e91 Port r54805 from python25-maint branch:
Barry Warsaw <barry@python.org>
parents: 41133
diff changeset
153 # Linux 2.6, it's 4000 bytes and the child won't block, but on OS
534e4c707e91 Port r54805 from python25-maint branch:
Barry Warsaw <barry@python.org>
parents: 41133
diff changeset
154 # X even the small writes in the child above will block it. Also
534e4c707e91 Port r54805 from python25-maint branch:
Barry Warsaw <barry@python.org>
parents: 41133
diff changeset
155 # on Linux, the read() will throw an OSError (input/output error)
534e4c707e91 Port r54805 from python25-maint branch:
Barry Warsaw <barry@python.org>
parents: 41133
diff changeset
156 # when it tries to read past the end of the buffer but the child's
534e4c707e91 Port r54805 from python25-maint branch:
Barry Warsaw <barry@python.org>
parents: 41133
diff changeset
157 # already exited, so catch and discard those exceptions. It's not
534e4c707e91 Port r54805 from python25-maint branch:
Barry Warsaw <barry@python.org>
parents: 41133
diff changeset
158 # worth checking for EIO.
534e4c707e91 Port r54805 from python25-maint branch:
Barry Warsaw <barry@python.org>
parents: 41133
diff changeset
159 while True:
534e4c707e91 Port r54805 from python25-maint branch:
Barry Warsaw <barry@python.org>
parents: 41133
diff changeset
160 try:
534e4c707e91 Port r54805 from python25-maint branch:
Barry Warsaw <barry@python.org>
parents: 41133
diff changeset
161 data = os.read(master_fd, 80)
534e4c707e91 Port r54805 from python25-maint branch:
Barry Warsaw <barry@python.org>
parents: 41133
diff changeset
162 except OSError:
534e4c707e91 Port r54805 from python25-maint branch:
Barry Warsaw <barry@python.org>
parents: 41133
diff changeset
163 break
534e4c707e91 Port r54805 from python25-maint branch:
Barry Warsaw <barry@python.org>
parents: 41133
diff changeset
164 if not data:
534e4c707e91 Port r54805 from python25-maint branch:
Barry Warsaw <barry@python.org>
parents: 41133
diff changeset
165 break
534e4c707e91 Port r54805 from python25-maint branch:
Barry Warsaw <barry@python.org>
parents: 41133
diff changeset
166 sys.stdout.write(data.replace('\r\n', '\n'))
534e4c707e91 Port r54805 from python25-maint branch:
Barry Warsaw <barry@python.org>
parents: 41133
diff changeset
167
41058
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
168 ##line = os.read(master_fd, 80)
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
169 ##lines = line.replace('\r\n', '\n').split('\n')
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
170 ##if False and lines != ['In child, calling os.setsid()',
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
171 ## 'Good: OSError was raised.', '']:
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
172 ## raise TestFailed("Unexpected output from child: %r" % line)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41058
diff changeset
173
41058
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
174 (pid, status) = os.waitpid(pid, 0)
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
175 res = status >> 8
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
176 debug("Child (%d) exited with status %d (%d)." % (pid, res, status))
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
177 if res == 1:
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
178 self.fail("Child raised an unexpected exception in os.setsid()")
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
179 elif res == 2:
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
180 self.fail("pty.fork() failed to make child a session leader.")
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
181 elif res == 3:
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
182 self.fail("Child spawned by pty.fork() did not have a tty as stdout")
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
183 elif res != 4:
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
184 self.fail("pty.fork() failed for unknown reasons.")
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41058
diff changeset
185
41058
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
186 ##debug("Reading from master_fd now that the child has exited")
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
187 ##try:
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
188 ## s1 = os.read(master_fd, 1024)
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
189 ##except os.error:
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
190 ## pass
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
191 ##else:
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
192 ## raise TestFailed("Read from master_fd did not raise exception")
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41058
diff changeset
193
41058
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
194 os.close(master_fd)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41058
diff changeset
195
41058
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
196 # pty.fork() passed.
13254
ff0974e8821b Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
197
74979
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
198
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
199 class SmallPtyTests(unittest.TestCase):
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
200 """These tests don't spawn children or hang."""
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
201
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
202 def setUp(self):
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
203 self.orig_stdin_fileno = pty.STDIN_FILENO
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
204 self.orig_stdout_fileno = pty.STDOUT_FILENO
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
205 self.orig_pty_select = pty.select
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
206 self.fds = [] # A list of file descriptors to close.
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
207 self.select_rfds_lengths = []
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
208 self.select_rfds_results = []
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
209
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
210 def tearDown(self):
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
211 pty.STDIN_FILENO = self.orig_stdin_fileno
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
212 pty.STDOUT_FILENO = self.orig_stdout_fileno
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
213 pty.select = self.orig_pty_select
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
214 for fd in self.fds:
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
215 try:
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
216 os.close(fd)
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
217 except:
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
218 pass
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
219
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
220 def _pipe(self):
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
221 pipe_fds = os.pipe()
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
222 self.fds.extend(pipe_fds)
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
223 return pipe_fds
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
224
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
225 def _mock_select(self, rfds, wfds, xfds):
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
226 # This will raise IndexError when no more expected calls exist.
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
227 self.assertEqual(self.select_rfds_lengths.pop(0), len(rfds))
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
228 return self.select_rfds_results.pop(0), [], []
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
229
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
230 def test__copy_to_each(self):
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
231 """Test the normal data case on both master_fd and stdin."""
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
232 read_from_stdout_fd, mock_stdout_fd = self._pipe()
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
233 pty.STDOUT_FILENO = mock_stdout_fd
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
234 mock_stdin_fd, write_to_stdin_fd = self._pipe()
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
235 pty.STDIN_FILENO = mock_stdin_fd
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
236 socketpair = socket.socketpair()
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
237 masters = [s.fileno() for s in socketpair]
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
238 self.fds.extend(masters)
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
239
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
240 # Feed data. Smaller than PIPEBUF. These writes will not block.
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
241 os.write(masters[1], b'from master')
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
242 os.write(write_to_stdin_fd, b'from stdin')
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
243
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
244 # Expect two select calls, the last one will cause IndexError
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
245 pty.select = self._mock_select
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
246 self.select_rfds_lengths.append(2)
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
247 self.select_rfds_results.append([mock_stdin_fd, masters[0]])
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
248 self.select_rfds_lengths.append(2)
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
249
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
250 with self.assertRaises(IndexError):
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
251 pty._copy(masters[0])
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
252
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
253 # Test that the right data went to the right places.
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
254 rfds = select.select([read_from_stdout_fd, masters[1]], [], [], 0)[0]
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
255 self.assertEqual([read_from_stdout_fd, masters[1]], rfds)
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
256 self.assertEqual(os.read(read_from_stdout_fd, 20), b'from master')
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
257 self.assertEqual(os.read(masters[1], 20), b'from stdin')
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
258
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
259 def test__copy_eof_on_all(self):
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
260 """Test the empty read EOF case on both master_fd and stdin."""
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
261 read_from_stdout_fd, mock_stdout_fd = self._pipe()
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
262 pty.STDOUT_FILENO = mock_stdout_fd
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
263 mock_stdin_fd, write_to_stdin_fd = self._pipe()
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
264 pty.STDIN_FILENO = mock_stdin_fd
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
265 socketpair = socket.socketpair()
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
266 masters = [s.fileno() for s in socketpair]
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
267 self.fds.extend(masters)
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
268
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
269 os.close(masters[1])
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
270 socketpair[1].close()
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
271 os.close(write_to_stdin_fd)
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
272
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
273 # Expect two select calls, the last one will cause IndexError
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
274 pty.select = self._mock_select
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
275 self.select_rfds_lengths.append(2)
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
276 self.select_rfds_results.append([mock_stdin_fd, masters[0]])
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
277 # We expect that both fds were removed from the fds list as they
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
278 # both encountered an EOF before the second select call.
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
279 self.select_rfds_lengths.append(0)
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
280
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
281 with self.assertRaises(IndexError):
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
282 pty._copy(masters[0])
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
283
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
284
41058
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
285 def test_main(verbose=None):
74979
f889458c65cc Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents: 68256
diff changeset
286 run_unittest(SmallPtyTests, PtyTest)
41058
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
287
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
288 if __name__ == "__main__":
3b538acf7694 Patch #1675471: convert test_pty to unittest.
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
289 test_main()