blob: 9dd887ab1a5f4d00f9d2c261c849f5389610fc9e [file] [log] [blame]
Roger E. Massefb01d4b1996-12-17 17:41:09 +00001"""Test program for the fcntl C module.
Roger E. Massefb01d4b1996-12-17 17:41:09 +00002"""
Gregory P. Smithe5aefa42013-03-31 10:10:50 -07003import platform
Christian Heimese25f35e2008-03-20 10:49:03 +00004import os
5import struct
6import sys
Serhiy Storchaka78980432013-01-15 01:12:17 +02007import _testcapi
Christian Heimesdd15f6c2008-03-16 00:07:10 +00008import unittest
R. David Murraya21e4ca2009-03-31 23:16:50 +00009from test.support import verbose, TESTFN, unlink, run_unittest, import_module
10
11# Skip test if no fnctl module.
12fcntl = import_module('fcntl')
13
Roger E. Massefb01d4b1996-12-17 17:41:09 +000014
Christian Heimese25f35e2008-03-20 10:49:03 +000015# TODO - Write tests for flock() and lockf().
Martin v. Löwisa660a342001-10-18 22:07:48 +000016
Christian Heimesdd15f6c2008-03-16 00:07:10 +000017def get_lockdata():
Antoine Pitrou6103ab12009-10-24 20:11:21 +000018 try:
19 os.O_LARGEFILE
20 except AttributeError:
21 start_len = "ll"
Hye-Shik Changac89f6e2005-04-04 15:21:04 +000022 else:
Antoine Pitrou6103ab12009-10-24 20:11:21 +000023 start_len = "qq"
Fred Drakebc7809b2001-05-09 21:11:59 +000024
Victor Stinnere6747472011-08-21 00:39:18 +020025 if (sys.platform.startswith(('netbsd', 'freebsd', 'openbsd', 'bsdos'))
26 or sys.platform == 'darwin'):
Christian Heimesdd15f6c2008-03-16 00:07:10 +000027 if struct.calcsize('l') == 8:
28 off_t = 'l'
29 pid_t = 'i'
30 else:
31 off_t = 'lxxxx'
32 pid_t = 'l'
33 lockdata = struct.pack(off_t + off_t + pid_t + 'hh', 0, 0, 0,
34 fcntl.F_WRLCK, 0)
35 elif sys.platform in ['aix3', 'aix4', 'hp-uxB', 'unixware7']:
36 lockdata = struct.pack('hhlllii', fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0)
Christian Heimesdd15f6c2008-03-16 00:07:10 +000037 else:
38 lockdata = struct.pack('hh'+start_len+'hh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
39 if lockdata:
40 if verbose:
41 print('struct.pack: ', repr(lockdata))
42 return lockdata
Fred Drakebc7809b2001-05-09 21:11:59 +000043
Christian Heimesdd15f6c2008-03-16 00:07:10 +000044lockdata = get_lockdata()
Roger E. Massefb01d4b1996-12-17 17:41:09 +000045
Christian Heimesdd15f6c2008-03-16 00:07:10 +000046class TestFcntl(unittest.TestCase):
47
48 def setUp(self):
49 self.f = None
50
51 def tearDown(self):
Antoine Pitrou61f77b52009-05-24 15:46:13 +000052 if self.f and not self.f.closed:
Christian Heimesdd15f6c2008-03-16 00:07:10 +000053 self.f.close()
54 unlink(TESTFN)
55
56 def test_fcntl_fileno(self):
57 # the example from the library docs
Victor Stinnera6d2c762011-06-30 18:20:11 +020058 self.f = open(TESTFN, 'wb')
Christian Heimesdd15f6c2008-03-16 00:07:10 +000059 rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
60 if verbose:
61 print('Status from fcntl with O_NONBLOCK: ', rv)
Jesus Ceaf1af7052012-10-05 02:48:46 +020062 rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETLKW, lockdata)
63 if verbose:
64 print('String from fcntl with F_SETLKW: ', repr(rv))
Christian Heimesdd15f6c2008-03-16 00:07:10 +000065 self.f.close()
66
67 def test_fcntl_file_descriptor(self):
68 # again, but pass the file rather than numeric descriptor
Victor Stinnera6d2c762011-06-30 18:20:11 +020069 self.f = open(TESTFN, 'wb')
Christian Heimesdd15f6c2008-03-16 00:07:10 +000070 rv = fcntl.fcntl(self.f, fcntl.F_SETFL, os.O_NONBLOCK)
Jesus Ceaf1af7052012-10-05 02:48:46 +020071 rv = fcntl.fcntl(self.f, fcntl.F_SETLKW, lockdata)
Christian Heimesdd15f6c2008-03-16 00:07:10 +000072 self.f.close()
Fred Drakebc7809b2001-05-09 21:11:59 +000073
Serhiy Storchaka78980432013-01-15 01:12:17 +020074 def test_fcntl_bad_file(self):
75 class F:
76 def __init__(self, fn):
77 self.fn = fn
78 def fileno(self):
79 return self.fn
80 self.assertRaises(ValueError, fcntl.fcntl, -1, fcntl.F_SETFL, os.O_NONBLOCK)
81 self.assertRaises(ValueError, fcntl.fcntl, F(-1), fcntl.F_SETFL, os.O_NONBLOCK)
82 self.assertRaises(TypeError, fcntl.fcntl, 'spam', fcntl.F_SETFL, os.O_NONBLOCK)
83 self.assertRaises(TypeError, fcntl.fcntl, F('spam'), fcntl.F_SETFL, os.O_NONBLOCK)
84 # Issue 15989
85 self.assertRaises(OverflowError, fcntl.fcntl, _testcapi.INT_MAX + 1,
86 fcntl.F_SETFL, os.O_NONBLOCK)
87 self.assertRaises(OverflowError, fcntl.fcntl, F(_testcapi.INT_MAX + 1),
88 fcntl.F_SETFL, os.O_NONBLOCK)
89 self.assertRaises(OverflowError, fcntl.fcntl, _testcapi.INT_MIN - 1,
90 fcntl.F_SETFL, os.O_NONBLOCK)
91 self.assertRaises(OverflowError, fcntl.fcntl, F(_testcapi.INT_MIN - 1),
92 fcntl.F_SETFL, os.O_NONBLOCK)
93
Gregory P. Smithe5aefa42013-03-31 10:10:50 -070094 @unittest.skipIf(
95 platform.machine().startswith('arm') and platform.system() == 'Linux',
96 "ARM Linux returns EINVAL for F_NOTIFY DN_MULTISHOT")
Antoine Pitrou61f77b52009-05-24 15:46:13 +000097 def test_fcntl_64_bit(self):
98 # Issue #1309352: fcntl shouldn't fail when the third arg fits in a
99 # C 'long' but not in a C 'int'.
100 try:
101 cmd = fcntl.F_NOTIFY
102 # This flag is larger than 2**31 in 64-bit builds
103 flags = fcntl.DN_MULTISHOT
104 except AttributeError:
105 self.skipTest("F_NOTIFY or DN_MULTISHOT unavailable")
106 fd = os.open(os.path.dirname(os.path.abspath(TESTFN)), os.O_RDONLY)
107 try:
108 fcntl.fcntl(fd, cmd, flags)
109 finally:
110 os.close(fd)
111
Fred Drakebc7809b2001-05-09 21:11:59 +0000112
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000113def test_main():
114 run_unittest(TestFcntl)
Fred Drakebc7809b2001-05-09 21:11:59 +0000115
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000116if __name__ == '__main__':
117 test_main()