blob: 350e2330262b9e36277a47480a0236f110ad0246 [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"""
Roger E. Massefb01d4b1996-12-17 17:41:09 +00006import fcntl
Gregory P. Smitha5cfcad2008-03-19 23:03:25 +00007import os
8import struct
9import sys
Brett Cannon1f5182b2008-03-13 21:09:28 +000010import unittest
11from test.test_support import verbose, TESTFN, unlink, run_unittest
Roger E. Massefb01d4b1996-12-17 17:41:09 +000012
Gregory P. Smith6af3db82008-03-20 05:41:53 +000013# TODO - Write tests for flock() and lockf().
Martin v. Löwisa660a342001-10-18 22:07:48 +000014
Brett Cannon1f5182b2008-03-13 21:09:28 +000015def get_lockdata():
16 if sys.platform.startswith('atheos'):
17 start_len = "qq"
Hye-Shik Changac89f6e2005-04-04 15:21:04 +000018 else:
Brett Cannon1f5182b2008-03-13 21:09:28 +000019 try:
20 os.O_LARGEFILE
21 except AttributeError:
22 start_len = "ll"
23 else:
24 start_len = "qq"
Fred Drakebc7809b2001-05-09 21:11:59 +000025
Brett Cannon1f5182b2008-03-13 21:09:28 +000026 if sys.platform in ('netbsd1', 'netbsd2', 'netbsd3',
27 'Darwin1.2', 'darwin',
28 'freebsd2', 'freebsd3', 'freebsd4', 'freebsd5',
29 'freebsd6', 'freebsd7', 'freebsd8',
30 'bsdos2', 'bsdos3', 'bsdos4',
31 'openbsd', 'openbsd2', 'openbsd3', 'openbsd4'):
32 if struct.calcsize('l') == 8:
33 off_t = 'l'
34 pid_t = 'i'
35 else:
36 off_t = 'lxxxx'
37 pid_t = 'l'
38 lockdata = struct.pack(off_t + off_t + pid_t + 'hh', 0, 0, 0,
39 fcntl.F_WRLCK, 0)
40 elif sys.platform in ['aix3', 'aix4', 'hp-uxB', 'unixware7']:
41 lockdata = struct.pack('hhlllii', fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0)
42 elif sys.platform in ['os2emx']:
43 lockdata = None
44 else:
45 lockdata = struct.pack('hh'+start_len+'hh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
46 if lockdata:
47 if verbose:
48 print 'struct.pack: ', repr(lockdata)
49 return lockdata
Fred Drakebc7809b2001-05-09 21:11:59 +000050
Brett Cannon1f5182b2008-03-13 21:09:28 +000051lockdata = get_lockdata()
Fred Drakebc7809b2001-05-09 21:11:59 +000052
53
Brett Cannon1f5182b2008-03-13 21:09:28 +000054class TestFcntl(unittest.TestCase):
Fred Drakebc7809b2001-05-09 21:11:59 +000055
Brett Cannon1f5182b2008-03-13 21:09:28 +000056 def setUp(self):
57 self.f = None
Fred Drakebc7809b2001-05-09 21:11:59 +000058
Brett Cannon1f5182b2008-03-13 21:09:28 +000059 def tearDown(self):
Antoine Pitrou6c3064b2009-05-24 15:41:43 +000060 if self.f and not self.f.closed:
Brett Cannon1f5182b2008-03-13 21:09:28 +000061 self.f.close()
62 unlink(TESTFN)
63
64 def test_fcntl_fileno(self):
65 # the example from the library docs
66 self.f = open(TESTFN, 'w')
67 rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
68 if verbose:
69 print 'Status from fcntl with O_NONBLOCK: ', rv
70 if sys.platform not in ['os2emx']:
71 rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETLKW, lockdata)
72 if verbose:
73 print 'String from fcntl with F_SETLKW: ', repr(rv)
74 self.f.close()
75
76 def test_fcntl_file_descriptor(self):
77 # again, but pass the file rather than numeric descriptor
78 self.f = open(TESTFN, 'w')
79 rv = fcntl.fcntl(self.f, fcntl.F_SETFL, os.O_NONBLOCK)
80 if sys.platform not in ['os2emx']:
81 rv = fcntl.fcntl(self.f, fcntl.F_SETLKW, lockdata)
82 self.f.close()
83
Antoine Pitrou6c3064b2009-05-24 15:41:43 +000084 def test_fcntl_64_bit(self):
85 # Issue #1309352: fcntl shouldn't fail when the third arg fits in a
86 # C 'long' but not in a C 'int'.
87 try:
88 cmd = fcntl.F_NOTIFY
89 # This flag is larger than 2**31 in 64-bit builds
90 flags = fcntl.DN_MULTISHOT
91 except AttributeError:
Antoine Pitroubcc2f1a2009-05-24 20:07:45 +000092 # F_NOTIFY or DN_MULTISHOT unavailable, skipping
93 return
Antoine Pitrou6c3064b2009-05-24 15:41:43 +000094 fd = os.open(os.path.dirname(os.path.abspath(TESTFN)), os.O_RDONLY)
95 try:
96 fcntl.fcntl(fd, cmd, flags)
97 finally:
98 os.close(fd)
99
Brett Cannon1f5182b2008-03-13 21:09:28 +0000100
101def test_main():
102 run_unittest(TestFcntl)
103
104if __name__ == '__main__':
105 test_main()