Skip to content

bpo-36746: Create test for fcntl.lockf() #12999

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions Lib/test/test_fcntl.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
fcntl = import_module('fcntl')


# TODO - Write tests for flock() and lockf().

def get_lockdata():
try:
os.O_LARGEFILE
Expand Down Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line causes a ResourceWarning since the open file handle is not closed. Perhaps assign it to a variable and make sure it's closed in the test.

➜  cpython git:(master) ✗ ./python.exe -m unittest -v test.test_fcntl.TestFcntl.test_lockf
struct.pack:  b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00'
test_lockf (test.test_fcntl.TestFcntl) ... /Users/karthikeyansingaravelan/stuff/python/cpython/Lib/test/test_fcntl.py:154: ResourceWarning: unclosed file <_io.BufferedRandom name='@test_27262_tmp'>
  fcntl.lockf(open(self.f.name, self.f.mode), fcntl.LOCK_EX | fcntl.LOCK_NB)
ResourceWarning: Enable tracemalloc to get the object allocation traceback
ok

----------------------------------------------------------------------
Ran 1 test in 0.008s

OK

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.assertGreater(pid, 0) could be useful in error messages where pid value is shown for debugging assertion failure.

(pid, status) = os.waitpid(pid, 0)
self.assertEqual(os.WIFEXITED(status), True)

fcntl.lockf(self.f, fcntl.LOCK_UN)

self.f.close()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel this is redundant since tearDown takes care of closing this. I am not sure some of the other tests close it too. Is it intentional?


@cpython_only
def test_flock_overflow(self):
import _testcapi
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A test for `fcntl.lockf()` is now implemented.