From 19c202592cb6333a12b179f43b300d4fb68339db Mon Sep 17 00:00:00 2001 From: EmilyBourne Date: Mon, 13 May 2024 15:46:19 +0200 Subject: [PATCH] Hide traceback for epyccel and lambdify errors (#1869) Add a `try/except` around internal Pyccel calls to catch all `PyccelError`s. This allows the shortest possible traceback to be shown to the user when the error is recognised, handled and reported by Pyccel. Fixes #1868. --- CHANGELOG.md | 1 + pyccel/commands/lambdify.py | 9 +++++++-- pyccel/epyccel.py | 9 +++++++-- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a4d2772a1a..4a0af0dfdf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file. - #1830 : Add a `pyccel.lambdify.lambdify` function to accelerate SymPy expressions. - #1867 : Add a `use_out` parameter to `pyccel.lambdify` to avoid unnecessary memory allocation. - #1867 : Auto-generate a docstring for functions generated via calls to `pyccel.lambdify`. +- #1868 : Hide traceback for `epyccel` and `lambdify` errors. ### Fixed diff --git a/pyccel/commands/lambdify.py b/pyccel/commands/lambdify.py index 88f8c9608a..c3220b2865 100644 --- a/pyccel/commands/lambdify.py +++ b/pyccel/commands/lambdify.py @@ -5,8 +5,9 @@ import sympy as sp from packaging import version -from pyccel.epyccel import epyccel +from pyccel.epyccel import epyccel from pyccel.utilities.strings import random_string +from pyccel.errors.errors import PyccelError if version.parse(sp.__version__) >= version.parse('1.8'): from sympy.printing.numpy import NumPyPrinter @@ -117,6 +118,10 @@ def lambdify(expr : sp.Expr, args : 'dict[sp.Symbol, str]', *, result_type : str docstring += '\n """' func = '\n'.join((numpy_import, decorators, signature, docstring, code)) - package = epyccel(func, **kwargs) + try: + package = epyccel(func, **kwargs) + except PyccelError as e: + raise type(e)(str(e)) from None + return getattr(package, func_name) diff --git a/pyccel/epyccel.py b/pyccel/epyccel.py index c5f288814a..f4fc277ed2 100644 --- a/pyccel/epyccel.py +++ b/pyccel/epyccel.py @@ -17,7 +17,7 @@ from pyccel.utilities.strings import random_string from pyccel.codegen.pipeline import execute_pyccel -from pyccel.errors.errors import ErrorsMode +from pyccel.errors.errors import ErrorsMode, PyccelError __all__ = ['get_source_function', 'epyccel_seq', 'epyccel'] @@ -344,6 +344,8 @@ def epyccel( python_function_or_module, **kwargs ): fun_name = python_function_or_module.__name__ if fun else None success = True # error handling carried out after broadcast to prevent deadlocks + except PyccelError as e: + raise type(e)(str(e)) from None except BaseException as e: # pylint: disable=broad-except exc_info = e success = False @@ -381,7 +383,10 @@ def epyccel( python_function_or_module, **kwargs ): # Serial version else: - mod, fun = epyccel_seq( python_function_or_module, **kwargs ) + try: + mod, fun = epyccel_seq( python_function_or_module, **kwargs ) + except PyccelError as e: + raise type(e)(str(e)) from None # Return Fortran function (if any), otherwise module return fun or mod