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(): ...@@ -348,7 +348,7 @@ def get_cython_cache_dir():
@contextmanager @contextmanager
def captured_fd(stream=2): def captured_fd(stream=2, encoding=None):
pipe_in = t = 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:
...@@ -369,11 +369,17 @@ def captured_fd(stream=2): ...@@ -369,11 +369,17 @@ def captured_fd(stream=2):
finally: finally:
os.close(pipe_in) os.close(pipe_in)
def get_output():
output = b''.join(data)
if encoding:
output = output.decode(encoding)
return output
from threading import Thread from threading import Thread
t = Thread(target=copy) t = Thread(target=copy)
t.daemon = True # just in case t.daemon = True # just in case
t.start() t.start()
yield data yield get_output
finally: finally:
os.dup2(orig_stream, stream) # restore original stream os.dup2(orig_stream, stream) # restore original stream
if t is not None: if t is not None:
......
...@@ -868,20 +868,25 @@ class CythonCompileTestCase(unittest.TestCase): ...@@ -868,20 +868,25 @@ class CythonCompileTestCase(unittest.TestCase):
so_path = None so_path = None
if not self.cython_only: if not self.cython_only:
from Cython.Utils import captured_fd from Cython.Utils import captured_fd
c_compiler_stderr = None get_stderr = None
try: 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) so_path = self.run_distutils(test_directory, module, workdir, incdir)
except Exception: except Exception:
if expected_errors == '_FAIL_C_COMPILE': if expected_errors == '_FAIL_C_COMPILE' and get_stderr and get_stderr():
assert c_compiler_stderr pass
else: else:
raise raise
else: else:
c_compiler_stderr = ''.join(c_compiler_stderr).strip() c_compiler_stderr = get_stderr()
if c_compiler_stderr: if c_compiler_stderr:
print("\n=== C/C++ compiler output: ===") print("\n=== C/C++ compiler error output: ===")
print(c_compiler_stderr) 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': if expected_errors == '_FAIL_C_COMPILE':
# must raise this outside the try block # must raise this outside the try block
raise RuntimeError('should have failed C compile') 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