blob: 6d9ee0bf5aa3331f469d5293c0a199191384ed07 [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"""
Christian Heimese25f35e2008-03-20 10:49:03 +00003import os
4import struct
5import sys
Christian Heimesdd15f6c2008-03-16 00:07:10 +00006import unittest
R. David Murraya21e4ca2009-03-31 23:16:50 +00007from test.support import verbose, TESTFN, unlink, run_unittest, import_module
8
9# Skip test if no fnctl module.
10fcntl = import_module('fcntl')
11
Roger E. Massefb01d4b1996-12-17 17:41:09 +000012
Christian Heimese25f35e2008-03-20 10:49:03 +000013# TODO - Write tests for flock() and lockf().
Martin v. Löwisa660a342001-10-18 22:07:48 +000014
Christian Heimesdd15f6c2008-03-16 00:07:10 +000015def get_lockdata():
Antoine Pitrou6103ab12009-10-24 20:11:21 +000016 try:
17 os.O_LARGEFILE
18 except AttributeError:
19 start_len = "ll"
Hye-Shik Changac89f6e2005-04-04 15:21:04 +000020 else:
Antoine Pitrou6103ab12009-10-24 20:11:21 +000021 start_len = "qq"
Fred Drakebc7809b2001-05-09 21:11:59 +000022
Victor Stinnere6747472011-08-21 00:39:18 +020023 if (sys.platform.startswith(('netbsd', 'freebsd', 'openbsd', 'bsdos'))
24 or sys.platform == 'darwin'):
Christian Heimesdd15f6c2008-03-16 00:07:10 +000025 if struct.calcsize('l') == 8:
26 off_t = 'l'
27 pid_t = 'i'
28 else:
29 off_t = 'lxxxx'
30 pid_t = 'l'
31 lockdata = struct.pack(off_t + off_t + pid_t + 'hh', 0, 0, 0,
32 fcntl.F_WRLCK, 0)
33 elif sys.platform in ['aix3', 'aix4', 'hp-uxB', 'unixware7']:
34 lockdata = struct.pack('hhlllii', fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0)
Christian Heimesdd15f6c2008-03-16 00:07:10 +000035 else:
36 lockdata = struct.pack('hh'+start_len+'hh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
37 if lockdata:
38 if verbose:
39 print('struct.pack: ', repr(lockdata))
40 return lockdata
Fred Drakebc7809b2001-05-09 21:11:59 +000041
Christian Heimesdd15f6c2008-03-16 00:07:10 +000042lockdata = get_lockdata()
Roger E. Massefb01d4b1996-12-17 17:41:09 +000043
Christian Heimesdd15f6c2008-03-16 00:07:10 +000044class TestFcntl(unittest.TestCase):
45
46 def setUp(self):
47 self.f = None
48
49 def tearDown(self):
Antoine Pitrou61f77b52009-05-24 15:46:13 +000050 if self.f and not self.f.closed:
Christian Heimesdd15f6c2008-03-16 00:07:10 +000051 self.f.close()
52 unlink(TESTFN)
53
54 def test_fcntl_fileno(self):
55 # the example from the library docs
Victor Stinnera6d2c762011-06-30 18:20:11 +020056 self.f = open(TESTFN, 'wb')
Christian Heimesdd15f6c2008-03-16 00:07:10 +000057 rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
58 if verbose:
59 print('Status from fcntl with O_NONBLOCK: ', rv)
Jesus Ceaf1af7052012-10-05 02:48:46 +020060 rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETLKW, lockdata)
61 if verbose:
62 print('String from fcntl with F_SETLKW: ', repr(rv))
Christian Heimesdd15f6c2008-03-16 00:07:10 +000063 self.f.close()
64
65 def test_fcntl_file_descriptor(self):
66 # again, but pass the file rather than numeric descriptor
Victor Stinnera6d2c762011-06-30 18:20:11 +020067 self.f = open(TESTFN, 'wb')
Christian Heimesdd15f6c2008-03-16 00:07:10 +000068 rv = fcntl.fcntl(self.f, fcntl.F_SETFL, os.O_NONBLOCK)
Jesus Ceaf1af7052012-10-05 02:48:46 +020069 rv = fcntl.fcntl(self.f, fcntl.F_SETLKW, lockdata)
Christian Heimesdd15f6c2008-03-16 00:07:10 +000070 self.f.close()
Fred Drakebc7809b2001-05-09 21:11:59 +000071
Antoine Pitrou61f77b52009-05-24 15:46:13 +000072 def test_fcntl_64_bit(self):
73 # Issue #1309352: fcntl shouldn't fail when the third arg fits in a
74 # C 'long' but not in a C 'int'.
75 try:
76 cmd = fcntl.F_NOTIFY
77 # This flag is larger than 2**31 in 64-bit builds
78 flags = fcntl.DN_MULTISHOT
79 except AttributeError:
80 self.skipTest("F_NOTIFY or DN_MULTISHOT unavailable")
81 fd = os.open(os.path.dirname(os.path.abspath(TESTFN)), os.O_RDONLY)
82 try:
83 fcntl.fcntl(fd, cmd, flags)
84 finally:
85 os.close(fd)
86
Fred Drakebc7809b2001-05-09 21:11:59 +000087
Christian Heimesdd15f6c2008-03-16 00:07:10 +000088def test_main():
89 run_unittest(TestFcntl)
Fred Drakebc7809b2001-05-09 21:11:59 +000090
Christian Heimesdd15f6c2008-03-16 00:07:10 +000091if __name__ == '__main__':
92 test_main()