@@ -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+
5469class 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