blob: c8ea7b7ea51e4ca945264a973b2e586d5482ac58 [file] [log] [blame]
Roger E. Massefb01d4b1996-12-17 17:41:09 +00001"""Test program for the fcntl C module.
Brett Cannon1f5182b2008-03-13 21:09:28 +00002
3OS/2+EMX doesn't support the file locking operations.
4
Roger E. Massefb01d4b1996-12-17 17:41:09 +00005"""
Gregory P. Smitha5cfcad2008-03-19 23:03:25 +00006import os
7import struct
8import sys
Brett Cannon1f5182b2008-03-13 21:09:28 +00009import unittest
R. David Murray597ebab2009-03-31 18:32:17 +000010from test.test_support import (verbose, TESTFN, unlink, run_unittest,
11 import_module)
12
13# Skip test if no fnctl module.
14fcntl = import_module('fcntl')
15
Roger E. Massefb01d4b1996-12-17 17:41:09 +000016
Gregory P. Smith6af3db82008-03-20 05:41:53 +000017# TODO - Write tests for flock() and lockf().
Martin v. Löwisa660a342001-10-18 22:07:48 +000018
Brett Cannon1f5182b2008-03-13 21:09:28 +000019def get_lockdata():
20 if sys.platform.startswith('atheos'):
21 start_len = "qq"
Hye-Shik Changac89f6e2005-04-04 15:21:04 +000022 else:
Brett Cannon1f5182b2008-03-13 21:09:28 +000023 try:
24 os.O_LARGEFILE
25 except AttributeError:
26 start_len = "ll"
27 else:
28 start_len = "qq"
Fred Drakebc7809b2001-05-09 21:11:59 +000029
Brett Cannon1f5182b2008-03-13 21:09:28 +000030 if sys.platform in ('netbsd1', 'netbsd2', 'netbsd3',
31 'Darwin1.2', 'darwin',
32 'freebsd2', 'freebsd3', 'freebsd4', 'freebsd5',
33 'freebsd6', 'freebsd7', 'freebsd8',
34 'bsdos2', 'bsdos3', 'bsdos4',
35 'openbsd', 'openbsd2', 'openbsd3', 'openbsd4'):
36 if struct.calcsize('l') == 8:
37 off_t = 'l'
38 pid_t = 'i'
39 else:
40 off_t = 'lxxxx'
41 pid_t = 'l'
42 lockdata = struct.pack(off_t + off_t + pid_t + 'hh', 0, 0, 0,
43 fcntl.F_WRLCK, 0)
44 elif sys.platform in ['aix3', 'aix4', 'hp-uxB', 'unixware7']:
45 lockdata = struct.pack('hhlllii', fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0)
46 elif sys.platform in ['os2emx']:
47 lockdata = None
48 else:
49 lockdata = struct.pack('hh'+start_len+'hh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
50 if lockdata:
51 if verbose:
52 print 'struct.pack: ', repr(lockdata)
53 return lockdata
Fred Drakebc7809b2001-05-09 21:11:59 +000054
Brett Cannon1f5182b2008-03-13 21:09:28 +000055lockdata = get_lockdata()
Fred Drakebc7809b2001-05-09 21:11:59 +000056
57
Brett Cannon1f5182b2008-03-13 21:09:28 +000058class TestFcntl(unittest.TestCase):
Fred Drakebc7809b2001-05-09 21:11:59 +000059
Brett Cannon1f5182b2008-03-13 21:09:28 +000060 def setUp(self):
61 self.f = None
Fred Drakebc7809b2001-05-09 21:11:59 +000062
Brett Cannon1f5182b2008-03-13 21:09:28 +000063 def tearDown(self):
Antoine Pitroud49e3752009-05-24 15:40:09 +000064 if self.f and not self.f.closed:
Brett Cannon1f5182b2008-03-13 21:09:28 +000065 self.f.close()
66 unlink(TESTFN)
67
68 def test_fcntl_fileno(self):
69 # the example from the library docs
70 self.f = open(TESTFN, 'w')
71 rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
72 if verbose:
73 print 'Status from fcntl with O_NONBLOCK: ', rv
74 if sys.platform not in ['os2emx']:
75 rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETLKW, lockdata)
76 if verbose:
77 print 'String from fcntl with F_SETLKW: ', repr(rv)
78 self.f.close()
79
80 def test_fcntl_file_descriptor(self):
81 # again, but pass the file rather than numeric descriptor
82 self.f = open(TESTFN, 'w')
83 rv = fcntl.fcntl(self.f, fcntl.F_SETFL, os.O_NONBLOCK)
84 if sys.platform not in ['os2emx']:
85 rv = fcntl.fcntl(self.f, fcntl.F_SETLKW, lockdata)
86 self.f.close()
87
Antoine Pitroud49e3752009-05-24 15:40:09 +000088 def test_fcntl_64_bit(self):
89 # Issue #1309352: fcntl shouldn't fail when the third arg fits in a
90 # C 'long' but not in a C 'int'.
91 try:
92 cmd = fcntl.F_NOTIFY
93 # This flag is larger than 2**31 in 64-bit builds
94 flags = fcntl.DN_MULTISHOT
95 except AttributeError:
96 self.skipTest("F_NOTIFY or DN_MULTISHOT unavailable")
97 fd = os.open(os.path.dirname(os.path.abspath(TESTFN)), os.O_RDONLY)
98 try:
99 fcntl.fcntl(fd, cmd, flags)
100 finally:
101 os.close(fd)
102
Brett Cannon1f5182b2008-03-13 21:09:28 +0000103
104def test_main():
105 run_unittest(TestFcntl)
106
107if __name__ == '__main__':
108 test_main()