From c3b786f77192663c91c75dfad117f8c444862734 Mon Sep 17 00:00:00 2001 From: nanjekyejoannah Date: Sun, 28 Apr 2019 14:28:15 -0400 Subject: [PATCH 1/4] create test for fcntl.lockf --- Lib/test/test_fcntl.py | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_fcntl.py b/Lib/test/test_fcntl.py index 5d4abe388f78287..9ad55a098568219 100644 --- a/Lib/test/test_fcntl.py +++ b/Lib/test/test_fcntl.py @@ -12,8 +12,6 @@ fcntl = import_module('fcntl') -# TODO - Write tests for flock() and lockf(). - def get_lockdata(): try: os.O_LARGEFILE @@ -138,6 +136,39 @@ 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+') + + 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) + + fcntl.lockf(self.f, fcntl.LOCK_EX | fcntl.LOCK_NB) + + pid = os.fork() + if pid == 0: + rval = 2 + try: + fcntl.lockf(open(self.f.name, self.f.mode), fcntl.LOCK_EX | fcntl.LOCK_NB) + except IOError as e: + if e.errno not in (errno.EACCES, errno.EAGAIN): + raise + rval = 0 + else: + rval = 1 + finally: + os._exit(rval) + + assert pid > 0 + (pid, status) = os.waitpid(pid, 0) + self.assertEqual(os.WIFEXITED(status), True) + + fcntl.lockf(self.f, fcntl.LOCK_UN) + + self.f.close() + @cpython_only def test_flock_overflow(self): import _testcapi From b9b199b1479ecd0e25e83fc2864032b27386a40e Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" Date: Sun, 28 Apr 2019 18:36:56 +0000 Subject: [PATCH 2/4] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Misc/NEWS.d/next/Tests/2019-04-28-18-36-54.bpo-36746.aqYNtS.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Tests/2019-04-28-18-36-54.bpo-36746.aqYNtS.rst diff --git a/Misc/NEWS.d/next/Tests/2019-04-28-18-36-54.bpo-36746.aqYNtS.rst b/Misc/NEWS.d/next/Tests/2019-04-28-18-36-54.bpo-36746.aqYNtS.rst new file mode 100644 index 000000000000000..f0d39769456e3dd --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2019-04-28-18-36-54.bpo-36746.aqYNtS.rst @@ -0,0 +1 @@ +A test for `fcntl.lockf()` is now implemented. \ No newline at end of file From 2280d1cf520f3a29483d86a8dd7e9833d3b8cb05 Mon Sep 17 00:00:00 2001 From: Joannah Nanjekye <33177550+nanjekyejoannah@users.noreply.github.com> Date: Sun, 28 Apr 2019 15:26:41 -0400 Subject: [PATCH 3/4] remove news entry for trivial fix. --- Misc/NEWS.d/next/Tests/2019-04-28-18-36-54.bpo-36746.aqYNtS.rst | 1 - 1 file changed, 1 deletion(-) delete mode 100644 Misc/NEWS.d/next/Tests/2019-04-28-18-36-54.bpo-36746.aqYNtS.rst diff --git a/Misc/NEWS.d/next/Tests/2019-04-28-18-36-54.bpo-36746.aqYNtS.rst b/Misc/NEWS.d/next/Tests/2019-04-28-18-36-54.bpo-36746.aqYNtS.rst deleted file mode 100644 index f0d39769456e3dd..000000000000000 --- a/Misc/NEWS.d/next/Tests/2019-04-28-18-36-54.bpo-36746.aqYNtS.rst +++ /dev/null @@ -1 +0,0 @@ -A test for `fcntl.lockf()` is now implemented. \ No newline at end of file From bb26eb810e3d04d5a873704849deaafafe5b41a6 Mon Sep 17 00:00:00 2001 From: Joannah nanjekye Date: Thu, 6 Jun 2019 15:07:03 -0300 Subject: [PATCH 4/4] Fix space --- Lib/test/test_fcntl.py | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) mode change 100644 => 100755 Lib/test/test_fcntl.py diff --git a/Lib/test/test_fcntl.py b/Lib/test/test_fcntl.py old mode 100644 new mode 100755 index 9ad55a098568219..d1fc737058970c5 --- 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) @@ -140,34 +142,36 @@ def test_lockf(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) - fcntl.lockf(self.f, fcntl.LOCK_EX | fcntl.LOCK_NB) - pid = os.fork() - if pid == 0: - rval = 2 + code = textwrap.dedent(''' try: fcntl.lockf(open(self.f.name, self.f.mode), fcntl.LOCK_EX | fcntl.LOCK_NB) - except IOError as e: + except OSError as e: if e.errno not in (errno.EACCES, errno.EAGAIN): raise - rval = 0 + os.EX_OSERR else: - rval = 1 + os.EX_OK finally: - os._exit(rval) + os._exit() + ''') - assert pid > 0 - (pid, status) = os.waitpid(pid, 0) - self.assertEqual(os.WIFEXITED(status), True) + args = (sys.executable, '-c', code) + proc = subprocess.run(args, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT) fcntl.lockf(self.f, fcntl.LOCK_UN) - self.f.close() + 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): @@ -176,8 +180,5 @@ def test_flock_overflow(self): fcntl.LOCK_SH) -def test_main(): - run_unittest(TestFcntl) - if __name__ == '__main__': - test_main() + unittest.main()