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 | """ |
Roger E. Masse | fb01d4b | 1996-12-17 17:41:09 +0000 | [diff] [blame] | 6 | import fcntl |
Christian Heimes | e25f35e | 2008-03-20 10:49:03 +0000 | [diff] [blame] | 7 | import os |
| 8 | import struct |
| 9 | import sys |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 11 | from test.support import verbose, TESTFN, unlink, run_unittest |
Roger E. Masse | fb01d4b | 1996-12-17 17:41:09 +0000 | [diff] [blame] | 12 | |
Christian Heimes | e25f35e | 2008-03-20 10:49:03 +0000 | [diff] [blame] | 13 | # TODO - Write tests for flock() and lockf(). |
Martin v. Löwis | a660a34 | 2001-10-18 22:07:48 +0000 | [diff] [blame] | 14 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 15 | def get_lockdata(): |
| 16 | if sys.platform.startswith('atheos'): |
| 17 | start_len = "qq" |
Hye-Shik Chang | ac89f6e | 2005-04-04 15:21:04 +0000 | [diff] [blame] | 18 | else: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 19 | try: |
| 20 | os.O_LARGEFILE |
| 21 | except AttributeError: |
| 22 | start_len = "ll" |
| 23 | else: |
| 24 | start_len = "qq" |
Fred Drake | bc7809b | 2001-05-09 21:11:59 +0000 | [diff] [blame] | 25 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 26 | if sys.platform in ('netbsd1', 'netbsd2', 'netbsd3', |
| 27 | 'Darwin1.2', 'darwin', |
| 28 | 'freebsd2', 'freebsd3', 'freebsd4', 'freebsd5', |
| 29 | 'freebsd6', 'freebsd7', 'freebsd8', |
| 30 | 'bsdos2', 'bsdos3', 'bsdos4', |
| 31 | 'openbsd', 'openbsd2', 'openbsd3', 'openbsd4'): |
| 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 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 51 | lockdata = get_lockdata() |
Roger E. Masse | fb01d4b | 1996-12-17 17:41:09 +0000 | [diff] [blame] | 52 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 53 | class TestFcntl(unittest.TestCase): |
| 54 | |
| 55 | def setUp(self): |
| 56 | self.f = None |
| 57 | |
| 58 | def tearDown(self): |
| 59 | if not self.f.closed: |
| 60 | self.f.close() |
| 61 | unlink(TESTFN) |
| 62 | |
| 63 | def test_fcntl_fileno(self): |
| 64 | # the example from the library docs |
| 65 | self.f = open(TESTFN, 'w') |
| 66 | rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETFL, os.O_NONBLOCK) |
| 67 | if verbose: |
| 68 | print('Status from fcntl with O_NONBLOCK: ', rv) |
| 69 | if sys.platform not in ['os2emx']: |
| 70 | rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETLKW, lockdata) |
| 71 | if verbose: |
| 72 | print('String from fcntl with F_SETLKW: ', repr(rv)) |
| 73 | self.f.close() |
| 74 | |
| 75 | def test_fcntl_file_descriptor(self): |
| 76 | # again, but pass the file rather than numeric descriptor |
| 77 | self.f = open(TESTFN, 'w') |
| 78 | rv = fcntl.fcntl(self.f, fcntl.F_SETFL, os.O_NONBLOCK) |
| 79 | if sys.platform not in ['os2emx']: |
| 80 | rv = fcntl.fcntl(self.f, fcntl.F_SETLKW, lockdata) |
| 81 | self.f.close() |
Fred Drake | bc7809b | 2001-05-09 21:11:59 +0000 | [diff] [blame] | 82 | |
| 83 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 84 | def test_main(): |
| 85 | run_unittest(TestFcntl) |
Fred Drake | bc7809b | 2001-05-09 21:11:59 +0000 | [diff] [blame] | 86 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 87 | if __name__ == '__main__': |
| 88 | test_main() |