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