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 | """ |
Gregory P. Smith | e5aefa4 | 2013-03-31 10:10:50 -0700 | [diff] [blame] | 6 | import platform |
Christian Heimes | e25f35e | 2008-03-20 10:49:03 +0000 | [diff] [blame] | 7 | import os |
| 8 | import struct |
| 9 | import sys |
Serhiy Storchaka | 441d30f | 2013-01-19 12:26:26 +0200 | [diff] [blame] | 10 | import _testcapi |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11 | import unittest |
R. David Murray | a21e4ca | 2009-03-31 23:16:50 +0000 | [diff] [blame] | 12 | from test.support import verbose, TESTFN, unlink, run_unittest, import_module |
| 13 | |
Ezio Melotti | 78ede7c | 2013-08-23 23:06:31 +0300 | [diff] [blame^] | 14 | # Skip test if no fcntl module. |
R. David Murray | a21e4ca | 2009-03-31 23:16:50 +0000 | [diff] [blame] | 15 | fcntl = import_module('fcntl') |
| 16 | |
Roger E. Masse | fb01d4b | 1996-12-17 17:41:09 +0000 | [diff] [blame] | 17 | |
Christian Heimes | e25f35e | 2008-03-20 10:49:03 +0000 | [diff] [blame] | 18 | # TODO - Write tests for flock() and lockf(). |
Martin v. Löwis | a660a34 | 2001-10-18 22:07:48 +0000 | [diff] [blame] | 19 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 20 | def get_lockdata(): |
Antoine Pitrou | 6103ab1 | 2009-10-24 20:11:21 +0000 | [diff] [blame] | 21 | try: |
| 22 | os.O_LARGEFILE |
| 23 | except AttributeError: |
| 24 | start_len = "ll" |
Hye-Shik Chang | ac89f6e | 2005-04-04 15:21:04 +0000 | [diff] [blame] | 25 | else: |
Antoine Pitrou | 6103ab1 | 2009-10-24 20:11:21 +0000 | [diff] [blame] | 26 | start_len = "qq" |
Fred Drake | bc7809b | 2001-05-09 21:11:59 +0000 | [diff] [blame] | 27 | |
Charles-François Natali | 6cea35a | 2011-08-23 19:46:46 +0200 | [diff] [blame] | 28 | if (sys.platform.startswith(('netbsd', 'freebsd', 'openbsd', 'bsdos')) |
| 29 | or sys.platform == 'darwin'): |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 30 | if struct.calcsize('l') == 8: |
| 31 | off_t = 'l' |
| 32 | pid_t = 'i' |
| 33 | else: |
| 34 | off_t = 'lxxxx' |
| 35 | pid_t = 'l' |
| 36 | lockdata = struct.pack(off_t + off_t + pid_t + 'hh', 0, 0, 0, |
| 37 | fcntl.F_WRLCK, 0) |
doko@ubuntu.com | 1dfb918 | 2013-08-03 16:12:33 +0200 | [diff] [blame] | 38 | elif sys.platform.startswith('gnukfreebsd'): |
| 39 | lockdata = struct.pack('qqihhi', 0, 0, 0, fcntl.F_WRLCK, 0, 0) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 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): |
Antoine Pitrou | 61f77b5 | 2009-05-24 15:46:13 +0000 | [diff] [blame] | 59 | if self.f and not self.f.closed: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 60 | self.f.close() |
| 61 | unlink(TESTFN) |
| 62 | |
| 63 | def test_fcntl_fileno(self): |
| 64 | # the example from the library docs |
Victor Stinner | a6d2c76 | 2011-06-30 18:20:11 +0200 | [diff] [blame] | 65 | self.f = open(TESTFN, 'wb') |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 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 |
Victor Stinner | a6d2c76 | 2011-06-30 18:20:11 +0200 | [diff] [blame] | 77 | self.f = open(TESTFN, 'wb') |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 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 | |
Serhiy Storchaka | 441d30f | 2013-01-19 12:26:26 +0200 | [diff] [blame] | 83 | def test_fcntl_bad_file(self): |
| 84 | class F: |
| 85 | def __init__(self, fn): |
| 86 | self.fn = fn |
| 87 | def fileno(self): |
| 88 | return self.fn |
| 89 | self.assertRaises(ValueError, fcntl.fcntl, -1, fcntl.F_SETFL, os.O_NONBLOCK) |
| 90 | self.assertRaises(ValueError, fcntl.fcntl, F(-1), fcntl.F_SETFL, os.O_NONBLOCK) |
| 91 | self.assertRaises(TypeError, fcntl.fcntl, 'spam', fcntl.F_SETFL, os.O_NONBLOCK) |
| 92 | self.assertRaises(TypeError, fcntl.fcntl, F('spam'), fcntl.F_SETFL, os.O_NONBLOCK) |
| 93 | # Issue 15989 |
| 94 | self.assertRaises(OverflowError, fcntl.fcntl, _testcapi.INT_MAX + 1, |
| 95 | fcntl.F_SETFL, os.O_NONBLOCK) |
| 96 | self.assertRaises(OverflowError, fcntl.fcntl, F(_testcapi.INT_MAX + 1), |
| 97 | fcntl.F_SETFL, os.O_NONBLOCK) |
| 98 | self.assertRaises(OverflowError, fcntl.fcntl, _testcapi.INT_MIN - 1, |
| 99 | fcntl.F_SETFL, os.O_NONBLOCK) |
| 100 | self.assertRaises(OverflowError, fcntl.fcntl, F(_testcapi.INT_MIN - 1), |
| 101 | fcntl.F_SETFL, os.O_NONBLOCK) |
| 102 | |
Gregory P. Smith | e5aefa4 | 2013-03-31 10:10:50 -0700 | [diff] [blame] | 103 | @unittest.skipIf( |
| 104 | platform.machine().startswith('arm') and platform.system() == 'Linux', |
| 105 | "ARM Linux returns EINVAL for F_NOTIFY DN_MULTISHOT") |
Antoine Pitrou | 61f77b5 | 2009-05-24 15:46:13 +0000 | [diff] [blame] | 106 | def test_fcntl_64_bit(self): |
| 107 | # Issue #1309352: fcntl shouldn't fail when the third arg fits in a |
| 108 | # C 'long' but not in a C 'int'. |
| 109 | try: |
| 110 | cmd = fcntl.F_NOTIFY |
| 111 | # This flag is larger than 2**31 in 64-bit builds |
| 112 | flags = fcntl.DN_MULTISHOT |
| 113 | except AttributeError: |
| 114 | self.skipTest("F_NOTIFY or DN_MULTISHOT unavailable") |
| 115 | fd = os.open(os.path.dirname(os.path.abspath(TESTFN)), os.O_RDONLY) |
| 116 | try: |
| 117 | fcntl.fcntl(fd, cmd, flags) |
| 118 | finally: |
| 119 | os.close(fd) |
| 120 | |
Fred Drake | bc7809b | 2001-05-09 21:11:59 +0000 | [diff] [blame] | 121 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 122 | def test_main(): |
| 123 | run_unittest(TestFcntl) |
Fred Drake | bc7809b | 2001-05-09 21:11:59 +0000 | [diff] [blame] | 124 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 125 | if __name__ == '__main__': |
| 126 | test_main() |