blob: cc4f2134b312dfcf62f05ade5ee0154b635940bd [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
Fredrik Lundhf7850422001-01-17 21:51:36 +00009from 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',
Guido van Rossum2d218632000-08-29 14:57:27 +000024 'freebsd2', 'freebsd3', 'freebsd4', 'freebsd5',
Guido van Rossum0cf46bc1999-04-19 17:22:12 +000025 'bsdos2', 'bsdos3', 'bsdos4',
Guido van Rossumdf4dabd2002-05-13 14:58:02 +000026 'openbsd', 'openbsd2', 'openbsd3'):
Fred Drakebc7809b2001-05-09 21:11:59 +000027 lockdata = struct.pack('lxxxxlxxxxlhh', 0, 0, 0, fcntl.F_WRLCK, 0)
Guido van Rossum2242f2f2001-04-11 20:58:20 +000028elif sys.platform in ['aix3', 'aix4', 'hp-uxB', 'unixware7']:
Fred Drakebc7809b2001-05-09 21:11:59 +000029 lockdata = struct.pack('hhlllii', fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0)
Andrew MacIntyre5cef5712002-02-24 05:32:32 +000030elif sys.platform in ['os2emx']:
31 lockdata = None
Guido van Rossum91221c21997-12-02 20:30:29 +000032else:
Martin v. Löwisa660a342001-10-18 22:07:48 +000033 lockdata = struct.pack('hh'+start_len+'hh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
Andrew MacIntyre5cef5712002-02-24 05:32:32 +000034if lockdata:
35 if verbose:
36 print 'struct.pack: ', `lockdata`
Fred Drakebc7809b2001-05-09 21:11:59 +000037
38# the example from the library docs
39f = open(filename, 'w')
40rv = fcntl.fcntl(f.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
41if verbose:
42 print 'Status from fnctl with O_NONBLOCK: ', rv
43
Andrew MacIntyre5cef5712002-02-24 05:32:32 +000044if sys.platform not in ['os2emx']:
45 rv = fcntl.fcntl(f.fileno(), fcntl.F_SETLKW, lockdata)
46 if verbose:
47 print 'String from fcntl with F_SETLKW: ', `rv`
Roger E. Massefb01d4b1996-12-17 17:41:09 +000048
49f.close()
50os.unlink(filename)
Fred Drakebc7809b2001-05-09 21:11:59 +000051
52
53# Again, but pass the file rather than numeric descriptor:
54f = open(filename, 'w')
55rv = fcntl.fcntl(f, fcntl.F_SETFL, os.O_NONBLOCK)
56
Andrew MacIntyre5cef5712002-02-24 05:32:32 +000057if sys.platform not in ['os2emx']:
58 rv = fcntl.fcntl(f, fcntl.F_SETLKW, lockdata)
Fred Drakebc7809b2001-05-09 21:11:59 +000059
60f.close()
61os.unlink(filename)