1 | n/a | import errno |
---|
2 | n/a | import os |
---|
3 | n/a | import select |
---|
4 | n/a | import sys |
---|
5 | n/a | import unittest |
---|
6 | n/a | from test import support |
---|
7 | n/a | |
---|
8 | n/a | @unittest.skipIf((sys.platform[:3]=='win'), |
---|
9 | n/a | "can't easily test on this system") |
---|
10 | n/a | class SelectTestCase(unittest.TestCase): |
---|
11 | n/a | |
---|
12 | n/a | class Nope: |
---|
13 | n/a | pass |
---|
14 | n/a | |
---|
15 | n/a | class Almost: |
---|
16 | n/a | def fileno(self): |
---|
17 | n/a | return 'fileno' |
---|
18 | n/a | |
---|
19 | n/a | def test_error_conditions(self): |
---|
20 | n/a | self.assertRaises(TypeError, select.select, 1, 2, 3) |
---|
21 | n/a | self.assertRaises(TypeError, select.select, [self.Nope()], [], []) |
---|
22 | n/a | self.assertRaises(TypeError, select.select, [self.Almost()], [], []) |
---|
23 | n/a | self.assertRaises(TypeError, select.select, [], [], [], "not a number") |
---|
24 | n/a | self.assertRaises(ValueError, select.select, [], [], [], -1) |
---|
25 | n/a | |
---|
26 | n/a | # Issue #12367: http://www.freebsd.org/cgi/query-pr.cgi?pr=kern/155606 |
---|
27 | n/a | @unittest.skipIf(sys.platform.startswith('freebsd'), |
---|
28 | n/a | 'skip because of a FreeBSD bug: kern/155606') |
---|
29 | n/a | def test_errno(self): |
---|
30 | n/a | with open(__file__, 'rb') as fp: |
---|
31 | n/a | fd = fp.fileno() |
---|
32 | n/a | fp.close() |
---|
33 | n/a | try: |
---|
34 | n/a | select.select([fd], [], [], 0) |
---|
35 | n/a | except OSError as err: |
---|
36 | n/a | self.assertEqual(err.errno, errno.EBADF) |
---|
37 | n/a | else: |
---|
38 | n/a | self.fail("exception not raised") |
---|
39 | n/a | |
---|
40 | n/a | def test_returned_list_identity(self): |
---|
41 | n/a | # See issue #8329 |
---|
42 | n/a | r, w, x = select.select([], [], [], 1) |
---|
43 | n/a | self.assertIsNot(r, w) |
---|
44 | n/a | self.assertIsNot(r, x) |
---|
45 | n/a | self.assertIsNot(w, x) |
---|
46 | n/a | |
---|
47 | n/a | def test_select(self): |
---|
48 | n/a | cmd = 'for i in 0 1 2 3 4 5 6 7 8 9; do echo testing...; sleep 1; done' |
---|
49 | n/a | p = os.popen(cmd, 'r') |
---|
50 | n/a | for tout in (0, 1, 2, 4, 8, 16) + (None,)*10: |
---|
51 | n/a | if support.verbose: |
---|
52 | n/a | print('timeout =', tout) |
---|
53 | n/a | rfd, wfd, xfd = select.select([p], [], [], tout) |
---|
54 | n/a | if (rfd, wfd, xfd) == ([], [], []): |
---|
55 | n/a | continue |
---|
56 | n/a | if (rfd, wfd, xfd) == ([p], [], []): |
---|
57 | n/a | line = p.readline() |
---|
58 | n/a | if support.verbose: |
---|
59 | n/a | print(repr(line)) |
---|
60 | n/a | if not line: |
---|
61 | n/a | if support.verbose: |
---|
62 | n/a | print('EOF') |
---|
63 | n/a | break |
---|
64 | n/a | continue |
---|
65 | n/a | self.fail('Unexpected return values from select():', rfd, wfd, xfd) |
---|
66 | n/a | p.close() |
---|
67 | n/a | |
---|
68 | n/a | # Issue 16230: Crash on select resized list |
---|
69 | n/a | def test_select_mutated(self): |
---|
70 | n/a | a = [] |
---|
71 | n/a | class F: |
---|
72 | n/a | def fileno(self): |
---|
73 | n/a | del a[-1] |
---|
74 | n/a | return sys.__stdout__.fileno() |
---|
75 | n/a | a[:] = [F()] * 10 |
---|
76 | n/a | self.assertEqual(select.select([], a, []), ([], a[:5], [])) |
---|
77 | n/a | |
---|
78 | n/a | def tearDownModule(): |
---|
79 | n/a | support.reap_children() |
---|
80 | n/a | |
---|
81 | n/a | if __name__ == "__main__": |
---|
82 | n/a | unittest.main() |
---|