Commit f54826aa authored by Stefan Behnel's avatar Stefan Behnel

fix stderr test output printing in Py3

parent 047a1d83
......@@ -348,7 +348,7 @@ def get_cython_cache_dir():
@contextmanager
def captured_fd(stream=2):
def captured_fd(stream=2, encoding=None):
pipe_in = t = None
orig_stream = os.dup(stream) # keep copy of original stream
try:
......@@ -369,11 +369,17 @@ def captured_fd(stream=2):
finally:
os.close(pipe_in)
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 data
yield get_output
finally:
os.dup2(orig_stream, stream) # restore original stream
if t is not None:
......
......@@ -868,20 +868,25 @@ class CythonCompileTestCase(unittest.TestCase):
so_path = None
if not self.cython_only:
from Cython.Utils import captured_fd
c_compiler_stderr = None
get_stderr = None
try:
with captured_fd(2) as c_compiler_stderr:
with captured_fd(2) as get_stderr:
so_path = self.run_distutils(test_directory, module, workdir, incdir)
except Exception:
if expected_errors == '_FAIL_C_COMPILE':
assert c_compiler_stderr
if expected_errors == '_FAIL_C_COMPILE' and get_stderr and get_stderr():
pass
else:
raise
else:
c_compiler_stderr = ''.join(c_compiler_stderr).strip()
c_compiler_stderr = get_stderr()
if c_compiler_stderr:
print("\n=== C/C++ compiler output: ===")
print(c_compiler_stderr)
print("\n=== C/C++ compiler error output: ===")
sys.stdout.flush()
try:
out = sys.stdout.buffer # Py3
except AttributeError:
out = sys.stdout # Py2
out.write(c_compiler_stderr)
if expected_errors == '_FAIL_C_COMPILE':
# must raise this outside the try block
raise RuntimeError('should have failed C compile')
......
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