blob: 2d800b2a5c9e5aa9b64f2413c8b1d7c1a8c30ad8 [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
Georg Brandld819c132006-06-21 17:52:36 +000023if sys.platform in ('netbsd1', 'netbsd2', 'netbsd3',
24 'Darwin1.2', 'darwin',
25 'freebsd2', 'freebsd3', 'freebsd4', 'freebsd5',
26 'freebsd6', 'freebsd7',
Guido van Rossum0cf46bc1999-04-19 17:22:12 +000027 'bsdos2', 'bsdos3', 'bsdos4',
Neal Norwitz541a48b2006-09-05 02:54:42 +000028 'openbsd', 'openbsd2', 'openbsd3', 'openbsd4'):
Hye-Shik Changac89f6e2005-04-04 15:21:04 +000029 if struct.calcsize('l') == 8:
30 off_t = 'l'
31 pid_t = 'i'
32 else:
33 off_t = 'lxxxx'
34 pid_t = 'l'
35 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 +000036elif sys.platform in ['aix3', 'aix4', 'hp-uxB', 'unixware7']:
Fred Drakebc7809b2001-05-09 21:11:59 +000037 lockdata = struct.pack('hhlllii', fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0)
Andrew MacIntyre5cef5712002-02-24 05:32:32 +000038elif sys.platform in ['os2emx']:
39 lockdata = None
Guido van Rossum91221c21997-12-02 20:30:29 +000040else:
Martin v. Löwisa660a342001-10-18 22:07:48 +000041 lockdata = struct.pack('hh'+start_len+'hh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
Andrew MacIntyre5cef5712002-02-24 05:32:32 +000042if lockdata:
43 if verbose:
Walter Dörwald70a6b492004-02-12 17:35:32 +000044 print 'struct.pack: ', repr(lockdata)
Fred Drakebc7809b2001-05-09 21:11:59 +000045
46# the example from the library docs
47f = open(filename, 'w')
48rv = fcntl.fcntl(f.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
49if verbose:
Hye-Shik Changac89f6e2005-04-04 15:21:04 +000050 print 'Status from fcntl with O_NONBLOCK: ', rv
Fred Drakebc7809b2001-05-09 21:11:59 +000051
Andrew MacIntyre5cef5712002-02-24 05:32:32 +000052if sys.platform not in ['os2emx']:
53 rv = fcntl.fcntl(f.fileno(), fcntl.F_SETLKW, lockdata)
54 if verbose:
Walter Dörwald70a6b492004-02-12 17:35:32 +000055 print 'String from fcntl with F_SETLKW: ', repr(rv)
Roger E. Massefb01d4b1996-12-17 17:41:09 +000056
57f.close()
58os.unlink(filename)
Fred Drakebc7809b2001-05-09 21:11:59 +000059
60
61# Again, but pass the file rather than numeric descriptor:
62f = open(filename, 'w')
63rv = fcntl.fcntl(f, fcntl.F_SETFL, os.O_NONBLOCK)
64
Andrew MacIntyre5cef5712002-02-24 05:32:32 +000065if sys.platform not in ['os2emx']:
66 rv = fcntl.fcntl(f, fcntl.F_SETLKW, lockdata)
Fred Drakebc7809b2001-05-09 21:11:59 +000067
68f.close()
69os.unlink(filename)