Guido van Rossum | b31c7f7 | 1993-11-11 10:31:23 +0000 | [diff] [blame] | 1 | # Testing select module |
Barry Warsaw | 792c94a | 1996-12-11 23:58:46 +0000 | [diff] [blame] | 2 | import select |
| 3 | import os |
| 4 | |
| 5 | # test some known error conditions |
| 6 | try: |
| 7 | rfd, wfd, xfd = select.select(1, 2, 3) |
| 8 | except TypeError: |
| 9 | pass |
| 10 | else: |
| 11 | print 'expected TypeError exception not raised' |
| 12 | |
| 13 | class Nope: |
| 14 | pass |
| 15 | |
| 16 | class Almost: |
| 17 | def fileno(self): |
| 18 | return 'fileno' |
| 19 | |
| 20 | try: |
| 21 | rfd, wfd, xfd = select.select([Nope()], [], []) |
| 22 | except TypeError: |
| 23 | pass |
| 24 | else: |
| 25 | print 'expected TypeError exception not raised' |
| 26 | |
| 27 | try: |
| 28 | rfd, wfd, xfd = select.select([Almost()], [], []) |
| 29 | except TypeError: |
| 30 | pass |
| 31 | else: |
| 32 | print 'expected TypeError exception not raised' |
| 33 | |
Guido van Rossum | b31c7f7 | 1993-11-11 10:31:23 +0000 | [diff] [blame] | 34 | |
Guido van Rossum | b31c7f7 | 1993-11-11 10:31:23 +0000 | [diff] [blame] | 35 | def test(): |
Barry Warsaw | 792c94a | 1996-12-11 23:58:46 +0000 | [diff] [blame] | 36 | cmd = 'for i in 0 1 2 3 4 5 6 7 8 9; do echo testing...; sleep 1; done' |
Guido van Rossum | b31c7f7 | 1993-11-11 10:31:23 +0000 | [diff] [blame] | 37 | p = os.popen(cmd, 'r') |
| 38 | for tout in (0, 1, 2, 4, 8, 16) + (None,)*10: |
| 39 | print 'timeout =', tout |
| 40 | rfd, wfd, xfd = select.select([p], [], [], tout) |
Barry Warsaw | 792c94a | 1996-12-11 23:58:46 +0000 | [diff] [blame] | 41 | ## print rfd, wfd, xfd |
Guido van Rossum | b31c7f7 | 1993-11-11 10:31:23 +0000 | [diff] [blame] | 42 | if (rfd, wfd, xfd) == ([], [], []): |
| 43 | continue |
| 44 | if (rfd, wfd, xfd) == ([p], [], []): |
| 45 | line = p.readline() |
| 46 | print `line` |
| 47 | if not line: |
| 48 | print 'EOF' |
| 49 | break |
| 50 | continue |
| 51 | print 'Heh?' |
Barry Warsaw | bd8a911 | 1997-01-14 17:49:15 +0000 | [diff] [blame] | 52 | p.close() |
Guido van Rossum | b31c7f7 | 1993-11-11 10:31:23 +0000 | [diff] [blame] | 53 | |
| 54 | test() |
Barry Warsaw | 792c94a | 1996-12-11 23:58:46 +0000 | [diff] [blame] | 55 | |