blob: 09cb047298e8d359de9b478aaf01239f9ca7c547 [file] [log] [blame]
Roger E. Massefb01d4b1996-12-17 17:41:09 +00001"""Test program for the fcntl C module.
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002
3OS/2+EMX doesn't support the file locking operations.
4
Roger E. Massefb01d4b1996-12-17 17:41:09 +00005"""
Gregory P. Smithe5aefa42013-03-31 10:10:50 -07006import platform
Christian Heimese25f35e2008-03-20 10:49:03 +00007import os
8import struct
9import sys
Serhiy Storchaka441d30f2013-01-19 12:26:26 +020010import _testcapi
Christian Heimesdd15f6c2008-03-16 00:07:10 +000011import unittest
R. David Murraya21e4ca2009-03-31 23:16:50 +000012from test.support import verbose, TESTFN, unlink, run_unittest, import_module
13
14# Skip test if no fnctl module.
15fcntl = import_module('fcntl')
16
Roger E. Massefb01d4b1996-12-17 17:41:09 +000017
Christian Heimese25f35e2008-03-20 10:49:03 +000018# TODO - Write tests for flock() and lockf().
Martin v. Löwisa660a342001-10-18 22:07:48 +000019
Christian Heimesdd15f6c2008-03-16 00:07:10 +000020def get_lockdata():
Antoine Pitrou6103ab12009-10-24 20:11:21 +000021 try:
22 os.O_LARGEFILE
23 except AttributeError:
24 start_len = "ll"
Hye-Shik Changac89f6e2005-04-04 15:21:04 +000025 else:
Antoine Pitrou6103ab12009-10-24 20:11:21 +000026 start_len = "qq"
Fred Drakebc7809b2001-05-09 21:11:59 +000027
Charles-François Natali6cea35a2011-08-23 19:46:46 +020028 if (sys.platform.startswith(('netbsd', 'freebsd', 'openbsd', 'bsdos'))
29 or sys.platform == 'darwin'):
Christian Heimesdd15f6c2008-03-16 00:07:10 +000030 if struct.calcsize('l') == 8:
31 off_t = 'l'
32 pid_t = 'i'
33 else:
34 off_t = 'lxxxx'
35 pid_t = 'l'
36 lockdata = struct.pack(off_t + off_t + pid_t + 'hh', 0, 0, 0,
37 fcntl.F_WRLCK, 0)
38 elif sys.platform in ['aix3', 'aix4', 'hp-uxB', 'unixware7']:
39 lockdata = struct.pack('hhlllii', fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0)
40 elif sys.platform in ['os2emx']:
41 lockdata = None
42 else:
43 lockdata = struct.pack('hh'+start_len+'hh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
44 if lockdata:
45 if verbose:
46 print('struct.pack: ', repr(lockdata))
47 return lockdata
Fred Drakebc7809b2001-05-09 21:11:59 +000048
Christian Heimesdd15f6c2008-03-16 00:07:10 +000049lockdata = get_lockdata()
Roger E. Massefb01d4b1996-12-17 17:41:09 +000050
Christian Heimesdd15f6c2008-03-16 00:07:10 +000051class TestFcntl(unittest.TestCase):
52
53 def setUp(self):
54 self.f = None
55
56 def tearDown(self):
Antoine Pitrou61f77b52009-05-24 15:46:13 +000057 if self.f and not self.f.closed:
Christian Heimesdd15f6c2008-03-16 00:07:10 +000058 self.f.close()
59 unlink(TESTFN)
60
61 def test_fcntl_fileno(self):
62 # the example from the library docs
Victor Stinnera6d2c762011-06-30 18:20:11 +020063 self.f = open(TESTFN, 'wb')
Christian Heimesdd15f6c2008-03-16 00:07:10 +000064 rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
65 if verbose:
66 print('Status from fcntl with O_NONBLOCK: ', rv)
67 if sys.platform not in ['os2emx']:
68 rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETLKW, lockdata)
69 if verbose:
70 print('String from fcntl with F_SETLKW: ', repr(rv))
71 self.f.close()
72
73 def test_fcntl_file_descriptor(self):
74 # again, but pass the file rather than numeric descriptor
Victor Stinnera6d2c762011-06-30 18:20:11 +020075 self.f = open(TESTFN, 'wb')
Christian Heimesdd15f6c2008-03-16 00:07:10 +000076 rv = fcntl.fcntl(self.f, fcntl.F_SETFL, os.O_NONBLOCK)
77 if sys.platform not in ['os2emx']:
78 rv = fcntl.fcntl(self.f, fcntl.F_SETLKW, lockdata)
79 self.f.close()
Fred Drakebc7809b2001-05-09 21:11:59 +000080
Serhiy Storchaka441d30f2013-01-19 12:26:26 +020081 def test_fcntl_bad_file(self):
82 class F:
83 def __init__(self, fn):
84 self.fn = fn
85 def fileno(self):
86 return self.fn
87 self.assertRaises(ValueError, fcntl.fcntl, -1, fcntl.F_SETFL, os.O_NONBLOCK)
88 self.assertRaises(ValueError, fcntl.fcntl, F(-1), fcntl.F_SETFL, os.O_NONBLOCK)
89 self.assertRaises(TypeError, fcntl.fcntl, 'spam', fcntl.F_SETFL, os.O_NONBLOCK)
90 self.assertRaises(TypeError, fcntl.fcntl, F('spam'), fcntl.F_SETFL, os.O_NONBLOCK)
91 # Issue 15989
92 self.assertRaises(OverflowError, fcntl.fcntl, _testcapi.INT_MAX + 1,
93 fcntl.F_SETFL, os.O_NONBLOCK)
94 self.assertRaises(OverflowError, fcntl.fcntl, F(_testcapi.INT_MAX + 1),
95 fcntl.F_SETFL, os.O_NONBLOCK)
96 self.assertRaises(OverflowError, fcntl.fcntl, _testcapi.INT_MIN - 1,
97 fcntl.F_SETFL, os.O_NONBLOCK)
98 self.assertRaises(OverflowError, fcntl.fcntl, F(_testcapi.INT_MIN - 1),
99 fcntl.F_SETFL, os.O_NONBLOCK)
100
Gregory P. Smithe5aefa42013-03-31 10:10:50 -0700101 @unittest.skipIf(
102 platform.machine().startswith('arm') and platform.system() == 'Linux',
103 "ARM Linux returns EINVAL for F_NOTIFY DN_MULTISHOT")
Antoine Pitrou61f77b52009-05-24 15:46:13 +0000104 def test_fcntl_64_bit(self):
105 # Issue #1309352: fcntl shouldn't fail when the third arg fits in a
106 # C 'long' but not in a C 'int'.
107 try:
108 cmd = fcntl.F_NOTIFY
109 # This flag is larger than 2**31 in 64-bit builds
110 flags = fcntl.DN_MULTISHOT
111 except AttributeError:
112 self.skipTest("F_NOTIFY or DN_MULTISHOT unavailable")
113 fd = os.open(os.path.dirname(os.path.abspath(TESTFN)), os.O_RDONLY)
114 try:
115 fcntl.fcntl(fd, cmd, flags)
116 finally:
117 os.close(fd)
118
Fred Drakebc7809b2001-05-09 21:11:59 +0000119
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000120def test_main():
121 run_unittest(TestFcntl)
Fred Drakebc7809b2001-05-09 21:11:59 +0000122
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000123if __name__ == '__main__':
124 test_main()