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. |
Andrew MacIntyre | 5cef571 | 2002-02-24 05:32:32 +0000 | [diff] [blame^] | 3 | OS/2+EMX doesn't support the file locking operations. |
Roger E. Masse | fb01d4b | 1996-12-17 17:41:09 +0000 | [diff] [blame] | 4 | Roger E. Masse |
| 5 | """ |
| 6 | import struct |
| 7 | import fcntl |
Guido van Rossum | 91221c2 | 1997-12-02 20:30:29 +0000 | [diff] [blame] | 8 | import os, sys |
Fredrik Lundh | f785042 | 2001-01-17 21:51:36 +0000 | [diff] [blame] | 9 | from test_support import verbose, TESTFN |
Roger E. Masse | fb01d4b | 1996-12-17 17:41:09 +0000 | [diff] [blame] | 10 | |
Fred Drake | f7ef15d | 2000-10-18 01:21:38 +0000 | [diff] [blame] | 11 | filename = TESTFN |
Roger E. Masse | fb01d4b | 1996-12-17 17:41:09 +0000 | [diff] [blame] | 12 | |
Martin v. Löwis | a660a34 | 2001-10-18 22:07:48 +0000 | [diff] [blame] | 13 | try: |
| 14 | os.O_LARGEFILE |
| 15 | except AttributeError: |
| 16 | start_len = "ll" |
| 17 | else: |
| 18 | start_len = "qq" |
| 19 | |
Jack Jansen | 8a97f4a | 2001-12-05 23:27:32 +0000 | [diff] [blame] | 20 | if sys.platform in ('netbsd1', 'Darwin1.2', 'darwin', |
Guido van Rossum | 2d21863 | 2000-08-29 14:57:27 +0000 | [diff] [blame] | 21 | 'freebsd2', 'freebsd3', 'freebsd4', 'freebsd5', |
Guido van Rossum | 0cf46bc | 1999-04-19 17:22:12 +0000 | [diff] [blame] | 22 | 'bsdos2', 'bsdos3', 'bsdos4', |
| 23 | 'openbsd', 'openbsd2'): |
Fred Drake | bc7809b | 2001-05-09 21:11:59 +0000 | [diff] [blame] | 24 | lockdata = struct.pack('lxxxxlxxxxlhh', 0, 0, 0, fcntl.F_WRLCK, 0) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 25 | elif sys.platform in ['aix3', 'aix4', 'hp-uxB', 'unixware7']: |
Fred Drake | bc7809b | 2001-05-09 21:11:59 +0000 | [diff] [blame] | 26 | lockdata = struct.pack('hhlllii', fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0) |
Andrew MacIntyre | 5cef571 | 2002-02-24 05:32:32 +0000 | [diff] [blame^] | 27 | elif sys.platform in ['os2emx']: |
| 28 | lockdata = None |
Guido van Rossum | 91221c2 | 1997-12-02 20:30:29 +0000 | [diff] [blame] | 29 | else: |
Martin v. Löwis | a660a34 | 2001-10-18 22:07:48 +0000 | [diff] [blame] | 30 | lockdata = struct.pack('hh'+start_len+'hh', fcntl.F_WRLCK, 0, 0, 0, 0, 0) |
Andrew MacIntyre | 5cef571 | 2002-02-24 05:32:32 +0000 | [diff] [blame^] | 31 | if lockdata: |
| 32 | if verbose: |
| 33 | print 'struct.pack: ', `lockdata` |
Fred Drake | bc7809b | 2001-05-09 21:11:59 +0000 | [diff] [blame] | 34 | |
| 35 | # the example from the library docs |
| 36 | f = open(filename, 'w') |
| 37 | rv = fcntl.fcntl(f.fileno(), fcntl.F_SETFL, os.O_NONBLOCK) |
| 38 | if verbose: |
| 39 | print 'Status from fnctl with O_NONBLOCK: ', rv |
| 40 | |
Andrew MacIntyre | 5cef571 | 2002-02-24 05:32:32 +0000 | [diff] [blame^] | 41 | if sys.platform not in ['os2emx']: |
| 42 | rv = fcntl.fcntl(f.fileno(), fcntl.F_SETLKW, lockdata) |
| 43 | if verbose: |
| 44 | print 'String from fcntl with F_SETLKW: ', `rv` |
Roger E. Masse | fb01d4b | 1996-12-17 17:41:09 +0000 | [diff] [blame] | 45 | |
| 46 | f.close() |
| 47 | os.unlink(filename) |
Fred Drake | bc7809b | 2001-05-09 21:11:59 +0000 | [diff] [blame] | 48 | |
| 49 | |
| 50 | # Again, but pass the file rather than numeric descriptor: |
| 51 | f = open(filename, 'w') |
| 52 | rv = fcntl.fcntl(f, fcntl.F_SETFL, os.O_NONBLOCK) |
| 53 | |
Andrew MacIntyre | 5cef571 | 2002-02-24 05:32:32 +0000 | [diff] [blame^] | 54 | if sys.platform not in ['os2emx']: |
| 55 | rv = fcntl.fcntl(f, fcntl.F_SETLKW, lockdata) |
Fred Drake | bc7809b | 2001-05-09 21:11:59 +0000 | [diff] [blame] | 56 | |
| 57 | f.close() |
| 58 | os.unlink(filename) |