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 |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 9 | from test.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 | |
Martin v. Löwis | f90ae20 | 2002-06-11 06:22:31 +0000 | [diff] [blame] | 20 | if sys.platform.startswith('atheos'): |
| 21 | start_len = "qq" |
| 22 | |
Georg Brandl | d819c13 | 2006-06-21 17:52:36 +0000 | [diff] [blame^] | 23 | if sys.platform in ('netbsd1', 'netbsd2', 'netbsd3', |
| 24 | 'Darwin1.2', 'darwin', |
| 25 | 'freebsd2', 'freebsd3', 'freebsd4', 'freebsd5', |
| 26 | 'freebsd6', 'freebsd7', |
Guido van Rossum | 0cf46bc | 1999-04-19 17:22:12 +0000 | [diff] [blame] | 27 | 'bsdos2', 'bsdos3', 'bsdos4', |
Guido van Rossum | df4dabd | 2002-05-13 14:58:02 +0000 | [diff] [blame] | 28 | 'openbsd', 'openbsd2', 'openbsd3'): |
Hye-Shik Chang | ac89f6e | 2005-04-04 15:21:04 +0000 | [diff] [blame] | 29 | if struct.calcsize('l') == 8: |
| 30 | off_t = 'l' |
| 31 | pid_t = 'i' |
| 32 | else: |
| 33 | off_t = 'lxxxx' |
| 34 | pid_t = 'l' |
| 35 | lockdata = struct.pack(off_t+off_t+pid_t+'hh', 0, 0, 0, fcntl.F_WRLCK, 0) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 36 | elif sys.platform in ['aix3', 'aix4', 'hp-uxB', 'unixware7']: |
Fred Drake | bc7809b | 2001-05-09 21:11:59 +0000 | [diff] [blame] | 37 | 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] | 38 | elif sys.platform in ['os2emx']: |
| 39 | lockdata = None |
Guido van Rossum | 91221c2 | 1997-12-02 20:30:29 +0000 | [diff] [blame] | 40 | else: |
Martin v. Löwis | a660a34 | 2001-10-18 22:07:48 +0000 | [diff] [blame] | 41 | 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] | 42 | if lockdata: |
| 43 | if verbose: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 44 | print 'struct.pack: ', repr(lockdata) |
Fred Drake | bc7809b | 2001-05-09 21:11:59 +0000 | [diff] [blame] | 45 | |
| 46 | # the example from the library docs |
| 47 | f = open(filename, 'w') |
| 48 | rv = fcntl.fcntl(f.fileno(), fcntl.F_SETFL, os.O_NONBLOCK) |
| 49 | if verbose: |
Hye-Shik Chang | ac89f6e | 2005-04-04 15:21:04 +0000 | [diff] [blame] | 50 | print 'Status from fcntl with O_NONBLOCK: ', rv |
Fred Drake | bc7809b | 2001-05-09 21:11:59 +0000 | [diff] [blame] | 51 | |
Andrew MacIntyre | 5cef571 | 2002-02-24 05:32:32 +0000 | [diff] [blame] | 52 | if sys.platform not in ['os2emx']: |
| 53 | rv = fcntl.fcntl(f.fileno(), fcntl.F_SETLKW, lockdata) |
| 54 | if verbose: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 55 | print 'String from fcntl with F_SETLKW: ', repr(rv) |
Roger E. Masse | fb01d4b | 1996-12-17 17:41:09 +0000 | [diff] [blame] | 56 | |
| 57 | f.close() |
| 58 | os.unlink(filename) |
Fred Drake | bc7809b | 2001-05-09 21:11:59 +0000 | [diff] [blame] | 59 | |
| 60 | |
| 61 | # Again, but pass the file rather than numeric descriptor: |
| 62 | f = open(filename, 'w') |
| 63 | rv = fcntl.fcntl(f, fcntl.F_SETFL, os.O_NONBLOCK) |
| 64 | |
Andrew MacIntyre | 5cef571 | 2002-02-24 05:32:32 +0000 | [diff] [blame] | 65 | if sys.platform not in ['os2emx']: |
| 66 | rv = fcntl.fcntl(f, fcntl.F_SETLKW, lockdata) |
Fred Drake | bc7809b | 2001-05-09 21:11:59 +0000 | [diff] [blame] | 67 | |
| 68 | f.close() |
| 69 | os.unlink(filename) |