1
- from typing import Iterator , List , Tuple
1
+ from typing import Iterator , List , Tuple , IO
2
2
import time
3
3
import os
4
4
import sys
13
13
WIN32 = sys .platform .startswith ("win" )
14
14
15
15
16
- def lock_file (filename : str , duration : float ) -> Thread :
17
- """Open a file and keep it open in a background thread for duration sec."""
18
- def _lock_file () -> None :
19
- with open (filename ):
20
- time .sleep (duration )
21
- t = Thread (target = _lock_file , daemon = True )
22
- t .start ()
23
- return t
24
-
25
-
26
16
@skipUnless (WIN32 , "only relevant for Windows" )
27
17
class WindowsReplace (TestCase ):
28
18
tmpdir = tempfile .TemporaryDirectory (prefix = 'mypy-test-' ,
@@ -34,6 +24,18 @@ class WindowsReplace(TestCase):
34
24
35
25
threads = [] # type: List[Thread]
36
26
27
+ @classmethod
28
+ def close_file_later (cls , file : IO , after : float ) -> Thread :
29
+ """Open a file and keep it open in a background thread for duration sec."""
30
+ def _close_file_later () -> None :
31
+ time .sleep (after )
32
+ file .close ()
33
+
34
+ t = Thread (target = _close_file_later , daemon = True )
35
+ cls .threads .append (t )
36
+ t .start ()
37
+ return t
38
+
37
39
@classmethod
38
40
def tearDownClass (cls ) -> None :
39
41
# Need to wait for threads to complete, otherwise we'll get PermissionError
@@ -52,12 +54,14 @@ def prepare_src_dest(self, src_lock_duration: float, dest_lock_duration: float
52
54
src = os .path .join (self .tmpdir .name , random_string ())
53
55
dest = os .path .join (self .tmpdir .name , random_string ())
54
56
55
- for fname in (src , dest ):
56
- with open (fname , 'w' ) as f :
57
- f .write (fname )
57
+ for fname , duration in zip ((src , dest ), (src_lock_duration , dest_lock_duration )):
58
+ f = open (fname , 'w' )
59
+ f .write (fname )
60
+ if duration :
61
+ self .close_file_later (f , duration )
62
+ else :
63
+ f .close ()
58
64
59
- self .threads .append (lock_file (src , src_lock_duration ))
60
- self .threads .append (lock_file (dest , dest_lock_duration ))
61
65
return src , dest
62
66
63
67
def replace_ok (self , src_lock_duration : float , dest_lock_duration : float ,
0 commit comments