1 | n/a | # Written to test interrupted system calls interfering with our many buffered |
---|
2 | n/a | # IO implementations. http://bugs.python.org/issue12268 |
---|
3 | n/a | # |
---|
4 | n/a | # It was suggested that this code could be merged into test_io and the tests |
---|
5 | n/a | # made to work using the same method as the existing signal tests in test_io. |
---|
6 | n/a | # I was unable to get single process tests using alarm or setitimer that way |
---|
7 | n/a | # to reproduce the EINTR problems. This process based test suite reproduces |
---|
8 | n/a | # the problems prior to the issue12268 patch reliably on Linux and OSX. |
---|
9 | n/a | # - gregory.p.smith |
---|
10 | n/a | |
---|
11 | n/a | import os |
---|
12 | n/a | import select |
---|
13 | n/a | import signal |
---|
14 | n/a | import subprocess |
---|
15 | n/a | import sys |
---|
16 | n/a | import time |
---|
17 | n/a | import unittest |
---|
18 | n/a | |
---|
19 | n/a | # Test import all of the things we're about to try testing up front. |
---|
20 | n/a | import _io |
---|
21 | n/a | import _pyio |
---|
22 | n/a | |
---|
23 | n/a | |
---|
24 | n/a | @unittest.skipUnless(os.name == 'posix', 'tests requires a posix system.') |
---|
25 | n/a | class TestFileIOSignalInterrupt: |
---|
26 | n/a | def setUp(self): |
---|
27 | n/a | self._process = None |
---|
28 | n/a | |
---|
29 | n/a | def tearDown(self): |
---|
30 | n/a | if self._process and self._process.poll() is None: |
---|
31 | n/a | try: |
---|
32 | n/a | self._process.kill() |
---|
33 | n/a | except OSError: |
---|
34 | n/a | pass |
---|
35 | n/a | |
---|
36 | n/a | def _generate_infile_setup_code(self): |
---|
37 | n/a | """Returns the infile = ... line of code for the reader process. |
---|
38 | n/a | |
---|
39 | n/a | subclasseses should override this to test different IO objects. |
---|
40 | n/a | """ |
---|
41 | n/a | return ('import %s as io ;' |
---|
42 | n/a | 'infile = io.FileIO(sys.stdin.fileno(), "rb")' % |
---|
43 | n/a | self.modname) |
---|
44 | n/a | |
---|
45 | n/a | def fail_with_process_info(self, why, stdout=b'', stderr=b'', |
---|
46 | n/a | communicate=True): |
---|
47 | n/a | """A common way to cleanup and fail with useful debug output. |
---|
48 | n/a | |
---|
49 | n/a | Kills the process if it is still running, collects remaining output |
---|
50 | n/a | and fails the test with an error message including the output. |
---|
51 | n/a | |
---|
52 | n/a | Args: |
---|
53 | n/a | why: Text to go after "Error from IO process" in the message. |
---|
54 | n/a | stdout, stderr: standard output and error from the process so |
---|
55 | n/a | far to include in the error message. |
---|
56 | n/a | communicate: bool, when True we call communicate() on the process |
---|
57 | n/a | after killing it to gather additional output. |
---|
58 | n/a | """ |
---|
59 | n/a | if self._process.poll() is None: |
---|
60 | n/a | time.sleep(0.1) # give it time to finish printing the error. |
---|
61 | n/a | try: |
---|
62 | n/a | self._process.terminate() # Ensure it dies. |
---|
63 | n/a | except OSError: |
---|
64 | n/a | pass |
---|
65 | n/a | if communicate: |
---|
66 | n/a | stdout_end, stderr_end = self._process.communicate() |
---|
67 | n/a | stdout += stdout_end |
---|
68 | n/a | stderr += stderr_end |
---|
69 | n/a | self.fail('Error from IO process %s:\nSTDOUT:\n%sSTDERR:\n%s\n' % |
---|
70 | n/a | (why, stdout.decode(), stderr.decode())) |
---|
71 | n/a | |
---|
72 | n/a | def _test_reading(self, data_to_write, read_and_verify_code): |
---|
73 | n/a | """Generic buffered read method test harness to validate EINTR behavior. |
---|
74 | n/a | |
---|
75 | n/a | Also validates that Python signal handlers are run during the read. |
---|
76 | n/a | |
---|
77 | n/a | Args: |
---|
78 | n/a | data_to_write: String to write to the child process for reading |
---|
79 | n/a | before sending it a signal, confirming the signal was handled, |
---|
80 | n/a | writing a final newline and closing the infile pipe. |
---|
81 | n/a | read_and_verify_code: Single "line" of code to read from a file |
---|
82 | n/a | object named 'infile' and validate the result. This will be |
---|
83 | n/a | executed as part of a python subprocess fed data_to_write. |
---|
84 | n/a | """ |
---|
85 | n/a | infile_setup_code = self._generate_infile_setup_code() |
---|
86 | n/a | # Total pipe IO in this function is smaller than the minimum posix OS |
---|
87 | n/a | # pipe buffer size of 512 bytes. No writer should block. |
---|
88 | n/a | assert len(data_to_write) < 512, 'data_to_write must fit in pipe buf.' |
---|
89 | n/a | |
---|
90 | n/a | # Start a subprocess to call our read method while handling a signal. |
---|
91 | n/a | self._process = subprocess.Popen( |
---|
92 | n/a | [sys.executable, '-u', '-c', |
---|
93 | n/a | 'import signal, sys ;' |
---|
94 | n/a | 'signal.signal(signal.SIGINT, ' |
---|
95 | n/a | 'lambda s, f: sys.stderr.write("$\\n")) ;' |
---|
96 | n/a | + infile_setup_code + ' ;' + |
---|
97 | n/a | 'sys.stderr.write("Worm Sign!\\n") ;' |
---|
98 | n/a | + read_and_verify_code + ' ;' + |
---|
99 | n/a | 'infile.close()' |
---|
100 | n/a | ], |
---|
101 | n/a | stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
---|
102 | n/a | stderr=subprocess.PIPE) |
---|
103 | n/a | |
---|
104 | n/a | # Wait for the signal handler to be installed. |
---|
105 | n/a | worm_sign = self._process.stderr.read(len(b'Worm Sign!\n')) |
---|
106 | n/a | if worm_sign != b'Worm Sign!\n': # See also, Dune by Frank Herbert. |
---|
107 | n/a | self.fail_with_process_info('while awaiting a sign', |
---|
108 | n/a | stderr=worm_sign) |
---|
109 | n/a | self._process.stdin.write(data_to_write) |
---|
110 | n/a | |
---|
111 | n/a | signals_sent = 0 |
---|
112 | n/a | rlist = [] |
---|
113 | n/a | # We don't know when the read_and_verify_code in our child is actually |
---|
114 | n/a | # executing within the read system call we want to interrupt. This |
---|
115 | n/a | # loop waits for a bit before sending the first signal to increase |
---|
116 | n/a | # the likelihood of that. Implementations without correct EINTR |
---|
117 | n/a | # and signal handling usually fail this test. |
---|
118 | n/a | while not rlist: |
---|
119 | n/a | rlist, _, _ = select.select([self._process.stderr], (), (), 0.05) |
---|
120 | n/a | self._process.send_signal(signal.SIGINT) |
---|
121 | n/a | signals_sent += 1 |
---|
122 | n/a | if signals_sent > 200: |
---|
123 | n/a | self._process.kill() |
---|
124 | n/a | self.fail('reader process failed to handle our signals.') |
---|
125 | n/a | # This assumes anything unexpected that writes to stderr will also |
---|
126 | n/a | # write a newline. That is true of the traceback printing code. |
---|
127 | n/a | signal_line = self._process.stderr.readline() |
---|
128 | n/a | if signal_line != b'$\n': |
---|
129 | n/a | self.fail_with_process_info('while awaiting signal', |
---|
130 | n/a | stderr=signal_line) |
---|
131 | n/a | |
---|
132 | n/a | # We append a newline to our input so that a readline call can |
---|
133 | n/a | # end on its own before the EOF is seen and so that we're testing |
---|
134 | n/a | # the read call that was interrupted by a signal before the end of |
---|
135 | n/a | # the data stream has been reached. |
---|
136 | n/a | stdout, stderr = self._process.communicate(input=b'\n') |
---|
137 | n/a | if self._process.returncode: |
---|
138 | n/a | self.fail_with_process_info( |
---|
139 | n/a | 'exited rc=%d' % self._process.returncode, |
---|
140 | n/a | stdout, stderr, communicate=False) |
---|
141 | n/a | # PASS! |
---|
142 | n/a | |
---|
143 | n/a | # String format for the read_and_verify_code used by read methods. |
---|
144 | n/a | _READING_CODE_TEMPLATE = ( |
---|
145 | n/a | 'got = infile.{read_method_name}() ;' |
---|
146 | n/a | 'expected = {expected!r} ;' |
---|
147 | n/a | 'assert got == expected, (' |
---|
148 | n/a | '"{read_method_name} returned wrong data.\\n"' |
---|
149 | n/a | '"got data %r\\nexpected %r" % (got, expected))' |
---|
150 | n/a | ) |
---|
151 | n/a | |
---|
152 | n/a | def test_readline(self): |
---|
153 | n/a | """readline() must handle signals and not lose data.""" |
---|
154 | n/a | self._test_reading( |
---|
155 | n/a | data_to_write=b'hello, world!', |
---|
156 | n/a | read_and_verify_code=self._READING_CODE_TEMPLATE.format( |
---|
157 | n/a | read_method_name='readline', |
---|
158 | n/a | expected=b'hello, world!\n')) |
---|
159 | n/a | |
---|
160 | n/a | def test_readlines(self): |
---|
161 | n/a | """readlines() must handle signals and not lose data.""" |
---|
162 | n/a | self._test_reading( |
---|
163 | n/a | data_to_write=b'hello\nworld!', |
---|
164 | n/a | read_and_verify_code=self._READING_CODE_TEMPLATE.format( |
---|
165 | n/a | read_method_name='readlines', |
---|
166 | n/a | expected=[b'hello\n', b'world!\n'])) |
---|
167 | n/a | |
---|
168 | n/a | def test_readall(self): |
---|
169 | n/a | """readall() must handle signals and not lose data.""" |
---|
170 | n/a | self._test_reading( |
---|
171 | n/a | data_to_write=b'hello\nworld!', |
---|
172 | n/a | read_and_verify_code=self._READING_CODE_TEMPLATE.format( |
---|
173 | n/a | read_method_name='readall', |
---|
174 | n/a | expected=b'hello\nworld!\n')) |
---|
175 | n/a | # read() is the same thing as readall(). |
---|
176 | n/a | self._test_reading( |
---|
177 | n/a | data_to_write=b'hello\nworld!', |
---|
178 | n/a | read_and_verify_code=self._READING_CODE_TEMPLATE.format( |
---|
179 | n/a | read_method_name='read', |
---|
180 | n/a | expected=b'hello\nworld!\n')) |
---|
181 | n/a | |
---|
182 | n/a | |
---|
183 | n/a | class CTestFileIOSignalInterrupt(TestFileIOSignalInterrupt, unittest.TestCase): |
---|
184 | n/a | modname = '_io' |
---|
185 | n/a | |
---|
186 | n/a | class PyTestFileIOSignalInterrupt(TestFileIOSignalInterrupt, unittest.TestCase): |
---|
187 | n/a | modname = '_pyio' |
---|
188 | n/a | |
---|
189 | n/a | |
---|
190 | n/a | class TestBufferedIOSignalInterrupt(TestFileIOSignalInterrupt): |
---|
191 | n/a | def _generate_infile_setup_code(self): |
---|
192 | n/a | """Returns the infile = ... line of code to make a BufferedReader.""" |
---|
193 | n/a | return ('import %s as io ;infile = io.open(sys.stdin.fileno(), "rb") ;' |
---|
194 | n/a | 'assert isinstance(infile, io.BufferedReader)' % |
---|
195 | n/a | self.modname) |
---|
196 | n/a | |
---|
197 | n/a | def test_readall(self): |
---|
198 | n/a | """BufferedReader.read() must handle signals and not lose data.""" |
---|
199 | n/a | self._test_reading( |
---|
200 | n/a | data_to_write=b'hello\nworld!', |
---|
201 | n/a | read_and_verify_code=self._READING_CODE_TEMPLATE.format( |
---|
202 | n/a | read_method_name='read', |
---|
203 | n/a | expected=b'hello\nworld!\n')) |
---|
204 | n/a | |
---|
205 | n/a | class CTestBufferedIOSignalInterrupt(TestBufferedIOSignalInterrupt, unittest.TestCase): |
---|
206 | n/a | modname = '_io' |
---|
207 | n/a | |
---|
208 | n/a | class PyTestBufferedIOSignalInterrupt(TestBufferedIOSignalInterrupt, unittest.TestCase): |
---|
209 | n/a | modname = '_pyio' |
---|
210 | n/a | |
---|
211 | n/a | |
---|
212 | n/a | class TestTextIOSignalInterrupt(TestFileIOSignalInterrupt): |
---|
213 | n/a | def _generate_infile_setup_code(self): |
---|
214 | n/a | """Returns the infile = ... line of code to make a TextIOWrapper.""" |
---|
215 | n/a | return ('import %s as io ;' |
---|
216 | n/a | 'infile = io.open(sys.stdin.fileno(), "rt", newline=None) ;' |
---|
217 | n/a | 'assert isinstance(infile, io.TextIOWrapper)' % |
---|
218 | n/a | self.modname) |
---|
219 | n/a | |
---|
220 | n/a | def test_readline(self): |
---|
221 | n/a | """readline() must handle signals and not lose data.""" |
---|
222 | n/a | self._test_reading( |
---|
223 | n/a | data_to_write=b'hello, world!', |
---|
224 | n/a | read_and_verify_code=self._READING_CODE_TEMPLATE.format( |
---|
225 | n/a | read_method_name='readline', |
---|
226 | n/a | expected='hello, world!\n')) |
---|
227 | n/a | |
---|
228 | n/a | def test_readlines(self): |
---|
229 | n/a | """readlines() must handle signals and not lose data.""" |
---|
230 | n/a | self._test_reading( |
---|
231 | n/a | data_to_write=b'hello\r\nworld!', |
---|
232 | n/a | read_and_verify_code=self._READING_CODE_TEMPLATE.format( |
---|
233 | n/a | read_method_name='readlines', |
---|
234 | n/a | expected=['hello\n', 'world!\n'])) |
---|
235 | n/a | |
---|
236 | n/a | def test_readall(self): |
---|
237 | n/a | """read() must handle signals and not lose data.""" |
---|
238 | n/a | self._test_reading( |
---|
239 | n/a | data_to_write=b'hello\nworld!', |
---|
240 | n/a | read_and_verify_code=self._READING_CODE_TEMPLATE.format( |
---|
241 | n/a | read_method_name='read', |
---|
242 | n/a | expected="hello\nworld!\n")) |
---|
243 | n/a | |
---|
244 | n/a | class CTestTextIOSignalInterrupt(TestTextIOSignalInterrupt, unittest.TestCase): |
---|
245 | n/a | modname = '_io' |
---|
246 | n/a | |
---|
247 | n/a | class PyTestTextIOSignalInterrupt(TestTextIOSignalInterrupt, unittest.TestCase): |
---|
248 | n/a | modname = '_pyio' |
---|
249 | n/a | |
---|
250 | n/a | |
---|
251 | n/a | if __name__ == '__main__': |
---|
252 | n/a | unittest.main() |
---|