Home | History | Annotate | Download | only in test
      1 import errno
      2 import os
      3 import select
      4 import sys
      5 import unittest
      6 from test import support
      7 
      8 @unittest.skipIf((sys.platform[:3]=='win'),
      9                  "can't easily test on this system")
     10 class 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         p = os.popen(cmd, 'r')
     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         p.close()
     67 
     68     # Issue 16230: Crash on select resized list
     69     def test_select_mutated(self):
     70         a = []
     71         class F:
     72             def fileno(self):
     73                 del a[-1]
     74                 return sys.__stdout__.fileno()
     75         a[:] = [F()] * 10
     76         self.assertEqual(select.select([], a, []), ([], a[:5], []))
     77 
     78 def tearDownModule():
     79     support.reap_children()
     80 
     81 if __name__ == "__main__":
     82     unittest.main()
     83