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