blob: 3a1803137dbf9318bb094cf12bb7c682fb7347a1 [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,
Serhiy Storchaka76249ea2014-02-07 10:06:05 +020011 import_module, cpython_only)
R. David Murray597ebab2009-03-31 18:32:17 +000012
Ezio Melotti6c61a5a2013-08-23 23:06:31 +030013# Skip test if no fcntl module.
R. David Murray597ebab2009-03-31 18:32:17 +000014fcntl = 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
Charles-François Natalicdaafe02011-08-23 19:42:02 +020030 if (sys.platform.startswith(('netbsd', 'freebsd', 'openbsd', 'bsdos'))
31 or sys.platform == 'darwin'):
Brett Cannon1f5182b2008-03-13 21:09:28 +000032 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
Serhiy Storchaka76249ea2014-02-07 10:06:05 +020054class BadFile:
55 def __init__(self, fn):
56 self.fn = fn
57 def fileno(self):
58 return self.fn
59
Brett Cannon1f5182b2008-03-13 21:09:28 +000060class TestFcntl(unittest.TestCase):
Fred Drakebc7809b2001-05-09 21:11:59 +000061
Brett Cannon1f5182b2008-03-13 21:09:28 +000062 def setUp(self):
63 self.f = None
Fred Drakebc7809b2001-05-09 21:11:59 +000064
Brett Cannon1f5182b2008-03-13 21:09:28 +000065 def tearDown(self):
Antoine Pitroud49e3752009-05-24 15:40:09 +000066 if self.f and not self.f.closed:
Brett Cannon1f5182b2008-03-13 21:09:28 +000067 self.f.close()
68 unlink(TESTFN)
69
70 def test_fcntl_fileno(self):
71 # the example from the library docs
72 self.f = open(TESTFN, 'w')
73 rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
74 if verbose:
75 print 'Status from fcntl with O_NONBLOCK: ', rv
76 if sys.platform not in ['os2emx']:
77 rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETLKW, lockdata)
78 if verbose:
79 print 'String from fcntl with F_SETLKW: ', repr(rv)
80 self.f.close()
81
82 def test_fcntl_file_descriptor(self):
83 # again, but pass the file rather than numeric descriptor
84 self.f = open(TESTFN, 'w')
85 rv = fcntl.fcntl(self.f, fcntl.F_SETFL, os.O_NONBLOCK)
86 if sys.platform not in ['os2emx']:
87 rv = fcntl.fcntl(self.f, fcntl.F_SETLKW, lockdata)
88 self.f.close()
89
Serhiy Storchaka74f49ab2013-01-19 12:55:39 +020090 def test_fcntl_bad_file(self):
Serhiy Storchaka76249ea2014-02-07 10:06:05 +020091 with self.assertRaises(ValueError):
92 fcntl.fcntl(-1, fcntl.F_SETFL, os.O_NONBLOCK)
93 with self.assertRaises(ValueError):
94 fcntl.fcntl(BadFile(-1), fcntl.F_SETFL, os.O_NONBLOCK)
95 with self.assertRaises(TypeError):
96 fcntl.fcntl('spam', fcntl.F_SETFL, os.O_NONBLOCK)
97 with self.assertRaises(TypeError):
98 fcntl.fcntl(BadFile('spam'), fcntl.F_SETFL, os.O_NONBLOCK)
99
100 @cpython_only
101 def test_fcntl_bad_file_overflow(self):
102 from _testcapi import INT_MAX, INT_MIN
Serhiy Storchaka74f49ab2013-01-19 12:55:39 +0200103 # Issue 15989
Serhiy Storchaka76249ea2014-02-07 10:06:05 +0200104 with self.assertRaises(ValueError):
105 fcntl.fcntl(INT_MAX + 1, fcntl.F_SETFL, os.O_NONBLOCK)
106 with self.assertRaises(ValueError):
107 fcntl.fcntl(BadFile(INT_MAX + 1), fcntl.F_SETFL, os.O_NONBLOCK)
108 with self.assertRaises(ValueError):
109 fcntl.fcntl(INT_MIN - 1, fcntl.F_SETFL, os.O_NONBLOCK)
110 with self.assertRaises(ValueError):
111 fcntl.fcntl(BadFile(INT_MIN - 1), fcntl.F_SETFL, os.O_NONBLOCK)
Serhiy Storchaka74f49ab2013-01-19 12:55:39 +0200112
Antoine Pitroud49e3752009-05-24 15:40:09 +0000113 def test_fcntl_64_bit(self):
114 # Issue #1309352: fcntl shouldn't fail when the third arg fits in a
115 # C 'long' but not in a C 'int'.
116 try:
117 cmd = fcntl.F_NOTIFY
118 # This flag is larger than 2**31 in 64-bit builds
119 flags = fcntl.DN_MULTISHOT
120 except AttributeError:
121 self.skipTest("F_NOTIFY or DN_MULTISHOT unavailable")
122 fd = os.open(os.path.dirname(os.path.abspath(TESTFN)), os.O_RDONLY)
123 try:
Gregory P. Smithd899caa2013-11-25 04:45:27 +0000124 # This will raise OverflowError if issue1309352 is present.
Antoine Pitroud49e3752009-05-24 15:40:09 +0000125 fcntl.fcntl(fd, cmd, flags)
Gregory P. Smithd899caa2013-11-25 04:45:27 +0000126 except IOError:
127 pass # Running on a system that doesn't support these flags.
Antoine Pitroud49e3752009-05-24 15:40:09 +0000128 finally:
129 os.close(fd)
130
Brett Cannon1f5182b2008-03-13 21:09:28 +0000131
132def test_main():
133 run_unittest(TestFcntl)
134
135if __name__ == '__main__':
136 test_main()