blob: 2d176ea4a2ede1c7e7b68d3717fde2761fb67888 [file] [log] [blame]
Roger E. Massefb01d4b1996-12-17 17:41:09 +00001#! /usr/bin/env python
2"""Test program for the fcntl C module.
Andrew MacIntyre5cef5712002-02-24 05:32:32 +00003 OS/2+EMX doesn't support the file locking operations.
Roger E. Massefb01d4b1996-12-17 17:41:09 +00004 Roger E. Masse
5"""
6import struct
7import fcntl
Guido van Rossum91221c21997-12-02 20:30:29 +00008import os, sys
Barry Warsaw04f357c2002-07-23 19:04:11 +00009from test.test_support import verbose, TESTFN
Roger E. Massefb01d4b1996-12-17 17:41:09 +000010
Fred Drakef7ef15d2000-10-18 01:21:38 +000011filename = TESTFN
Roger E. Massefb01d4b1996-12-17 17:41:09 +000012
Martin v. Löwisa660a342001-10-18 22:07:48 +000013try:
14 os.O_LARGEFILE
15except AttributeError:
16 start_len = "ll"
17else:
18 start_len = "qq"
19
Martin v. Löwisf90ae202002-06-11 06:22:31 +000020if sys.platform.startswith('atheos'):
21 start_len = "qq"
22
Jack Jansen8a97f4a2001-12-05 23:27:32 +000023if sys.platform in ('netbsd1', 'Darwin1.2', 'darwin',
Hye-Shik Changf64700a2004-08-18 15:13:41 +000024 'freebsd2', 'freebsd3', 'freebsd4', 'freebsd5', 'freebsd6',
Guido van Rossum0cf46bc1999-04-19 17:22:12 +000025 'bsdos2', 'bsdos3', 'bsdos4',
Guido van Rossumdf4dabd2002-05-13 14:58:02 +000026 'openbsd', 'openbsd2', 'openbsd3'):
Hye-Shik Changac89f6e2005-04-04 15:21:04 +000027 if struct.calcsize('l') == 8:
28 off_t = 'l'
29 pid_t = 'i'
30 else:
31 off_t = 'lxxxx'
32 pid_t = 'l'
33 lockdata = struct.pack(off_t+off_t+pid_t+'hh', 0, 0, 0, fcntl.F_WRLCK, 0)
Guido van Rossum2242f2f2001-04-11 20:58:20 +000034elif sys.platform in ['aix3', 'aix4', 'hp-uxB', 'unixware7']:
Fred Drakebc7809b2001-05-09 21:11:59 +000035 lockdata = struct.pack('hhlllii', fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0)
Andrew MacIntyre5cef5712002-02-24 05:32:32 +000036elif sys.platform in ['os2emx']:
37 lockdata = None
Guido van Rossum91221c21997-12-02 20:30:29 +000038else:
Martin v. Löwisa660a342001-10-18 22:07:48 +000039 lockdata = struct.pack('hh'+start_len+'hh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
Andrew MacIntyre5cef5712002-02-24 05:32:32 +000040if lockdata:
41 if verbose:
Walter Dörwald70a6b492004-02-12 17:35:32 +000042 print 'struct.pack: ', repr(lockdata)
Fred Drakebc7809b2001-05-09 21:11:59 +000043
44# the example from the library docs
45f = open(filename, 'w')
46rv = fcntl.fcntl(f.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
47if verbose:
Hye-Shik Changac89f6e2005-04-04 15:21:04 +000048 print 'Status from fcntl with O_NONBLOCK: ', rv
Fred Drakebc7809b2001-05-09 21:11:59 +000049
Andrew MacIntyre5cef5712002-02-24 05:32:32 +000050if sys.platform not in ['os2emx']:
51 rv = fcntl.fcntl(f.fileno(), fcntl.F_SETLKW, lockdata)
52 if verbose:
Walter Dörwald70a6b492004-02-12 17:35:32 +000053 print 'String from fcntl with F_SETLKW: ', repr(rv)
Roger E. Massefb01d4b1996-12-17 17:41:09 +000054
55f.close()
56os.unlink(filename)
Fred Drakebc7809b2001-05-09 21:11:59 +000057
58
59# Again, but pass the file rather than numeric descriptor:
60f = open(filename, 'w')
61rv = fcntl.fcntl(f, fcntl.F_SETFL, os.O_NONBLOCK)
62
Andrew MacIntyre5cef5712002-02-24 05:32:32 +000063if sys.platform not in ['os2emx']:
64 rv = fcntl.fcntl(f, fcntl.F_SETLKW, lockdata)
Fred Drakebc7809b2001-05-09 21:11:59 +000065
66f.close()
67os.unlink(filename)