test_fcntl.py revision 2242f2fbd0d50546ad79e800efbc6f0a2334bd0b
1#! /usr/bin/env python
2"""Test program for the fcntl C module.
3   Roger E. Masse
4"""
5import struct
6import fcntl
7import FCNTL
8import os, sys
9from test_support import verbose, TESTFN
10
11filename = TESTFN
12
13# the example from the library docs
14f = open(filename, 'w')
15rv = fcntl.fcntl(f.fileno(), FCNTL.F_SETFL, os.O_NONBLOCK)
16if verbose:
17    print 'Status from fnctl with O_NONBLOCK: ', rv
18
19if sys.platform in ('netbsd1', 'Darwin1.2', 'darwin1',
20                    'freebsd2', 'freebsd3', 'freebsd4', 'freebsd5',
21                    'bsdos2', 'bsdos3', 'bsdos4',
22                    'openbsd', 'openbsd2'):
23    lockdata = struct.pack('lxxxxlxxxxlhh', 0, 0, 0, FCNTL.F_WRLCK, 0)
24elif sys.platform in ['aix3', 'aix4', 'hp-uxB', 'unixware7']:
25    lockdata = struct.pack('hhlllii', FCNTL.F_WRLCK, 0, 0, 0, 0, 0, 0)
26else:
27    lockdata = struct.pack('hhllhh', FCNTL.F_WRLCK, 0, 0, 0, 0, 0)
28if verbose:
29    print 'struct.pack: ', `lockdata`
30
31rv = fcntl.fcntl(f.fileno(), FCNTL.F_SETLKW, lockdata)
32if verbose:
33    print 'String from fcntl with F_SETLKW: ', `rv`
34
35f.close()
36os.unlink(filename)
37