Skip to content

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

Closed
nanjekyejoannah wants to merge 4 commits into
python:masterfrom
nanjekyejoannah:test_lockf
Closed

bpo-36746: Create test for fcntl.lockf()#12999
nanjekyejoannah wants to merge 4 commits into
python:masterfrom
nanjekyejoannah:test_lockf

Conversation

@nanjekyejoannah

@nanjekyejoannah nanjekyejoannah commented Apr 28, 2019

Copy link
Copy Markdown
Contributor

I have created a test for fcntl.lockf().

https://bugs.python.org/issue36746

@bedevere-bot bedevere-bot added tests Tests in the Lib/test dir awaiting review labels Apr 28, 2019
@nanjekyejoannah nanjekyejoannah changed the title bpo-36746: Create test for fcntl.lockf bpo-36746: Create test for fcntl.lockf() Apr 28, 2019
Comment thread Misc/NEWS.d/next/Tests/2019-04-28-18-36-54.bpo-36746.aqYNtS.rst Outdated
@nanjekyejoannah

Copy link
Copy Markdown
Contributor Author

I have made the requested changes; please review again.

@bedevere-bot

Copy link
Copy Markdown

Thanks for making the requested changes!

: please review the changes made to this pull request.

Comment thread Lib/test/test_fcntl.py
if pid == 0:
rval = 2
try:
fcntl.lockf(open(self.f.name, self.f.mode), fcntl.LOCK_EX | fcntl.LOCK_NB)

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

Comment thread Lib/test/test_fcntl.py Outdated

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

self.f.close()

Copy link
Copy Markdown
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?

Comment thread Lib/test/test_fcntl.py Outdated
finally:
os._exit(rval)

assert pid > 0

Copy link
Copy Markdown
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.

@berkerpeksag berkerpeksag left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think your approach looks pretty good.

Have you considered using subprocess.run() instead of os.fork()? I think the former might be a better alternative especially if you are planning to increase test coverage of fcntl since it would make code reusing easier.

Also, while you are editing this file, could you please replace the following snippet

def test_main():
    run_unittest(TestFcntl)

if __name__ == '__main__':
    test_main()

with

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

Comment thread Lib/test/test_fcntl.py Outdated
Comment thread Lib/test/test_fcntl.py Outdated
Comment thread Lib/test/test_fcntl.py Outdated
Comment thread Lib/test/test_fcntl.py Outdated
Comment thread Lib/test/test_fcntl.py Outdated
Comment thread Lib/test/test_fcntl.py Outdated
Comment thread Lib/test/test_fcntl.py Outdated
@bedevere-bot

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@nanjekyejoannah

Copy link
Copy Markdown
Contributor Author

@berkerpeksag I have made some changes to use subprocess.run(). I have not used subprocess.run() before so your thoughts on this change are highly appreciated.

@nanjekyejoannah

Copy link
Copy Markdown
Contributor Author

I have made the requested changes; please review again

@bedevere-bot

Copy link
Copy Markdown

Thanks for making the requested changes!

@berkerpeksag: please review the changes made to this pull request.

Comment thread Lib/test/test_fcntl.py

fcntl.lockf(self.f, fcntl.LOCK_EX | fcntl.LOCK_NB)

code = textwrap.dedent('''

Copy link
Copy Markdown
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+'.

Comment thread Lib/test/test_fcntl.py
else:
os.EX_OK
finally:
os._exit()

Copy link
Copy Markdown
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.

Comment thread Lib/test/test_fcntl.py
except OSError as e:
if e.errno not in (errno.EACCES, errno.EAGAIN):
raise
os.EX_OSERR

Copy link
Copy Markdown
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?

Comment thread Lib/test/test_fcntl.py
fcntl.lockf(self.f, fcntl.LOCK_UN)

def test_lockf_errors(self):

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

@bedevere-bot

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@vstinner

Copy link
Copy Markdown
Member

PR #17010 added tests for fcntl.lockf(). I close this PR. Thanks @nanjekyejoannah anyway!

@vstinner vstinner closed this Jan 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting changes skip news tests Tests in the Lib/test dir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants