| 1 | #! /usr/bin/env python
|
|---|
| 2 | """Test program for the fcntl C module.
|
|---|
| 3 | OS/2+EMX doesn't support the file locking operations.
|
|---|
| 4 | Roger E. Masse
|
|---|
| 5 | """
|
|---|
| 6 | import struct
|
|---|
| 7 | import fcntl
|
|---|
| 8 | import os, sys
|
|---|
| 9 | from test.test_support import verbose, TESTFN
|
|---|
| 10 |
|
|---|
| 11 | filename = TESTFN
|
|---|
| 12 |
|
|---|
| 13 | try:
|
|---|
| 14 | os.O_LARGEFILE
|
|---|
| 15 | except AttributeError:
|
|---|
| 16 | start_len = "ll"
|
|---|
| 17 | else:
|
|---|
| 18 | start_len = "qq"
|
|---|
| 19 |
|
|---|
| 20 | if sys.platform.startswith('atheos'):
|
|---|
| 21 | start_len = "qq"
|
|---|
| 22 |
|
|---|
| 23 | if sys.platform in ('netbsd1', 'netbsd2', 'netbsd3',
|
|---|
| 24 | 'Darwin1.2', 'darwin',
|
|---|
| 25 | 'freebsd2', 'freebsd3', 'freebsd4', 'freebsd5',
|
|---|
| 26 | 'freebsd6', 'freebsd7',
|
|---|
| 27 | 'bsdos2', 'bsdos3', 'bsdos4',
|
|---|
| 28 | 'openbsd', 'openbsd2', 'openbsd3', 'openbsd4'):
|
|---|
| 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)
|
|---|
| 36 | elif sys.platform in ['aix3', 'aix4', 'hp-uxB', 'unixware7']:
|
|---|
| 37 | lockdata = struct.pack('hhlllii', fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0)
|
|---|
| 38 | elif sys.platform in ['os2emx']:
|
|---|
| 39 | lockdata = None
|
|---|
| 40 | else:
|
|---|
| 41 | lockdata = struct.pack('hh'+start_len+'hh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
|
|---|
| 42 | if lockdata:
|
|---|
| 43 | if verbose:
|
|---|
| 44 | print 'struct.pack: ', repr(lockdata)
|
|---|
| 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:
|
|---|
| 50 | print 'Status from fcntl with O_NONBLOCK: ', rv
|
|---|
| 51 |
|
|---|
| 52 | if sys.platform not in ['os2emx']:
|
|---|
| 53 | rv = fcntl.fcntl(f.fileno(), fcntl.F_SETLKW, lockdata)
|
|---|
| 54 | if verbose:
|
|---|
| 55 | print 'String from fcntl with F_SETLKW: ', repr(rv)
|
|---|
| 56 |
|
|---|
| 57 | f.close()
|
|---|
| 58 | os.unlink(filename)
|
|---|
| 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 |
|
|---|
| 65 | if sys.platform not in ['os2emx']:
|
|---|
| 66 | rv = fcntl.fcntl(f, fcntl.F_SETLKW, lockdata)
|
|---|
| 67 |
|
|---|
| 68 | f.close()
|
|---|
| 69 | os.unlink(filename)
|
|---|