test_fcntl.py revision 04f357cffef6d764f2f0ff2671dabde75ec250d1
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', 'Darwin1.2', 'darwin',
24                    'freebsd2', 'freebsd3', 'freebsd4', 'freebsd5',
25                    'bsdos2', 'bsdos3', 'bsdos4',
26                    'openbsd', 'openbsd2', 'openbsd3'):
27    lockdata = struct.pack('lxxxxlxxxxlhh', 0, 0, 0, fcntl.F_WRLCK, 0)
28elif sys.platform in ['aix3', 'aix4', 'hp-uxB', 'unixware7']:
29    lockdata = struct.pack('hhlllii', fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0)
30elif sys.platform in ['os2emx']:
31    lockdata = None
32else:
33    lockdata = struct.pack('hh'+start_len+'hh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
34if lockdata:
35    if verbose:
36        print 'struct.pack: ', `lockdata`
37
38# the example from the library docs
39f = open(filename, 'w')
40rv = fcntl.fcntl(f.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
41if verbose:
42    print 'Status from fnctl with O_NONBLOCK: ', rv
43
44if sys.platform not in ['os2emx']:
45    rv = fcntl.fcntl(f.fileno(), fcntl.F_SETLKW, lockdata)
46    if verbose:
47        print 'String from fcntl with F_SETLKW: ', `rv`
48
49f.close()
50os.unlink(filename)
51
52
53# Again, but pass the file rather than numeric descriptor:
54f = open(filename, 'w')
55rv = fcntl.fcntl(f, fcntl.F_SETFL, os.O_NONBLOCK)
56
57if sys.platform not in ['os2emx']:
58    rv = fcntl.fcntl(f, fcntl.F_SETLKW, lockdata)
59
60f.close()
61os.unlink(filename)
62