blob: 6e425aa7ef57a9a223faf655d3c4c97bc14c6db3 [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
Christian Heimesdd15f6c2008-03-16 00:07:10 +00009import unittest
R. David Murraya21e4ca2009-03-31 23:16:50 +000010from test.support import verbose, TESTFN, unlink, run_unittest, import_module
11
12# Skip test if no fnctl module.
13fcntl = import_module('fcntl')
14
Roger E. Massefb01d4b1996-12-17 17:41:09 +000015
Christian Heimese25f35e2008-03-20 10:49:03 +000016# TODO - Write tests for flock() and lockf().
Martin v. Löwisa660a342001-10-18 22:07:48 +000017
Christian Heimesdd15f6c2008-03-16 00:07:10 +000018def get_lockdata():
Antoine Pitrou6103ab12009-10-24 20:11:21 +000019 try:
20 os.O_LARGEFILE
21 except AttributeError:
22 start_len = "ll"
Hye-Shik Changac89f6e2005-04-04 15:21:04 +000023 else:
Antoine Pitrou6103ab12009-10-24 20:11:21 +000024 start_len = "qq"
Fred Drakebc7809b2001-05-09 21:11:59 +000025
Victor Stinner9c3de4a2011-08-17 20:49:41 +020026 if (any(sys.platform.startswith(prefix)
27 for prefix in ('netbsd', 'freebsd', 'openbsd', 'bsdos'))
28 or sys.platform in ('Darwin1.2', '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
Antoine Pitrou61f77b52009-05-24 15:46:13 +000080 def test_fcntl_64_bit(self):
81 # Issue #1309352: fcntl shouldn't fail when the third arg fits in a
82 # C 'long' but not in a C 'int'.
83 try:
84 cmd = fcntl.F_NOTIFY
85 # This flag is larger than 2**31 in 64-bit builds
86 flags = fcntl.DN_MULTISHOT
87 except AttributeError:
88 self.skipTest("F_NOTIFY or DN_MULTISHOT unavailable")
89 fd = os.open(os.path.dirname(os.path.abspath(TESTFN)), os.O_RDONLY)
90 try:
91 fcntl.fcntl(fd, cmd, flags)
92 finally:
93 os.close(fd)
94
Fred Drakebc7809b2001-05-09 21:11:59 +000095
Christian Heimesdd15f6c2008-03-16 00:07:10 +000096def test_main():
97 run_unittest(TestFcntl)
Fred Drakebc7809b2001-05-09 21:11:59 +000098
Christian Heimesdd15f6c2008-03-16 00:07:10 +000099if __name__ == '__main__':
100 test_main()