Skip to content

Commit

Permalink
Hide traceback for epyccel and lambdify errors (#1869)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
EmilyBourne committed May 13, 2024
1 parent 9ef22ae commit 19c2025
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 7 additions & 2 deletions pyccel/commands/lambdify.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

9 changes: 7 additions & 2 deletions pyccel/epyccel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 19c2025

Please sign in to comment.