diff --git a/Lib/test/test_fcntl.py b/Lib/test/test_fcntl.py old mode 100644 new mode 100755 index 5d4abe388f7828..d1fc737058970c --- a/Lib/test/test_fcntl.py +++ b/Lib/test/test_fcntl.py @@ -5,6 +5,8 @@ import struct import sys import unittest +import textwrap +import subprocess from test.support import (verbose, TESTFN, unlink, run_unittest, import_module, cpython_only) @@ -12,8 +14,6 @@ fcntl = import_module('fcntl') -# TODO - Write tests for flock() and lockf(). - def get_lockdata(): try: os.O_LARGEFILE @@ -138,6 +138,41 @@ def test_flock(self): self.assertRaises(ValueError, fcntl.flock, -1, fcntl.LOCK_SH) self.assertRaises(TypeError, fcntl.flock, 'spam', fcntl.LOCK_SH) + def test_lockf(self): + + self.f = open(TESTFN, 'wb+') + + fcntl.lockf(self.f, fcntl.LOCK_EX | fcntl.LOCK_NB) + + code = textwrap.dedent(''' + try: + fcntl.lockf(open(self.f.name, self.f.mode), fcntl.LOCK_EX | fcntl.LOCK_NB) + except OSError as e: + if e.errno not in (errno.EACCES, errno.EAGAIN): + raise + os.EX_OSERR + else: + os.EX_OK + finally: + os._exit() + ''') + + args = (sys.executable, '-c', code) + proc = subprocess.run(args, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT) + + fcntl.lockf(self.f, fcntl.LOCK_UN) + + def test_lockf_errors(self): + + self.f = open(TESTFN, 'wb+') + + self.assertRaises(TypeError, fcntl.lockf, self.f, "foo") + self.assertRaises(TypeError, fcntl.lockf, self.f, fcntl.LOCK_UN, "foo") + self.assertRaises(ValueError, fcntl.lockf, self.f, -256) + self.assertRaises(ValueError, fcntl.lockf, self.f, 256) + @cpython_only def test_flock_overflow(self): import _testcapi @@ -145,8 +180,5 @@ def test_flock_overflow(self): fcntl.LOCK_SH) -def test_main(): - run_unittest(TestFcntl) - if __name__ == '__main__': - test_main() + unittest.main()