Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 1 | # Testing select module |
Guido van Rossum | de554ec | 1997-05-08 23:14:57 +0000 | [diff] [blame] | 2 | from test_support import verbose |
Guido van Rossum | 228b8e8 | 1997-04-02 06:13:34 +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: |
| 12 | print 'expected TypeError exception not raised' |
| 13 | |
| 14 | class Nope: |
| 15 | pass |
| 16 | |
| 17 | class Almost: |
| 18 | def fileno(self): |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 19 | return 'fileno' |
Guido van Rossum | 228b8e8 | 1997-04-02 06:13:34 +0000 | [diff] [blame] | 20 | |
| 21 | try: |
| 22 | rfd, wfd, xfd = select.select([Nope()], [], []) |
| 23 | except TypeError: |
| 24 | pass |
| 25 | else: |
| 26 | print 'expected TypeError exception not raised' |
| 27 | |
| 28 | try: |
| 29 | rfd, wfd, xfd = select.select([Almost()], [], []) |
| 30 | except TypeError: |
| 31 | pass |
| 32 | else: |
| 33 | print 'expected TypeError exception not raised' |
| 34 | |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 35 | |
| 36 | def test(): |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 37 | import sys |
| 38 | if sys.platform[:3] in ('win', 'mac', 'os2'): |
| 39 | if verbose: |
| 40 | print "Can't test select easily on", sys.platform |
| 41 | return |
| 42 | cmd = 'for i in 0 1 2 3 4 5 6 7 8 9; do echo testing...; sleep 1; done' |
| 43 | p = os.popen(cmd, 'r') |
| 44 | for tout in (0, 1, 2, 4, 8, 16) + (None,)*10: |
| 45 | if verbose: |
| 46 | print 'timeout =', tout |
| 47 | rfd, wfd, xfd = select.select([p], [], [], tout) |
| 48 | ## print rfd, wfd, xfd |
| 49 | if (rfd, wfd, xfd) == ([], [], []): |
| 50 | continue |
| 51 | if (rfd, wfd, xfd) == ([p], [], []): |
| 52 | line = p.readline() |
| 53 | if verbose: |
| 54 | print `line` |
| 55 | if not line: |
| 56 | if verbose: |
| 57 | print 'EOF' |
| 58 | break |
| 59 | continue |
| 60 | print 'Heh?' |
| 61 | p.close() |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 62 | |
| 63 | test() |
Guido van Rossum | 228b8e8 | 1997-04-02 06:13:34 +0000 | [diff] [blame] | 64 | |