1 """Test program for the fcntl C module. 2 3 OS/2+EMX doesn't support the file locking operations. 4 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) 12 13 # Skip test if no fnctl module. 14 fcntl = import_module('fcntl') 15 16 17 # TODO - Write tests for flock() and lockf(). 18 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" 29 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 54 55 lockdata = get_lockdata() 56 57 58 class TestFcntl(unittest.TestCase): 59 60 def setUp(self): 61 self.f = None 62 63 def tearDown(self): 64 if self.f and not self.f.closed: 65 self.f.close() 66 unlink(TESTFN) 67 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() 79 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() 87 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) 102 103 104 def test_main(): 105 run_unittest(TestFcntl) 106 107 if __name__ == '__main__': 108 test_main() 109