move sections
[python/dscho.git] / Lib / test / test_fcntl.py
blobc8ea7b7ea51e4ca945264a973b2e586d5482ac58
1 """Test program for the fcntl C module.
3 OS/2+EMX doesn't support the file locking operations.
5 """
6 import os
7 import struct
8 import sys
9 import unittest
10 from test.test_support import (verbose, TESTFN, unlink, run_unittest,
11 import_module)
13 # Skip test if no fnctl module.
14 fcntl = import_module('fcntl')
17 # TODO - Write tests for flock() and lockf().
19 def get_lockdata():
20 if sys.platform.startswith('atheos'):
21 start_len = "qq"
22 else:
23 try:
24 os.O_LARGEFILE
25 except AttributeError:
26 start_len = "ll"
27 else:
28 start_len = "qq"
30 if sys.platform in ('netbsd1', 'netbsd2', 'netbsd3',
31 'Darwin1.2', 'darwin',
32 'freebsd2', 'freebsd3', 'freebsd4', 'freebsd5',
33 'freebsd6', 'freebsd7', 'freebsd8',
34 'bsdos2', 'bsdos3', 'bsdos4',
35 'openbsd', 'openbsd2', 'openbsd3', 'openbsd4'):
36 if struct.calcsize('l') == 8:
37 off_t = 'l'
38 pid_t = 'i'
39 else:
40 off_t = 'lxxxx'
41 pid_t = 'l'
42 lockdata = struct.pack(off_t + off_t + pid_t + 'hh', 0, 0, 0,
43 fcntl.F_WRLCK, 0)
44 elif sys.platform in ['aix3', 'aix4', 'hp-uxB', 'unixware7']:
45 lockdata = struct.pack('hhlllii', fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0)
46 elif sys.platform in ['os2emx']:
47 lockdata = None
48 else:
49 lockdata = struct.pack('hh'+start_len+'hh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
50 if lockdata:
51 if verbose:
52 print 'struct.pack: ', repr(lockdata)
53 return lockdata
55 lockdata = get_lockdata()
58 class TestFcntl(unittest.TestCase):
60 def setUp(self):
61 self.f = None
63 def tearDown(self):
64 if self.f and not self.f.closed:
65 self.f.close()
66 unlink(TESTFN)
68 def test_fcntl_fileno(self):
69 # the example from the library docs
70 self.f = open(TESTFN, 'w')
71 rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
72 if verbose:
73 print 'Status from fcntl with O_NONBLOCK: ', rv
74 if sys.platform not in ['os2emx']:
75 rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETLKW, lockdata)
76 if verbose:
77 print 'String from fcntl with F_SETLKW: ', repr(rv)
78 self.f.close()
80 def test_fcntl_file_descriptor(self):
81 # again, but pass the file rather than numeric descriptor
82 self.f = open(TESTFN, 'w')
83 rv = fcntl.fcntl(self.f, fcntl.F_SETFL, os.O_NONBLOCK)
84 if sys.platform not in ['os2emx']:
85 rv = fcntl.fcntl(self.f, fcntl.F_SETLKW, lockdata)
86 self.f.close()
88 def test_fcntl_64_bit(self):
89 # Issue #1309352: fcntl shouldn't fail when the third arg fits in a
90 # C 'long' but not in a C 'int'.
91 try:
92 cmd = fcntl.F_NOTIFY
93 # This flag is larger than 2**31 in 64-bit builds
94 flags = fcntl.DN_MULTISHOT
95 except AttributeError:
96 self.skipTest("F_NOTIFY or DN_MULTISHOT unavailable")
97 fd = os.open(os.path.dirname(os.path.abspath(TESTFN)), os.O_RDONLY)
98 try:
99 fcntl.fcntl(fd, cmd, flags)
100 finally:
101 os.close(fd)
104 def test_main():
105 run_unittest(TestFcntl)
107 if __name__ == '__main__':
108 test_main()