blob: a45140f6611d2947d3c638284f0f59e454083ae4 [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
Serhiy Storchaka74f49ab2013-01-19 12:55:39 +02009import _testcapi
Brett Cannon1f5182b2008-03-13 21:09:28 +000010import unittest
R. David Murray597ebab2009-03-31 18:32:17 +000011from test.test_support import (verbose, TESTFN, unlink, run_unittest,
12 import_module)
13
Ezio Melotti6c61a5a2013-08-23 23:06:31 +030014# Skip test if no fcntl module.
R. David Murray597ebab2009-03-31 18:32:17 +000015fcntl = import_module('fcntl')
16
Roger E. Massefb01d4b1996-12-17 17:41:09 +000017
Gregory P. Smith6af3db82008-03-20 05:41:53 +000018# TODO - Write tests for flock() and lockf().
Martin v. Löwisa660a342001-10-18 22:07:48 +000019
Brett Cannon1f5182b2008-03-13 21:09:28 +000020def get_lockdata():
21 if sys.platform.startswith('atheos'):
22 start_len = "qq"
Hye-Shik Changac89f6e2005-04-04 15:21:04 +000023 else:
Brett Cannon1f5182b2008-03-13 21:09:28 +000024 try:
25 os.O_LARGEFILE
26 except AttributeError:
27 start_len = "ll"
28 else:
29 start_len = "qq"
Fred Drakebc7809b2001-05-09 21:11:59 +000030
Charles-François Natalicdaafe02011-08-23 19:42:02 +020031 if (sys.platform.startswith(('netbsd', 'freebsd', 'openbsd', 'bsdos'))
32 or sys.platform == 'darwin'):
Brett Cannon1f5182b2008-03-13 21:09:28 +000033 if struct.calcsize('l') == 8:
34 off_t = 'l'
35 pid_t = 'i'
36 else:
37 off_t = 'lxxxx'
38 pid_t = 'l'
39 lockdata = struct.pack(off_t + off_t + pid_t + 'hh', 0, 0, 0,
40 fcntl.F_WRLCK, 0)
41 elif sys.platform in ['aix3', 'aix4', 'hp-uxB', 'unixware7']:
42 lockdata = struct.pack('hhlllii', fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0)
43 elif sys.platform in ['os2emx']:
44 lockdata = None
45 else:
46 lockdata = struct.pack('hh'+start_len+'hh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
47 if lockdata:
48 if verbose:
49 print 'struct.pack: ', repr(lockdata)
50 return lockdata
Fred Drakebc7809b2001-05-09 21:11:59 +000051
Brett Cannon1f5182b2008-03-13 21:09:28 +000052lockdata = get_lockdata()
Fred Drakebc7809b2001-05-09 21:11:59 +000053
54
Brett Cannon1f5182b2008-03-13 21:09:28 +000055class TestFcntl(unittest.TestCase):
Fred Drakebc7809b2001-05-09 21:11:59 +000056
Brett Cannon1f5182b2008-03-13 21:09:28 +000057 def setUp(self):
58 self.f = None
Fred Drakebc7809b2001-05-09 21:11:59 +000059
Brett Cannon1f5182b2008-03-13 21:09:28 +000060 def tearDown(self):
Antoine Pitroud49e3752009-05-24 15:40:09 +000061 if self.f and not self.f.closed:
Brett Cannon1f5182b2008-03-13 21:09:28 +000062 self.f.close()
63 unlink(TESTFN)
64
65 def test_fcntl_fileno(self):
66 # the example from the library docs
67 self.f = open(TESTFN, 'w')
68 rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
69 if verbose:
70 print 'Status from fcntl with O_NONBLOCK: ', rv
71 if sys.platform not in ['os2emx']:
72 rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETLKW, lockdata)
73 if verbose:
74 print 'String from fcntl with F_SETLKW: ', repr(rv)
75 self.f.close()
76
77 def test_fcntl_file_descriptor(self):
78 # again, but pass the file rather than numeric descriptor
79 self.f = open(TESTFN, 'w')
80 rv = fcntl.fcntl(self.f, fcntl.F_SETFL, os.O_NONBLOCK)
81 if sys.platform not in ['os2emx']:
82 rv = fcntl.fcntl(self.f, fcntl.F_SETLKW, lockdata)
83 self.f.close()
84
Serhiy Storchaka74f49ab2013-01-19 12:55:39 +020085 def test_fcntl_bad_file(self):
86 class F:
87 def __init__(self, fn):
88 self.fn = fn
89 def fileno(self):
90 return self.fn
91 self.assertRaises(ValueError, fcntl.fcntl, -1, fcntl.F_SETFL, os.O_NONBLOCK)
92 self.assertRaises(ValueError, fcntl.fcntl, F(-1), fcntl.F_SETFL, os.O_NONBLOCK)
93 self.assertRaises(TypeError, fcntl.fcntl, 'spam', fcntl.F_SETFL, os.O_NONBLOCK)
94 self.assertRaises(TypeError, fcntl.fcntl, F('spam'), fcntl.F_SETFL, os.O_NONBLOCK)
95 # Issue 15989
96 self.assertRaises(ValueError, fcntl.fcntl, _testcapi.INT_MAX + 1,
97 fcntl.F_SETFL, os.O_NONBLOCK)
98 self.assertRaises(ValueError, fcntl.fcntl, F(_testcapi.INT_MAX + 1),
99 fcntl.F_SETFL, os.O_NONBLOCK)
100 self.assertRaises(ValueError, fcntl.fcntl, _testcapi.INT_MIN - 1,
101 fcntl.F_SETFL, os.O_NONBLOCK)
102 self.assertRaises(ValueError, fcntl.fcntl, F(_testcapi.INT_MIN - 1),
103 fcntl.F_SETFL, os.O_NONBLOCK)
104
Antoine Pitroud49e3752009-05-24 15:40:09 +0000105 def test_fcntl_64_bit(self):
106 # Issue #1309352: fcntl shouldn't fail when the third arg fits in a
107 # C 'long' but not in a C 'int'.
108 try:
109 cmd = fcntl.F_NOTIFY
110 # This flag is larger than 2**31 in 64-bit builds
111 flags = fcntl.DN_MULTISHOT
112 except AttributeError:
113 self.skipTest("F_NOTIFY or DN_MULTISHOT unavailable")
114 fd = os.open(os.path.dirname(os.path.abspath(TESTFN)), os.O_RDONLY)
115 try:
Gregory P. Smithd899caa2013-11-25 04:45:27 +0000116 # This will raise OverflowError if issue1309352 is present.
Antoine Pitroud49e3752009-05-24 15:40:09 +0000117 fcntl.fcntl(fd, cmd, flags)
Gregory P. Smithd899caa2013-11-25 04:45:27 +0000118 except IOError:
119 pass # Running on a system that doesn't support these flags.
Antoine Pitroud49e3752009-05-24 15:40:09 +0000120 finally:
121 os.close(fd)
122
Brett Cannon1f5182b2008-03-13 21:09:28 +0000123
124def test_main():
125 run_unittest(TestFcntl)
126
127if __name__ == '__main__':
128 test_main()