Commit c58f339d authored by Stefan Behnel's avatar Stefan Behnel

Simplify the output stream capturing for the C compiler runs by using a temp...

Simplify the output stream capturing for the C compiler runs by using a temp file instead of threads.
parent acc11660
...@@ -21,6 +21,7 @@ import re ...@@ -21,6 +21,7 @@ import re
import io import io
import codecs import codecs
import shutil import shutil
import tempfile
from contextlib import contextmanager from contextlib import contextmanager
modification_time = os.path.getmtime modification_time = os.path.getmtime
...@@ -334,41 +335,25 @@ def get_cython_cache_dir(): ...@@ -334,41 +335,25 @@ def get_cython_cache_dir():
@contextmanager @contextmanager
def captured_fd(stream=2, encoding=None): def captured_fd(stream=2, encoding=None):
pipe_in = t = None
orig_stream = os.dup(stream) # keep copy of original stream orig_stream = os.dup(stream) # keep copy of original stream
try: try:
pipe_in, pipe_out = os.pipe() with tempfile.TemporaryFile(mode="a+b") as temp_file:
os.dup2(pipe_out, stream) # replace stream by copy of pipe def read_output(_output=[b'']):
try: if not temp_file.closed:
os.close(pipe_out) # close original pipe-out stream temp_file.seek(0)
data = [] _output[0] = temp_file.read()
return _output[0]
def copy():
try: os.dup2(temp_file.fileno(), stream) # replace stream by copy of pipe
while True: try:
d = os.read(pipe_in, 1000) def get_output():
if d: result = read_output()
data.append(d) return result.decode(encoding) if encoding else result
else:
break yield get_output
finally: finally:
os.close(pipe_in) os.dup2(orig_stream, stream) # restore original stream
read_output() # keep the output in case it's used after closing the context manager
def get_output():
output = b''.join(data)
if encoding:
output = output.decode(encoding)
return output
from threading import Thread
t = Thread(target=copy)
t.daemon = True # just in case
t.start()
yield get_output
finally:
os.dup2(orig_stream, stream) # restore original stream
if t is not None:
t.join()
finally: finally:
os.close(orig_stream) os.close(orig_stream)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment