test_fcntl.py revision 5771310a09640388066fd1747842035f0df2f8a5
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"""
6import struct
7import fcntl
8import os, sys
9from test.test_support import verbose, TESTFN
10
11filename = TESTFN
12
13try:
14    os.O_LARGEFILE
15except AttributeError:
16    start_len = "ll"
17else:
18    start_len = "qq"
19
20if sys.platform.startswith('atheos'):
21    start_len = "qq"
22
23if 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    if struct.calcsize('l') == 8:
29        off_t = 'l'
30        pid_t = 'i'
31    else:
32        off_t = 'lxxxx'
33        pid_t = 'l'
34    lockdata = struct.pack(off_t+off_t+pid_t+'hh', 0, 0, 0, fcntl.F_WRLCK, 0)
35elif sys.platform in ['aix3', 'aix4', 'hp-uxB', 'unixware7']:
36    lockdata = struct.pack('hhlllii', fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0)
37elif sys.platform in ['os2emx']:
38    lockdata = None
39else:
40    lockdata = struct.pack('hh'+start_len+'hh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
41if lockdata:
42    if verbose:
43        print 'struct.pack: ', repr(lockdata)
44
45# the example from the library docs
46f = open(filename, 'w')
47rv = fcntl.fcntl(f.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
48if verbose:
49    print 'Status from fcntl with O_NONBLOCK: ', rv
50
51if sys.platform not in ['os2emx']:
52    rv = fcntl.fcntl(f.fileno(), fcntl.F_SETLKW, lockdata)
53    if verbose:
54        print 'String from fcntl with F_SETLKW: ', repr(rv)
55
56f.close()
57os.unlink(filename)
58
59
60# Again, but pass the file rather than numeric descriptor:
61f = open(filename, 'w')
62rv = fcntl.fcntl(f, fcntl.F_SETFL, os.O_NONBLOCK)
63
64if sys.platform not in ['os2emx']:
65    rv = fcntl.fcntl(f, fcntl.F_SETLKW, lockdata)
66
67f.close()
68os.unlink(filename)
69