Roger E. Masse | fb01d4b | 1996-12-17 17:41:09 +0000 | [diff] [blame] | 1 | """Test program for the fcntl C module. |
Brett Cannon | 1f5182b | 2008-03-13 21:09:28 +0000 | [diff] [blame] | 2 | |
| 3 | OS/2+EMX doesn't support the file locking operations. |
| 4 | |
Roger E. Masse | fb01d4b | 1996-12-17 17:41:09 +0000 | [diff] [blame] | 5 | """ |
Gregory P. Smith | a5cfcad | 2008-03-19 23:03:25 +0000 | [diff] [blame] | 6 | import os |
| 7 | import struct |
| 8 | import sys |
Brett Cannon | 1f5182b | 2008-03-13 21:09:28 +0000 | [diff] [blame] | 9 | import unittest |
R. David Murray | 597ebab | 2009-03-31 18:32:17 +0000 | [diff] [blame] | 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 | |
Roger E. Masse | fb01d4b | 1996-12-17 17:41:09 +0000 | [diff] [blame] | 16 | |
Gregory P. Smith | 6af3db8 | 2008-03-20 05:41:53 +0000 | [diff] [blame] | 17 | # TODO - Write tests for flock() and lockf(). |
Martin v. Löwis | a660a34 | 2001-10-18 22:07:48 +0000 | [diff] [blame] | 18 | |
Brett Cannon | 1f5182b | 2008-03-13 21:09:28 +0000 | [diff] [blame] | 19 | def get_lockdata(): |
| 20 | if sys.platform.startswith('atheos'): |
| 21 | start_len = "qq" |
Hye-Shik Chang | ac89f6e | 2005-04-04 15:21:04 +0000 | [diff] [blame] | 22 | else: |
Brett Cannon | 1f5182b | 2008-03-13 21:09:28 +0000 | [diff] [blame] | 23 | try: |
| 24 | os.O_LARGEFILE |
| 25 | except AttributeError: |
| 26 | start_len = "ll" |
| 27 | else: |
| 28 | start_len = "qq" |
Fred Drake | bc7809b | 2001-05-09 21:11:59 +0000 | [diff] [blame] | 29 | |
Charles-François Natali | cdaafe0 | 2011-08-23 19:42:02 +0200 | [diff] [blame] | 30 | if (sys.platform.startswith(('netbsd', 'freebsd', 'openbsd', 'bsdos')) |
| 31 | or sys.platform == 'darwin'): |
Brett Cannon | 1f5182b | 2008-03-13 21:09:28 +0000 | [diff] [blame] | 32 | if struct.calcsize('l') == 8: |
| 33 | off_t = 'l' |
| 34 | pid_t = 'i' |
| 35 | else: |
| 36 | off_t = 'lxxxx' |
| 37 | pid_t = 'l' |
| 38 | lockdata = struct.pack(off_t + off_t + pid_t + 'hh', 0, 0, 0, |
| 39 | fcntl.F_WRLCK, 0) |
| 40 | elif sys.platform in ['aix3', 'aix4', 'hp-uxB', 'unixware7']: |
| 41 | lockdata = struct.pack('hhlllii', fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0) |
| 42 | elif sys.platform in ['os2emx']: |
| 43 | lockdata = None |
| 44 | else: |
| 45 | lockdata = struct.pack('hh'+start_len+'hh', fcntl.F_WRLCK, 0, 0, 0, 0, 0) |
| 46 | if lockdata: |
| 47 | if verbose: |
| 48 | print 'struct.pack: ', repr(lockdata) |
| 49 | return lockdata |
Fred Drake | bc7809b | 2001-05-09 21:11:59 +0000 | [diff] [blame] | 50 | |
Brett Cannon | 1f5182b | 2008-03-13 21:09:28 +0000 | [diff] [blame] | 51 | lockdata = get_lockdata() |
Fred Drake | bc7809b | 2001-05-09 21:11:59 +0000 | [diff] [blame] | 52 | |
| 53 | |
Brett Cannon | 1f5182b | 2008-03-13 21:09:28 +0000 | [diff] [blame] | 54 | class TestFcntl(unittest.TestCase): |
Fred Drake | bc7809b | 2001-05-09 21:11:59 +0000 | [diff] [blame] | 55 | |
Brett Cannon | 1f5182b | 2008-03-13 21:09:28 +0000 | [diff] [blame] | 56 | def setUp(self): |
| 57 | self.f = None |
Fred Drake | bc7809b | 2001-05-09 21:11:59 +0000 | [diff] [blame] | 58 | |
Brett Cannon | 1f5182b | 2008-03-13 21:09:28 +0000 | [diff] [blame] | 59 | def tearDown(self): |
Antoine Pitrou | d49e375 | 2009-05-24 15:40:09 +0000 | [diff] [blame] | 60 | if self.f and not self.f.closed: |
Brett Cannon | 1f5182b | 2008-03-13 21:09:28 +0000 | [diff] [blame] | 61 | self.f.close() |
| 62 | unlink(TESTFN) |
| 63 | |
| 64 | def test_fcntl_fileno(self): |
| 65 | # the example from the library docs |
| 66 | self.f = open(TESTFN, 'w') |
| 67 | rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETFL, os.O_NONBLOCK) |
| 68 | if verbose: |
| 69 | print 'Status from fcntl with O_NONBLOCK: ', rv |
| 70 | if sys.platform not in ['os2emx']: |
| 71 | rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETLKW, lockdata) |
| 72 | if verbose: |
| 73 | print 'String from fcntl with F_SETLKW: ', repr(rv) |
| 74 | self.f.close() |
| 75 | |
| 76 | def test_fcntl_file_descriptor(self): |
| 77 | # again, but pass the file rather than numeric descriptor |
| 78 | self.f = open(TESTFN, 'w') |
| 79 | rv = fcntl.fcntl(self.f, fcntl.F_SETFL, os.O_NONBLOCK) |
| 80 | if sys.platform not in ['os2emx']: |
| 81 | rv = fcntl.fcntl(self.f, fcntl.F_SETLKW, lockdata) |
| 82 | self.f.close() |
| 83 | |
Antoine Pitrou | d49e375 | 2009-05-24 15:40:09 +0000 | [diff] [blame] | 84 | def test_fcntl_64_bit(self): |
| 85 | # Issue #1309352: fcntl shouldn't fail when the third arg fits in a |
| 86 | # C 'long' but not in a C 'int'. |
| 87 | try: |
| 88 | cmd = fcntl.F_NOTIFY |
| 89 | # This flag is larger than 2**31 in 64-bit builds |
| 90 | flags = fcntl.DN_MULTISHOT |
| 91 | except AttributeError: |
| 92 | self.skipTest("F_NOTIFY or DN_MULTISHOT unavailable") |
| 93 | fd = os.open(os.path.dirname(os.path.abspath(TESTFN)), os.O_RDONLY) |
| 94 | try: |
| 95 | fcntl.fcntl(fd, cmd, flags) |
| 96 | finally: |
| 97 | os.close(fd) |
| 98 | |
Brett Cannon | 1f5182b | 2008-03-13 21:09:28 +0000 | [diff] [blame] | 99 | |
| 100 | def test_main(): |
| 101 | run_unittest(TestFcntl) |
| 102 | |
| 103 | if __name__ == '__main__': |
| 104 | test_main() |