Roger E. Masse | fb01d4b | 1996-12-17 17:41:09 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | """Test program for the fcntl C module. |
| 3 | Roger E. Masse |
| 4 | """ |
| 5 | import struct |
| 6 | import fcntl |
| 7 | import FCNTL |
Guido van Rossum | 91221c2 | 1997-12-02 20:30:29 +0000 | [diff] [blame] | 8 | import os, sys |
Roger E. Masse | fab8ab8 | 1996-12-20 22:36:52 +0000 | [diff] [blame] | 9 | from test_support import verbose |
Roger E. Masse | fb01d4b | 1996-12-17 17:41:09 +0000 | [diff] [blame] | 10 | |
| 11 | filename = '/tmp/delete-me' |
| 12 | |
| 13 | # the example from the library docs |
| 14 | f = open(filename,'w') |
Guido van Rossum | 0e351f3 | 1997-05-12 22:15:52 +0000 | [diff] [blame] | 15 | rv = fcntl.fcntl(f.fileno(), FCNTL.F_SETFL, os.O_NONBLOCK) |
Roger E. Masse | fb01d4b | 1996-12-17 17:41:09 +0000 | [diff] [blame] | 16 | if verbose: |
Guido van Rossum | 0e351f3 | 1997-05-12 22:15:52 +0000 | [diff] [blame] | 17 | print 'Status from fnctl with O_NONBLOCK: ', rv |
Roger E. Masse | fb01d4b | 1996-12-17 17:41:09 +0000 | [diff] [blame] | 18 | |
Guido van Rossum | 5ef8f0c | 1999-02-23 04:13:37 +0000 | [diff] [blame] | 19 | if sys.platform in ('netbsd1', |
| 20 | 'freebsd2', 'freebsd3', |
Guido van Rossum | 0cf46bc | 1999-04-19 17:22:12 +0000 | [diff] [blame^] | 21 | 'bsdos2', 'bsdos3', 'bsdos4', |
| 22 | 'openbsd', 'openbsd2'): |
Guido van Rossum | 91221c2 | 1997-12-02 20:30:29 +0000 | [diff] [blame] | 23 | lockdata = struct.pack('lxxxxlxxxxlhh', 0, 0, 0, FCNTL.F_WRLCK, 0) |
| 24 | elif sys.platform in ['aix3', 'aix4']: |
| 25 | lockdata = struct.pack('hhlllii', FCNTL.F_WRLCK, 0, 0, 0, 0, 0, 0) |
| 26 | else: |
| 27 | lockdata = struct.pack('hhllhh', FCNTL.F_WRLCK, 0, 0, 0, 0, 0) |
Roger E. Masse | fb01d4b | 1996-12-17 17:41:09 +0000 | [diff] [blame] | 28 | if verbose: |
Guido van Rossum | 16dffdc | 1997-05-09 02:06:05 +0000 | [diff] [blame] | 29 | print 'struct.pack: ', `lockdata` |
Roger E. Masse | fb01d4b | 1996-12-17 17:41:09 +0000 | [diff] [blame] | 30 | |
| 31 | rv = fcntl.fcntl(f.fileno(), FCNTL.F_SETLKW, lockdata) |
| 32 | if verbose: |
Guido van Rossum | 16dffdc | 1997-05-09 02:06:05 +0000 | [diff] [blame] | 33 | print 'String from fcntl with F_SETLKW: ', `rv` |
Roger E. Masse | fb01d4b | 1996-12-17 17:41:09 +0000 | [diff] [blame] | 34 | |
| 35 | f.close() |
| 36 | os.unlink(filename) |