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