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

Conversation

nanjekyejoannah
Copy link
Contributor

@nanjekyejoannah nanjekyejoannah commented Apr 28, 2019

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
@nanjekyejoannah
Copy link
Contributor Author

I have made the requested changes; please review again.

@bedevere-bot
Copy link

Thanks for making the requested changes!

: please review the changes made to this pull request.

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


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?

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.

Copy link
Member

@berkerpeksag berkerpeksag left a comment

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

@bedevere-bot
Copy link

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
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
Contributor Author

I have made the requested changes; please review again

@bedevere-bot
Copy link

Thanks for making the requested changes!

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


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+'.

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.

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?

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

@bedevere-bot
Copy link

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