1996-12-17 13:41:09 -04:00
|
|
|
#! /usr/bin/env python
|
|
|
|
"""Test program for the fcntl C module.
|
|
|
|
Roger E. Masse
|
|
|
|
"""
|
|
|
|
import struct
|
|
|
|
import fcntl
|
1997-12-02 16:30:29 -04:00
|
|
|
import os, sys
|
2001-01-17 17:51:36 -04:00
|
|
|
from test_support import verbose, TESTFN
|
1996-12-17 13:41:09 -04:00
|
|
|
|
2000-10-17 22:21:38 -03:00
|
|
|
filename = TESTFN
|
1996-12-17 13:41:09 -04:00
|
|
|
|
2001-10-18 19:07:48 -03:00
|
|
|
try:
|
|
|
|
os.O_LARGEFILE
|
|
|
|
except AttributeError:
|
|
|
|
start_len = "ll"
|
|
|
|
else:
|
|
|
|
start_len = "qq"
|
|
|
|
|
2001-03-27 21:14:56 -04:00
|
|
|
if sys.platform in ('netbsd1', 'Darwin1.2', 'darwin1',
|
2000-08-29 11:57:27 -03:00
|
|
|
'freebsd2', 'freebsd3', 'freebsd4', 'freebsd5',
|
1999-04-19 14:22:12 -03:00
|
|
|
'bsdos2', 'bsdos3', 'bsdos4',
|
|
|
|
'openbsd', 'openbsd2'):
|
2001-05-09 18:11:59 -03:00
|
|
|
lockdata = struct.pack('lxxxxlxxxxlhh', 0, 0, 0, fcntl.F_WRLCK, 0)
|
2001-04-11 17:58:20 -03:00
|
|
|
elif sys.platform in ['aix3', 'aix4', 'hp-uxB', 'unixware7']:
|
2001-05-09 18:11:59 -03:00
|
|
|
lockdata = struct.pack('hhlllii', fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0)
|
1997-12-02 16:30:29 -04:00
|
|
|
else:
|
2001-10-18 19:07:48 -03:00
|
|
|
lockdata = struct.pack('hh'+start_len+'hh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
|
1996-12-17 13:41:09 -04:00
|
|
|
if verbose:
|
1997-05-08 23:06:05 -03:00
|
|
|
print 'struct.pack: ', `lockdata`
|
2000-10-23 14:22:08 -03:00
|
|
|
|
2001-05-09 18:11:59 -03:00
|
|
|
|
|
|
|
# the example from the library docs
|
|
|
|
f = open(filename, 'w')
|
|
|
|
rv = fcntl.fcntl(f.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
|
|
|
|
if verbose:
|
|
|
|
print 'Status from fnctl with O_NONBLOCK: ', rv
|
|
|
|
|
|
|
|
rv = fcntl.fcntl(f.fileno(), fcntl.F_SETLKW, lockdata)
|
1996-12-17 13:41:09 -04:00
|
|
|
if verbose:
|
1997-05-08 23:06:05 -03:00
|
|
|
print 'String from fcntl with F_SETLKW: ', `rv`
|
1996-12-17 13:41:09 -04:00
|
|
|
|
|
|
|
f.close()
|
|
|
|
os.unlink(filename)
|
2001-05-09 18:11:59 -03:00
|
|
|
|
|
|
|
|
|
|
|
# Again, but pass the file rather than numeric descriptor:
|
|
|
|
f = open(filename, 'w')
|
|
|
|
rv = fcntl.fcntl(f, fcntl.F_SETFL, os.O_NONBLOCK)
|
|
|
|
|
|
|
|
rv = fcntl.fcntl(f, fcntl.F_SETLKW, lockdata)
|
|
|
|
|
|
|
|
f.close()
|
|
|
|
os.unlink(filename)
|