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
Changes from all 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
44 changes: 38 additions & 6 deletions Lib/test/test_fcntl.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
import struct
import sys
import unittest
import textwrap
import subprocess
from test.support import (verbose, TESTFN, unlink, run_unittest, import_module,
cpython_only)

# Skip test if no fcntl module.
fcntl = import_module('fcntl')


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

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

Choose a reason for hiding this comment

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

fcntl module should be imported, no? Moreover, you should pass self.f.name into the string. For example, use an f-string and replace self.f.name with {TESTFN!r}. Replace self.f.mode with 'wb+'.

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 OSError as e:
if e.errno not in (errno.EACCES, errno.EAGAIN):
raise
os.EX_OSERR
Copy link
Member

Choose a reason for hiding this comment

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

what's the purpose of this statement?

else:
os.EX_OK
finally:
os._exit()
Copy link
Member

Choose a reason for hiding this comment

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

os._exit() requires an argument.

''')

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):

Copy link
Member

Choose a reason for hiding this comment

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

remove this empty line

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
self.assertRaises(OverflowError, fcntl.flock, _testcapi.INT_MAX+1,
fcntl.LOCK_SH)


def test_main():
run_unittest(TestFcntl)

if __name__ == '__main__':
test_main()
unittest.main()