blob: 29af99cf1624a7d7cef94bde0cedf32040eb5d9e [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 Stinnere6747472011-08-21 00:39:18 +020026 if (sys.platform.startswith(('netbsd', 'freebsd', 'openbsd', 'bsdos'))
27 or sys.platform == 'darwin'):
Christian Heimesdd15f6c2008-03-16 00:07:10 +000028 if struct.calcsize('l') == 8:
29 off_t = 'l'
30 pid_t = 'i'
31 else:
32 off_t = 'lxxxx'
33 pid_t = 'l'
34 lockdata = struct.pack(off_t + off_t + pid_t + 'hh', 0, 0, 0,
35 fcntl.F_WRLCK, 0)
36 elif sys.platform in ['aix3', 'aix4', 'hp-uxB', 'unixware7']:
37 lockdata = struct.pack('hhlllii', fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0)
38 elif sys.platform in ['os2emx']:
39 lockdata = None
40 else:
41 lockdata = struct.pack('hh'+start_len+'hh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
42 if lockdata:
43 if verbose:
44 print('struct.pack: ', repr(lockdata))
45 return lockdata
Fred Drakebc7809b2001-05-09 21:11:59 +000046
Christian Heimesdd15f6c2008-03-16 00:07:10 +000047lockdata = get_lockdata()
Roger E. Massefb01d4b1996-12-17 17:41:09 +000048
Christian Heimesdd15f6c2008-03-16 00:07:10 +000049class TestFcntl(unittest.TestCase):
50
51 def setUp(self):
52 self.f = None
53
54 def tearDown(self):
Antoine Pitrou61f77b52009-05-24 15:46:13 +000055 if self.f and not self.f.closed:
Christian Heimesdd15f6c2008-03-16 00:07:10 +000056 self.f.close()
57 unlink(TESTFN)
58
59 def test_fcntl_fileno(self):
60 # the example from the library docs
Victor Stinnera6d2c762011-06-30 18:20:11 +020061 self.f = open(TESTFN, 'wb')
Christian Heimesdd15f6c2008-03-16 00:07:10 +000062 rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
63 if verbose:
64 print('Status from fcntl with O_NONBLOCK: ', rv)
65 if sys.platform not in ['os2emx']:
66 rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETLKW, lockdata)
67 if verbose:
68 print('String from fcntl with F_SETLKW: ', repr(rv))
69 self.f.close()
70
71 def test_fcntl_file_descriptor(self):
72 # again, but pass the file rather than numeric descriptor
Victor Stinnera6d2c762011-06-30 18:20:11 +020073 self.f = open(TESTFN, 'wb')
Christian Heimesdd15f6c2008-03-16 00:07:10 +000074 rv = fcntl.fcntl(self.f, fcntl.F_SETFL, os.O_NONBLOCK)
75 if sys.platform not in ['os2emx']:
76 rv = fcntl.fcntl(self.f, fcntl.F_SETLKW, lockdata)
77 self.f.close()
Fred Drakebc7809b2001-05-09 21:11:59 +000078
Antoine Pitrou61f77b52009-05-24 15:46:13 +000079 def test_fcntl_64_bit(self):
80 # Issue #1309352: fcntl shouldn't fail when the third arg fits in a
81 # C 'long' but not in a C 'int'.
82 try:
83 cmd = fcntl.F_NOTIFY
84 # This flag is larger than 2**31 in 64-bit builds
85 flags = fcntl.DN_MULTISHOT
86 except AttributeError:
87 self.skipTest("F_NOTIFY or DN_MULTISHOT unavailable")
88 fd = os.open(os.path.dirname(os.path.abspath(TESTFN)), os.O_RDONLY)
89 try:
90 fcntl.fcntl(fd, cmd, flags)
91 finally:
92 os.close(fd)
93
Fred Drakebc7809b2001-05-09 21:11:59 +000094
Christian Heimesdd15f6c2008-03-16 00:07:10 +000095def test_main():
96 run_unittest(TestFcntl)
Fred Drakebc7809b2001-05-09 21:11:59 +000097
Christian Heimesdd15f6c2008-03-16 00:07:10 +000098if __name__ == '__main__':
99 test_main()