Commit 81237f4 1 parent 37c9a05 commit 81237f4 Copy full SHA for 81237f4
File tree 1 file changed +23
-8
lines changed
1 file changed +23
-8
lines changed Original file line number Diff line number Diff line change 22
22
import sys
23
23
import subprocess
24
24
import time
25
+ import io
26
+ import six
25
27
from contextlib import contextmanager
26
28
27
29
join = os .path .join
@@ -190,16 +192,29 @@ def stdout_redirected(new_stdout='install.log'):
190
192
finally :
191
193
return
192
194
193
- old = os .dup (1 )
194
- os .close (1 )
195
- os .open (new_stdout , os .O_WRONLY | os .O_CREAT )
195
+ install_log_file_fid = os .open (new_stdout , os .O_WRONLY | os .O_CREAT )
196
+ original_stdout_fid = sys .stdout .fileno ()
197
+ copied_stdout_fid = os .dup (original_stdout_fid )
198
+
199
+ def _redirect (to_fid ):
200
+ # Flush and close the Python-level file object, not the C file descriptor
201
+ sys .stdout .close ()
202
+ # Point original_stdout_fid to to_fid
203
+ os .dup2 (to_fid ,original_stdout_fid )
204
+ # Create sys.stdout
205
+ if six .PY3 :
206
+ sys .stdout = io .TextIOWrapper (os .fdopen (original_stdout_fid ,'wb' ))
207
+ else :
208
+ sys .stdout = os .fdopen (original_stdout_fid ,'wb' )
196
209
197
210
try :
198
- yield None
199
- finally :
200
- os .close (1 )
201
- os .dup2 (old ,1 )
202
- os .close (old )
211
+ _redirect (install_log_file_fid )
212
+ yield
213
+ _redirect (copied_stdout_fid )
214
+ finally :
215
+ os .fsync (install_log_file_fid )
216
+ os .close (install_log_file_fid )
217
+ os .close (copied_stdout_fid )
203
218
204
219
205
220
def setup_package (setup_dict , subpackages ):
You can’t perform that action at this time.
0 commit comments