Guido van Rossum | b31c7f7 | 1993-11-11 10:31:23 +0000 | [diff] [blame] | 1 | # Testing select module |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2 | from test.test_support import verbose, reap_children |
Barry Warsaw | 792c94a | 1996-12-11 23:58:46 +0000 | [diff] [blame] | 3 | import select |
| 4 | import os |
| 5 | |
| 6 | # test some known error conditions |
| 7 | try: |
| 8 | rfd, wfd, xfd = select.select(1, 2, 3) |
| 9 | except TypeError: |
| 10 | pass |
| 11 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 12 | print('expected TypeError exception not raised') |
Barry Warsaw | 792c94a | 1996-12-11 23:58:46 +0000 | [diff] [blame] | 13 | |
| 14 | class Nope: |
| 15 | pass |
| 16 | |
| 17 | class Almost: |
| 18 | def fileno(self): |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 19 | return 'fileno' |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 20 | |
Barry Warsaw | 792c94a | 1996-12-11 23:58:46 +0000 | [diff] [blame] | 21 | try: |
| 22 | rfd, wfd, xfd = select.select([Nope()], [], []) |
| 23 | except TypeError: |
| 24 | pass |
| 25 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 26 | print('expected TypeError exception not raised') |
Barry Warsaw | 792c94a | 1996-12-11 23:58:46 +0000 | [diff] [blame] | 27 | |
| 28 | try: |
| 29 | rfd, wfd, xfd = select.select([Almost()], [], []) |
| 30 | except TypeError: |
| 31 | pass |
| 32 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 33 | print('expected TypeError exception not raised') |
Barry Warsaw | 792c94a | 1996-12-11 23:58:46 +0000 | [diff] [blame] | 34 | |
Neal Norwitz | 1b738e9 | 2002-06-13 22:23:47 +0000 | [diff] [blame] | 35 | try: |
| 36 | rfd, wfd, xfd = select.select([], [], [], 'not a number') |
| 37 | except TypeError: |
| 38 | pass |
| 39 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 40 | print('expected TypeError exception not raised') |
Neal Norwitz | 1b738e9 | 2002-06-13 22:23:47 +0000 | [diff] [blame] | 41 | |
Guido van Rossum | b31c7f7 | 1993-11-11 10:31:23 +0000 | [diff] [blame] | 42 | |
Guido van Rossum | b31c7f7 | 1993-11-11 10:31:23 +0000 | [diff] [blame] | 43 | def test(): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 44 | import sys |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 45 | if sys.platform[:3] in ('win', 'mac', 'os2', 'riscos'): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 46 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 47 | print("Can't test select easily on", sys.platform) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 48 | 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 Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 53 | print('timeout =', tout) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 54 | 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 Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 60 | print(repr(line)) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 61 | if not line: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 62 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 63 | print('EOF') |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 64 | break |
| 65 | continue |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 66 | print('Unexpected return values from select():', rfd, wfd, xfd) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 67 | p.close() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 68 | reap_children() |
Guido van Rossum | b31c7f7 | 1993-11-11 10:31:23 +0000 | [diff] [blame] | 69 | |
| 70 | test() |