@@ -51,6 +51,21 @@ def __init__(self, fn):
51
51
def fileno (self ):
52
52
return self .fn
53
53
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
+
54
69
class TestFcntl (unittest .TestCase ):
55
70
56
71
def setUp (self ):
@@ -141,11 +156,8 @@ def test_flock(self):
141
156
def test_lockf_exclusive (self ):
142
157
self .f = open (TESTFN , 'wb+' )
143
158
cmd = fcntl .LOCK_EX | fcntl .LOCK_NB
144
- def try_lockf_on_other_process ():
145
- self .assertRaises (BlockingIOError , fcntl .lockf , self .f , cmd )
146
-
147
159
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 ) )
149
161
p .start ()
150
162
p .join ()
151
163
fcntl .lockf (self .f , fcntl .LOCK_UN )
@@ -154,12 +166,8 @@ def try_lockf_on_other_process():
154
166
def test_lockf_share (self ):
155
167
self .f = open (TESTFN , 'wb+' )
156
168
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
-
161
169
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 ) )
163
171
p .start ()
164
172
p .join ()
165
173
fcntl .lockf (self .f , fcntl .LOCK_UN )
0 commit comments