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