1 from test import test_support
2 import unittest
3 import select
4 import os
5 import sys
6 
7 @unittest.skipIf(sys.platform[:3] in ('win', 'os2', 'riscos'),
8                  "can't easily test on this system")
9 class SelectTestCase(unittest.TestCase):
10 
11     class Nope:
12         pass
13 
14     class Almost:
15         def fileno(self):
16             return 'fileno'
17 
18     def test_error_conditions(self):
19         self.assertRaises(TypeError, select.select, 1, 2, 3)
20         self.assertRaises(TypeError, select.select, [self.Nope()], [], [])
21         self.assertRaises(TypeError, select.select, [self.Almost()], [], [])
22         self.assertRaises(TypeError, select.select, [], [], [], "not a number")
23 
24     def test_returned_list_identity(self):
25         # See issue #8329
26         r, w, x = select.select([], [], [], 1)
27         self.assertIsNot(r, w)
28         self.assertIsNot(r, x)
29         self.assertIsNot(w, x)
30 
31     def test_select(self):
32         cmd = 'for i in 0 1 2 3 4 5 6 7 8 9; do echo testing...; sleep 1; done'
33         p = os.popen(cmd, 'r')
34         for tout in (0, 1, 2, 4, 8, 16) + (None,)*10:
35             if test_support.verbose:
36                 print 'timeout =', tout
37             rfd, wfd, xfd = select.select([p], [], [], tout)
38             if (rfd, wfd, xfd) == ([], [], []):
39                 continue
40             if (rfd, wfd, xfd) == ([p], [], []):
41                 line = p.readline()
42                 if test_support.verbose:
43                     print repr(line)
44                 if not line:
45                     if test_support.verbose:
46                         print 'EOF'
47                     break
48                 continue
49             self.fail('Unexpected return values from select():', rfd, wfd, xfd)
50         p.close()
51 
52     # Issue 16230: Crash on select resized list
53     def test_select_mutated(self):
54         a = []
55         class F:
56             def fileno(self):
57                 del a[-1]
58                 return sys.__stdout__.fileno()
59         a[:] = [F()] * 10
60         self.assertEqual(select.select([], a, []), ([], a[:5], []))
61 
62 def test_main():
63     test_support.run_unittest(SelectTestCase)
64     test_support.reap_children()
65 
66 if __name__ == "__main__":
67     test_main()
68