bpo-36746: Create test for fcntl.lockf()#12999
Conversation
|
I have made the requested changes; please review again. |
|
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) |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
self.assertGreater(pid, 0) could be useful in error messages where pid value is shown for debugging assertion failure.
berkerpeksag
left a comment
There was a problem hiding this comment.
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()|
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 |
|
@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. |
|
I have made the requested changes; please review again |
|
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(''' |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
os._exit() requires an argument.
| except OSError as e: | ||
| if e.errno not in (errno.EACCES, errno.EAGAIN): | ||
| raise | ||
| os.EX_OSERR |
There was a problem hiding this comment.
what's the purpose of this statement?
| fcntl.lockf(self.f, fcntl.LOCK_UN) | ||
|
|
||
| def test_lockf_errors(self): | ||
|
|
|
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 |
|
PR #17010 added tests for fcntl.lockf(). I close this PR. Thanks @nanjekyejoannah anyway! |
I have created a test for
fcntl.lockf().https://bugs.python.org/issue36746