Skip to content

Commit 068dbbd

Browse files
committed
bpo-22367: Update test_fcntl.py
1 parent 4d231bc commit 068dbbd

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

Lib/test/test_fcntl.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,21 @@ def __init__(self, fn):
5151
def fileno(self):
5252
return self.fn
5353

54+
def try_lockf_on_other_process_fail(fname, cmd):
55+
f = open(fname, 'wb+')
56+
try:
57+
fcntl.lockf(f, cmd)
58+
except BlockingIOError:
59+
pass
60+
finally:
61+
f.close()
62+
63+
def try_lockf_on_other_process(fname, cmd):
64+
f = open(fname, 'wb+')
65+
fcntl.lockf(f, cmd)
66+
fcntl.lockf(f, fcntl.LOCK_UN)
67+
f.close()
68+
5469
class TestFcntl(unittest.TestCase):
5570

5671
def setUp(self):
@@ -141,11 +156,8 @@ def test_flock(self):
141156
def test_lockf_exclusive(self):
142157
self.f = open(TESTFN, 'wb+')
143158
cmd = fcntl.LOCK_EX | fcntl.LOCK_NB
144-
def try_lockf_on_other_process():
145-
self.assertRaises(BlockingIOError, fcntl.lockf, self.f, cmd)
146-
147159
fcntl.lockf(self.f, cmd)
148-
p = Process(target=try_lockf_on_other_process)
160+
p = Process(target=try_lockf_on_other_process_fail, args=(TESTFN, cmd))
149161
p.start()
150162
p.join()
151163
fcntl.lockf(self.f, fcntl.LOCK_UN)
@@ -154,12 +166,8 @@ def try_lockf_on_other_process():
154166
def test_lockf_share(self):
155167
self.f = open(TESTFN, 'wb+')
156168
cmd = fcntl.LOCK_SH | fcntl.LOCK_NB
157-
def try_lockf_on_other_process():
158-
fcntl.lockf(self.f, cmd)
159-
fcntl.lockf(self.f, fcntl.LOCK_UN)
160-
161169
fcntl.lockf(self.f, cmd)
162-
p = Process(target=try_lockf_on_other_process)
170+
p = Process(target=try_lockf_on_other_process, args=(TESTFN, cmd))
163171
p.start()
164172
p.join()
165173
fcntl.lockf(self.f, fcntl.LOCK_UN)

0 commit comments

Comments
 (0)