1996-12-17 13:41:09 -04:00
|
|
|
#! /usr/bin/env python
|
|
|
|
"""Test program for the fcntl C module.
|
2002-02-24 01:32:32 -04:00
|
|
|
OS/2+EMX doesn't support the file locking operations.
|
1996-12-17 13:41:09 -04:00
|
|
|
Roger E. Masse
|
|
|
|
"""
|
|
|
|
import struct
|
|
|
|
import fcntl
|
1997-12-02 16:30:29 -04:00
|
|
|
import os, sys
|
2002-07-23 16:04:11 -03:00
|
|
|
from test.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"
|
|
|
|
|
2002-06-11 03:22:31 -03:00
|
|
|
if sys.platform.startswith('atheos'):
|
|
|
|
start_len = "qq"
|
|
|
|
|
2001-12-05 19:27:32 -04:00
|
|
|
if sys.platform in ('netbsd1', 'Darwin1.2', 'darwin',
|
2004-08-18 12:13:41 -03:00
|
|
|
'freebsd2', 'freebsd3', 'freebsd4', 'freebsd5', 'freebsd6',
|
2005-07-16 23:36:59 -03:00
|
|
|
'freebsd7',
|
1999-04-19 14:22:12 -03:00
|
|
|
'bsdos2', 'bsdos3', 'bsdos4',
|
2002-05-13 11:58:02 -03:00
|
|
|
'openbsd', 'openbsd2', 'openbsd3'):
|
2005-04-04 12:21:04 -03:00
|
|
|
if struct.calcsize('l') == 8:
|
|
|
|
off_t = 'l'
|
|
|
|
pid_t = 'i'
|
|
|
|
else:
|
|
|
|
off_t = 'lxxxx'
|
|
|
|
pid_t = 'l'
|
|
|
|
lockdata = struct.pack(off_t+off_t+pid_t+'hh', 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)
|
2002-02-24 01:32:32 -04:00
|
|
|
elif sys.platform in ['os2emx']:
|
|
|
|
lockdata = None
|
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)
|
2002-02-24 01:32:32 -04:00
|
|
|
if lockdata:
|
|
|
|
if verbose:
|
2004-02-12 13:35:32 -04:00
|
|
|
print 'struct.pack: ', repr(lockdata)
|
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:
|
2005-04-04 12:21:04 -03:00
|
|
|
print 'Status from fcntl with O_NONBLOCK: ', rv
|
2001-05-09 18:11:59 -03:00
|
|
|
|
2002-02-24 01:32:32 -04:00
|
|
|
if sys.platform not in ['os2emx']:
|
|
|
|
rv = fcntl.fcntl(f.fileno(), fcntl.F_SETLKW, lockdata)
|
|
|
|
if verbose:
|
2004-02-12 13:35:32 -04:00
|
|
|
print 'String from fcntl with F_SETLKW: ', repr(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)
|
|
|
|
|
2002-02-24 01:32:32 -04:00
|
|
|
if sys.platform not in ['os2emx']:
|
|
|
|
rv = fcntl.fcntl(f, fcntl.F_SETLKW, lockdata)
|
2001-05-09 18:11:59 -03:00
|
|
|
|
|
|
|
f.close()
|
|
|
|
os.unlink(filename)
|