blob: 0ce3a5dd7ae6a3596413fcd0e965923473aaf750 [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"""
Christian Heimese25f35e2008-03-20 10:49:03 +00006import os
7import struct
8import sys
Serhiy Storchaka441d30f2013-01-19 12:26:26 +02009import _testcapi
Christian Heimesdd15f6c2008-03-16 00:07:10 +000010import unittest
R. David Murraya21e4ca2009-03-31 23:16:50 +000011from test.support import verbose, TESTFN, unlink, run_unittest, import_module
12
13# Skip test if no fnctl module.
14fcntl = import_module('fcntl')
15
Roger E. Massefb01d4b1996-12-17 17:41:09 +000016
Christian Heimese25f35e2008-03-20 10:49:03 +000017# TODO - Write tests for flock() and lockf().
Martin v. Löwisa660a342001-10-18 22:07:48 +000018
Christian Heimesdd15f6c2008-03-16 00:07:10 +000019def get_lockdata():
Antoine Pitrou6103ab12009-10-24 20:11:21 +000020 try:
21 os.O_LARGEFILE
22 except AttributeError:
23 start_len = "ll"
Hye-Shik Changac89f6e2005-04-04 15:21:04 +000024 else:
Antoine Pitrou6103ab12009-10-24 20:11:21 +000025 start_len = "qq"
Fred Drakebc7809b2001-05-09 21:11:59 +000026
Charles-François Natali6cea35a2011-08-23 19:46:46 +020027 if (sys.platform.startswith(('netbsd', 'freebsd', 'openbsd', 'bsdos'))
28 or sys.platform == 'darwin'):
Christian Heimesdd15f6c2008-03-16 00:07:10 +000029 if struct.calcsize('l') == 8:
30 off_t = 'l'
31 pid_t = 'i'
32 else:
33 off_t = 'lxxxx'
34 pid_t = 'l'
35 lockdata = struct.pack(off_t + off_t + pid_t + 'hh', 0, 0, 0,
36 fcntl.F_WRLCK, 0)
37 elif sys.platform in ['aix3', 'aix4', 'hp-uxB', 'unixware7']:
38 lockdata = struct.pack('hhlllii', fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0)
39 elif sys.platform in ['os2emx']:
40 lockdata = None
41 else:
42 lockdata = struct.pack('hh'+start_len+'hh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
43 if lockdata:
44 if verbose:
45 print('struct.pack: ', repr(lockdata))
46 return lockdata
Fred Drakebc7809b2001-05-09 21:11:59 +000047
Christian Heimesdd15f6c2008-03-16 00:07:10 +000048lockdata = get_lockdata()
Roger E. Massefb01d4b1996-12-17 17:41:09 +000049
Christian Heimesdd15f6c2008-03-16 00:07:10 +000050class TestFcntl(unittest.TestCase):
51
52 def setUp(self):
53 self.f = None
54
55 def tearDown(self):
Antoine Pitrou61f77b52009-05-24 15:46:13 +000056 if self.f and not self.f.closed:
Christian Heimesdd15f6c2008-03-16 00:07:10 +000057 self.f.close()
58 unlink(TESTFN)
59
60 def test_fcntl_fileno(self):
61 # the example from the library docs
Victor Stinnera6d2c762011-06-30 18:20:11 +020062 self.f = open(TESTFN, 'wb')
Christian Heimesdd15f6c2008-03-16 00:07:10 +000063 rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
64 if verbose:
65 print('Status from fcntl with O_NONBLOCK: ', rv)
66 if sys.platform not in ['os2emx']:
67 rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETLKW, lockdata)
68 if verbose:
69 print('String from fcntl with F_SETLKW: ', repr(rv))
70 self.f.close()
71
72 def test_fcntl_file_descriptor(self):
73 # again, but pass the file rather than numeric descriptor
Victor Stinnera6d2c762011-06-30 18:20:11 +020074 self.f = open(TESTFN, 'wb')
Christian Heimesdd15f6c2008-03-16 00:07:10 +000075 rv = fcntl.fcntl(self.f, fcntl.F_SETFL, os.O_NONBLOCK)
76 if sys.platform not in ['os2emx']:
77 rv = fcntl.fcntl(self.f, fcntl.F_SETLKW, lockdata)
78 self.f.close()
Fred Drakebc7809b2001-05-09 21:11:59 +000079
Serhiy Storchaka441d30f2013-01-19 12:26:26 +020080 def test_fcntl_bad_file(self):
81 class F:
82 def __init__(self, fn):
83 self.fn = fn
84 def fileno(self):
85 return self.fn
86 self.assertRaises(ValueError, fcntl.fcntl, -1, fcntl.F_SETFL, os.O_NONBLOCK)
87 self.assertRaises(ValueError, fcntl.fcntl, F(-1), fcntl.F_SETFL, os.O_NONBLOCK)
88 self.assertRaises(TypeError, fcntl.fcntl, 'spam', fcntl.F_SETFL, os.O_NONBLOCK)
89 self.assertRaises(TypeError, fcntl.fcntl, F('spam'), fcntl.F_SETFL, os.O_NONBLOCK)
90 # Issue 15989
91 self.assertRaises(OverflowError, fcntl.fcntl, _testcapi.INT_MAX + 1,
92 fcntl.F_SETFL, os.O_NONBLOCK)
93 self.assertRaises(OverflowError, fcntl.fcntl, F(_testcapi.INT_MAX + 1),
94 fcntl.F_SETFL, os.O_NONBLOCK)
95 self.assertRaises(OverflowError, fcntl.fcntl, _testcapi.INT_MIN - 1,
96 fcntl.F_SETFL, os.O_NONBLOCK)
97 self.assertRaises(OverflowError, fcntl.fcntl, F(_testcapi.INT_MIN - 1),
98 fcntl.F_SETFL, os.O_NONBLOCK)
99
Antoine Pitrou61f77b52009-05-24 15:46:13 +0000100 def test_fcntl_64_bit(self):
101 # Issue #1309352: fcntl shouldn't fail when the third arg fits in a
102 # C 'long' but not in a C 'int'.
103 try:
104 cmd = fcntl.F_NOTIFY
105 # This flag is larger than 2**31 in 64-bit builds
106 flags = fcntl.DN_MULTISHOT
107 except AttributeError:
108 self.skipTest("F_NOTIFY or DN_MULTISHOT unavailable")
109 fd = os.open(os.path.dirname(os.path.abspath(TESTFN)), os.O_RDONLY)
110 try:
111 fcntl.fcntl(fd, cmd, flags)
112 finally:
113 os.close(fd)
114
Fred Drakebc7809b2001-05-09 21:11:59 +0000115
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000116def test_main():
117 run_unittest(TestFcntl)
Fred Drakebc7809b2001-05-09 21:11:59 +0000118
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000119if __name__ == '__main__':
120 test_main()