test_fcntl.py 1.83 KB
Newer Older
1 2
#! /usr/bin/env python
"""Test program for the fcntl C module.
3
   OS/2+EMX doesn't support the file locking operations.
4 5 6 7
   Roger E. Masse
"""
import struct
import fcntl
8
import os, sys
9
from test.test_support import verbose, TESTFN
10

11
filename = TESTFN
12

13 14 15 16 17 18 19
try:
    os.O_LARGEFILE
except AttributeError:
    start_len = "ll"
else:
    start_len = "qq"

20 21 22
if sys.platform.startswith('atheos'):
    start_len = "qq"

23
if sys.platform in ('netbsd1', 'netbsd2', 'Darwin1.2', 'darwin',
24
                    'freebsd2', 'freebsd3', 'freebsd4', 'freebsd5', 'freebsd6',
25
                    'freebsd7',
26
                    'bsdos2', 'bsdos3', 'bsdos4',
27
                    'openbsd', 'openbsd2', 'openbsd3'):
28 29 30 31 32 33 34
    if struct.calcsize('l') == 8:
        off_t = 'l'
        pid_t = 'i'
    else:
        off_t = 'lxxxx'
        pid_t = 'l'
    lockdata = struct.pack(off_t+off_t+pid_t+'hh', 0, 0, 0, fcntl.F_WRLCK, 0)
35
elif sys.platform in ['aix3', 'aix4', 'hp-uxB', 'unixware7']:
36
    lockdata = struct.pack('hhlllii', fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0)
37 38
elif sys.platform in ['os2emx']:
    lockdata = None
39
else:
40
    lockdata = struct.pack('hh'+start_len+'hh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
41 42
if lockdata:
    if verbose:
43
        print 'struct.pack: ', repr(lockdata)
44 45 46 47 48

# the example from the library docs
f = open(filename, 'w')
rv = fcntl.fcntl(f.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
if verbose:
49
    print 'Status from fcntl with O_NONBLOCK: ', rv
50

51 52 53
if sys.platform not in ['os2emx']:
    rv = fcntl.fcntl(f.fileno(), fcntl.F_SETLKW, lockdata)
    if verbose:
54
        print 'String from fcntl with F_SETLKW: ', repr(rv)
55 56 57

f.close()
os.unlink(filename)
58 59 60 61 62 63


# Again, but pass the file rather than numeric descriptor:
f = open(filename, 'w')
rv = fcntl.fcntl(f, fcntl.F_SETFL, os.O_NONBLOCK)

64 65
if sys.platform not in ['os2emx']:
    rv = fcntl.fcntl(f, fcntl.F_SETLKW, lockdata)
66 67 68

f.close()
os.unlink(filename)