Commit 12c7237b authored by Stefan Behnel's avatar Stefan Behnel

Add "show_all_warnings" option to cythonize() to provide a simpler interface...

Add "show_all_warnings" option to cythonize() to provide a simpler interface for (temporarily) setting "Errors.LEVEL".
parent 34e357cb
...@@ -45,6 +45,7 @@ except: ...@@ -45,6 +45,7 @@ except:
from .. import Utils from .. import Utils
from ..Utils import (cached_function, cached_method, path_exists, from ..Utils import (cached_function, cached_method, path_exists,
safe_makedirs, copy_file_to_dir_if_newer, is_package_dir, replace_suffix) safe_makedirs, copy_file_to_dir_if_newer, is_package_dir, replace_suffix)
from ..Compiler import Errors
from ..Compiler.Main import Context from ..Compiler.Main import Context
from ..Compiler.Options import CompilationOptions, default_options from ..Compiler.Options import CompilationOptions, default_options
...@@ -882,7 +883,7 @@ def create_extension_list(patterns, exclude=None, ctx=None, aliases=None, quiet= ...@@ -882,7 +883,7 @@ def create_extension_list(patterns, exclude=None, ctx=None, aliases=None, quiet=
# This is the user-exposed entry point. # This is the user-exposed entry point.
def cythonize(module_list, exclude=None, nthreads=0, aliases=None, quiet=False, force=False, language=None, def cythonize(module_list, exclude=None, nthreads=0, aliases=None, quiet=False, force=False, language=None,
exclude_failures=False, **options): exclude_failures=False, show_all_warnings=False, **options):
""" """
Compile a set of source modules into C/C++ files and return a list of distutils Compile a set of source modules into C/C++ files and return a list of distutils
Extension objects for them. Extension objects for them.
...@@ -932,6 +933,9 @@ def cythonize(module_list, exclude=None, nthreads=0, aliases=None, quiet=False, ...@@ -932,6 +933,9 @@ def cythonize(module_list, exclude=None, nthreads=0, aliases=None, quiet=False,
really makes sense for compiling ``.py`` files which can also really makes sense for compiling ``.py`` files which can also
be used without compilation. be used without compilation.
:param show_all_warnings: By default, not all Cython warnings are printed.
Set to true to show all warnings.
:param annotate: If ``True``, will produce a HTML file for each of the ``.pyx`` or ``.py`` :param annotate: If ``True``, will produce a HTML file for each of the ``.pyx`` or ``.py``
files compiled. The HTML file gives an indication files compiled. The HTML file gives an indication
of how much Python interaction there is in of how much Python interaction there is in
...@@ -1058,7 +1062,7 @@ def cythonize(module_list, exclude=None, nthreads=0, aliases=None, quiet=False, ...@@ -1058,7 +1062,7 @@ def cythonize(module_list, exclude=None, nthreads=0, aliases=None, quiet=False,
to_compile.append(( to_compile.append((
priority, source, c_file, fingerprint, quiet, priority, source, c_file, fingerprint, quiet,
options, not exclude_failures, module_metadata.get(m.name), options, not exclude_failures, module_metadata.get(m.name),
full_module_name)) full_module_name, show_all_warnings))
new_sources.append(c_file) new_sources.append(c_file)
modules_by_cfile[c_file].append(m) modules_by_cfile[c_file].append(m)
else: else:
...@@ -1101,7 +1105,7 @@ def cythonize(module_list, exclude=None, nthreads=0, aliases=None, quiet=False, ...@@ -1101,7 +1105,7 @@ def cythonize(module_list, exclude=None, nthreads=0, aliases=None, quiet=False,
pool.terminate() pool.terminate()
raise raise
pool.join() pool.join()
if not nthreads: else:
for args in to_compile: for args in to_compile:
cythonize_one(*args) cythonize_one(*args)
...@@ -1206,7 +1210,8 @@ else: ...@@ -1206,7 +1210,8 @@ else:
# TODO: Share context? Issue: pyx processing leaks into pxd module # TODO: Share context? Issue: pyx processing leaks into pxd module
@record_results @record_results
def cythonize_one(pyx_file, c_file, fingerprint, quiet, options=None, def cythonize_one(pyx_file, c_file, fingerprint, quiet, options=None,
raise_on_failure=True, embedded_metadata=None, full_module_name=None, raise_on_failure=True, embedded_metadata=None,
full_module_name=None, show_all_warnings=False,
progress=""): progress=""):
from ..Compiler.Main import compile_single, default_options from ..Compiler.Main import compile_single, default_options
from ..Compiler.Errors import CompileError, PyrexError from ..Compiler.Errors import CompileError, PyrexError
...@@ -1242,6 +1247,10 @@ def cythonize_one(pyx_file, c_file, fingerprint, quiet, options=None, ...@@ -1242,6 +1247,10 @@ def cythonize_one(pyx_file, c_file, fingerprint, quiet, options=None,
options.output_file = c_file options.output_file = c_file
options.embedded_metadata = embedded_metadata options.embedded_metadata = embedded_metadata
old_warning_level = Errors.LEVEL
if show_all_warnings:
Errors.LEVEL = 0
any_failures = 0 any_failures = 0
try: try:
result = compile_single(pyx_file, options, full_module_name=full_module_name) result = compile_single(pyx_file, options, full_module_name=full_module_name)
...@@ -1259,6 +1268,10 @@ def cythonize_one(pyx_file, c_file, fingerprint, quiet, options=None, ...@@ -1259,6 +1268,10 @@ def cythonize_one(pyx_file, c_file, fingerprint, quiet, options=None,
import traceback import traceback
traceback.print_exc() traceback.print_exc()
any_failures = 1 any_failures = 1
finally:
if show_all_warnings:
Errors.LEVEL = old_warning_level
if any_failures: if any_failures:
if raise_on_failure: if raise_on_failure:
raise CompileError(None, pyx_file) raise CompileError(None, pyx_file)
......
# mode: run
PYTHON setup.py build_ext --inplace PYTHON setup.py build_ext --inplace
PYTHON -c "import a" PYTHON -c "import a"
...@@ -5,11 +7,31 @@ PYTHON -c "import a" ...@@ -5,11 +7,31 @@ PYTHON -c "import a"
from Cython.Build.Dependencies import cythonize from Cython.Build.Dependencies import cythonize
import sys
from distutils.core import setup from distutils.core import setup
from io import StringIO
old_stderr = sys.stderr
captured = sys.stderr = StringIO()
try:
setup(
ext_modules = cythonize(
"*.pyx", include_path=['subdir'],
compiler_directives={'cdivision': True},
show_all_warnings=True,
),
)
output = sys.stderr.getvalue()
finally:
sys.stderr = old_stderr
sys.stderr.write(captured.getvalue())
assert "Unraisable exception in function" in output, output
setup( ######## subdir/x.pxd ########
ext_modules = cythonize("*.pyx", include_path=['subdir'], compiler_directives={'cdivision': True}),
) ######## subdir/y.pxi ########
######## a.pyx ######## ######## a.pyx ########
...@@ -22,8 +44,6 @@ def mod_int_c(int a, int b): ...@@ -22,8 +44,6 @@ def mod_int_c(int a, int b):
assert mod_int_c(-1, 10) < 0 assert mod_int_c(-1, 10) < 0
# unraisable exceptions should produce a warning
######## subdir/x.pxd ######## cdef int no_exc_propagate():
raise TypeError()
######## subdir/y.pxi ########
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