blob: fd766746f7baf3334028cba8318c58d25ddfd822 [file] [log] [blame]
Guido van Rossumb31c7f71993-11-11 10:31:23 +00001# Testing select module
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002from test.test_support import verbose, reap_children
Barry Warsaw792c94a1996-12-11 23:58:46 +00003import select
4import os
5
6# test some known error conditions
7try:
8 rfd, wfd, xfd = select.select(1, 2, 3)
9except TypeError:
10 pass
11else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000012 print('expected TypeError exception not raised')
Barry Warsaw792c94a1996-12-11 23:58:46 +000013
14class Nope:
15 pass
16
17class Almost:
18 def fileno(self):
Guido van Rossum41360a41998-03-26 19:42:58 +000019 return 'fileno'
Fred Drake004d5e62000-10-23 17:22:08 +000020
Barry Warsaw792c94a1996-12-11 23:58:46 +000021try:
22 rfd, wfd, xfd = select.select([Nope()], [], [])
23except TypeError:
24 pass
25else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000026 print('expected TypeError exception not raised')
Barry Warsaw792c94a1996-12-11 23:58:46 +000027
28try:
29 rfd, wfd, xfd = select.select([Almost()], [], [])
30except TypeError:
31 pass
32else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000033 print('expected TypeError exception not raised')
Barry Warsaw792c94a1996-12-11 23:58:46 +000034
Neal Norwitz1b738e92002-06-13 22:23:47 +000035try:
36 rfd, wfd, xfd = select.select([], [], [], 'not a number')
37except TypeError:
38 pass
39else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000040 print('expected TypeError exception not raised')
Neal Norwitz1b738e92002-06-13 22:23:47 +000041
Guido van Rossumb31c7f71993-11-11 10:31:23 +000042
Guido van Rossumb31c7f71993-11-11 10:31:23 +000043def test():
Fred Drake004d5e62000-10-23 17:22:08 +000044 import sys
Martin v. Löwisa94568a2003-05-10 07:36:56 +000045 if sys.platform[:3] in ('win', 'mac', 'os2', 'riscos'):
Fred Drake004d5e62000-10-23 17:22:08 +000046 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000047 print("Can't test select easily on", sys.platform)
Fred Drake004d5e62000-10-23 17:22:08 +000048 return
49 cmd = 'for i in 0 1 2 3 4 5 6 7 8 9; do echo testing...; sleep 1; done'
50 p = os.popen(cmd, 'r')
51 for tout in (0, 1, 2, 4, 8, 16) + (None,)*10:
52 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000053 print('timeout =', tout)
Fred Drake004d5e62000-10-23 17:22:08 +000054 rfd, wfd, xfd = select.select([p], [], [], tout)
55 if (rfd, wfd, xfd) == ([], [], []):
56 continue
57 if (rfd, wfd, xfd) == ([p], [], []):
58 line = p.readline()
59 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000060 print(repr(line))
Fred Drake004d5e62000-10-23 17:22:08 +000061 if not line:
Guido van Rossum41360a41998-03-26 19:42:58 +000062 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000063 print('EOF')
Fred Drake004d5e62000-10-23 17:22:08 +000064 break
65 continue
Guido van Rossumbe19ed72007-02-09 05:37:30 +000066 print('Unexpected return values from select():', rfd, wfd, xfd)
Fred Drake004d5e62000-10-23 17:22:08 +000067 p.close()
Thomas Wouters0e3f5912006-08-11 14:57:12 +000068 reap_children()
Guido van Rossumb31c7f71993-11-11 10:31:23 +000069
70test()