summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Behnel <stefan_ml@behnel.de>2021-07-31 20:43:48 +0200
committerStefan Behnel <stefan_ml@behnel.de>2021-07-31 20:44:03 +0200
commitc7dc24bc0a39af708976fb0a4bf63d2ee407badb (patch)
tree3152ecf11549e40b620f15b9faff39d3903bec44
parentd9adf77d2eb997808120440ba09f65d2e609f722 (diff)
downloadcython-c7dc24bc0a39af708976fb0a4bf63d2ee407badb.tar.gz
runtests: include the C compiler error output in the compile exception to show it at the end of the test run.
-rw-r--r--Cython/Build/IpythonMagic.py33
-rw-r--r--Cython/Utils.py31
-rwxr-xr-xruntests.py17
3 files changed, 48 insertions, 33 deletions
diff --git a/Cython/Build/IpythonMagic.py b/Cython/Build/IpythonMagic.py
index 15868d862..f074be168 100644
--- a/Cython/Build/IpythonMagic.py
+++ b/Cython/Build/IpythonMagic.py
@@ -82,7 +82,7 @@ from ..Shadow import __version__ as cython_version
from ..Compiler.Errors import CompileError
from .Inline import cython_inline
from .Dependencies import cythonize
-from ..Utils import captured_fd
+from ..Utils import captured_fd, print_captured
PGO_CONFIG = {
@@ -107,37 +107,6 @@ else:
return name
-def get_encoding_candidates():
- candidates = [sys.getdefaultencoding()]
- for stream in (sys.stdout, sys.stdin, sys.__stdout__, sys.__stdin__):
- encoding = getattr(stream, 'encoding', None)
- # encoding might be None (e.g. somebody redirects stdout):
- if encoding is not None and encoding not in candidates:
- candidates.append(encoding)
- return candidates
-
-
-def prepare_captured(captured):
- captured_bytes = captured.strip()
- if not captured_bytes:
- return None
- for encoding in get_encoding_candidates():
- try:
- return captured_bytes.decode(encoding)
- except UnicodeDecodeError:
- pass
- # last resort: print at least the readable ascii parts correctly.
- return captured_bytes.decode('latin-1')
-
-
-def print_captured(captured, output, header_line=None):
- captured = prepare_captured(captured)
- if captured:
- if header_line:
- output.write(header_line)
- output.write(captured)
-
-
@magics_class
class CythonMagics(Magics):
diff --git a/Cython/Utils.py b/Cython/Utils.py
index 96a006eac..e6c98f583 100644
--- a/Cython/Utils.py
+++ b/Cython/Utils.py
@@ -443,6 +443,37 @@ def captured_fd(stream=2, encoding=None):
os.close(orig_stream)
+def get_encoding_candidates():
+ candidates = [sys.getdefaultencoding()]
+ for stream in (sys.stdout, sys.stdin, sys.__stdout__, sys.__stdin__):
+ encoding = getattr(stream, 'encoding', None)
+ # encoding might be None (e.g. somebody redirects stdout):
+ if encoding is not None and encoding not in candidates:
+ candidates.append(encoding)
+ return candidates
+
+
+def prepare_captured(captured):
+ captured_bytes = captured.strip()
+ if not captured_bytes:
+ return None
+ for encoding in get_encoding_candidates():
+ try:
+ return captured_bytes.decode(encoding)
+ except UnicodeDecodeError:
+ pass
+ # last resort: print at least the readable ascii parts correctly.
+ return captured_bytes.decode('latin-1')
+
+
+def print_captured(captured, output, header_line=None):
+ captured = prepare_captured(captured)
+ if captured:
+ if header_line:
+ output.write(header_line)
+ output.write(captured)
+
+
def print_bytes(s, header_text=None, end=b'\n', file=sys.stdout, flush=True):
if header_text:
file.write(header_text) # note: text! => file.write() instead of out.write()
diff --git a/runtests.py b/runtests.py
index e23fc41be..b0218ff81 100755
--- a/runtests.py
+++ b/runtests.py
@@ -1277,10 +1277,25 @@ class CythonCompileTestCase(unittest.TestCase):
extension.language = 'c++'
if IS_PY2:
workdir = str(workdir) # work around type check in distutils that disallows unicode strings
+
build_extension.extensions = [extension]
build_extension.build_temp = workdir
build_extension.build_lib = workdir
- build_extension.run()
+
+ from Cython.Utils import captured_fd, prepare_captured
+ from distutils.errors import CompileError
+
+ error = None
+ with captured_fd(2) as get_stderr:
+ try:
+ build_extension.run()
+ except CompileError as exc:
+ error = exc
+ stderr = prepare_captured(get_stderr())
+ if stderr:
+ print("Compiler output for module %s:\n%s" % (module, stderr))
+ if error is not None:
+ raise CompileError("%s\nCompiler output:\n%s" % (error, stderr))
finally:
os.chdir(cwd)