-
-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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(''' | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. This line causes a
|
||
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 commentThe 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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() |
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+'.