|
| 1 | +import errno |
| 2 | +import select |
| 3 | +import subprocess |
| 4 | +import sys |
| 5 | +import textwrap |
| 6 | +import unittest |
| 7 | +from test import support |
| 8 | + |
| 9 | +support.requires_working_socket(module=True) |
| 10 | + |
| 11 | +@unittest.skipIf((sys.platform[:3]=='win'), |
| 12 | + "can't easily test on this system") |
| 13 | +class SelectTestCase(unittest.TestCase): |
| 14 | + |
| 15 | + class Nope: |
| 16 | + pass |
| 17 | + |
| 18 | + class Almost: |
| 19 | + def fileno(self): |
| 20 | + return 'fileno' |
| 21 | + |
| 22 | + def test_error_conditions(self): |
| 23 | + self.assertRaises(TypeError, select.select, 1, 2, 3) |
| 24 | + self.assertRaises(TypeError, select.select, [self.Nope()], [], []) |
| 25 | + self.assertRaises(TypeError, select.select, [self.Almost()], [], []) |
| 26 | + self.assertRaises(TypeError, select.select, [], [], [], "not a number") |
| 27 | + self.assertRaises(ValueError, select.select, [], [], [], -1) |
| 28 | + |
| 29 | + # Issue #12367: http://www.freebsd.org/cgi/query-pr.cgi?pr=kern/155606 |
| 30 | + @unittest.skipIf(sys.platform.startswith('freebsd'), |
| 31 | + 'skip because of a FreeBSD bug: kern/155606') |
| 32 | + def test_errno(self): |
| 33 | + with open(__file__, 'rb') as fp: |
| 34 | + fd = fp.fileno() |
| 35 | + fp.close() |
| 36 | + try: |
| 37 | + select.select([fd], [], [], 0) |
| 38 | + except OSError as err: |
| 39 | + self.assertEqual(err.errno, errno.EBADF) |
| 40 | + else: |
| 41 | + self.fail("exception not raised") |
| 42 | + |
| 43 | + @unittest.expectedFailure # TODO: RUSTPYTHON; AssertionError: unexpectedly identical: [] |
| 44 | + def test_returned_list_identity(self): |
| 45 | + # See issue #8329 |
| 46 | + r, w, x = select.select([], [], [], 1) |
| 47 | + self.assertIsNot(r, w) |
| 48 | + self.assertIsNot(r, x) |
| 49 | + self.assertIsNot(w, x) |
| 50 | + |
| 51 | + @support.requires_fork() |
| 52 | + def test_select(self): |
| 53 | + code = textwrap.dedent(''' |
| 54 | + import time |
| 55 | + for i in range(10): |
| 56 | + print("testing...", flush=True) |
| 57 | + time.sleep(0.050) |
| 58 | + ''') |
| 59 | + cmd = [sys.executable, '-I', '-c', code] |
| 60 | + with subprocess.Popen(cmd, stdout=subprocess.PIPE) as proc: |
| 61 | + pipe = proc.stdout |
| 62 | + for timeout in (0, 1, 2, 4, 8, 16) + (None,)*10: |
| 63 | + if support.verbose: |
| 64 | + print(f'timeout = {timeout}') |
| 65 | + rfd, wfd, xfd = select.select([pipe], [], [], timeout) |
| 66 | + self.assertEqual(wfd, []) |
| 67 | + self.assertEqual(xfd, []) |
| 68 | + if not rfd: |
| 69 | + continue |
| 70 | + if rfd == [pipe]: |
| 71 | + line = pipe.readline() |
| 72 | + if support.verbose: |
| 73 | + print(repr(line)) |
| 74 | + if not line: |
| 75 | + if support.verbose: |
| 76 | + print('EOF') |
| 77 | + break |
| 78 | + continue |
| 79 | + self.fail('Unexpected return values from select():', |
| 80 | + rfd, wfd, xfd) |
| 81 | + |
| 82 | + # Issue 16230: Crash on select resized list |
| 83 | + @unittest.skipIf( |
| 84 | + support.is_emscripten, "Emscripten cannot select a fd multiple times." |
| 85 | + ) |
| 86 | + @unittest.skip("TODO: RUSTPYTHON timed out") |
| 87 | + def test_select_mutated(self): |
| 88 | + a = [] |
| 89 | + class F: |
| 90 | + def fileno(self): |
| 91 | + del a[-1] |
| 92 | + return sys.__stdout__.fileno() |
| 93 | + a[:] = [F()] * 10 |
| 94 | + self.assertEqual(select.select([], a, []), ([], a[:5], [])) |
| 95 | + |
| 96 | + @unittest.expectedFailure # TODO: RUSTPYTHON; AssertionError: TypeError not raised by poll |
| 97 | + def test_disallow_instantiation(self): |
| 98 | + support.check_disallow_instantiation(self, type(select.poll())) |
| 99 | + |
| 100 | + if hasattr(select, 'devpoll'): |
| 101 | + support.check_disallow_instantiation(self, type(select.devpoll())) |
| 102 | + |
| 103 | +def tearDownModule(): |
| 104 | + support.reap_children() |
| 105 | + |
| 106 | +if __name__ == "__main__": |
| 107 | + unittest.main() |
0 commit comments