-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
Conversation
Misc/NEWS.d/next/Tests/2019-04-28-18-36-54.bpo-36746.aqYNtS.rst
Outdated
Show resolved
Hide resolved
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.
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
Lib/test/test_fcntl.py
Outdated
|
||
fcntl.lockf(self.f, fcntl.LOCK_UN) | ||
|
||
self.f.close() |
There was a problem hiding this comment.
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?
Lib/test/test_fcntl.py
Outdated
finally: | ||
os._exit(rval) | ||
|
||
assert pid > 0 |
There was a problem hiding this comment.
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.
There was a problem hiding this 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()
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.
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() |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove this empty line
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