From 5e6f29bfb43d03dd82e413ff60572f21648fe235 Mon Sep 17 00:00:00 2001 From: paugier Date: Wed, 24 Jan 2024 15:34:47 +0100 Subject: [PATCH] Try to avoid Fatal Python error Illegal instruction --- src/fluidfft/__init__.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/fluidfft/__init__.py b/src/fluidfft/__init__.py index dd2135c..7d8f971 100644 --- a/src/fluidfft/__init__.py +++ b/src/fluidfft/__init__.py @@ -28,7 +28,9 @@ """ -from importlib import import_module as _import_module +import importlib +import subprocess +import sys from fluiddyn.util.mpi import printby0 @@ -114,15 +116,26 @@ def import_fft_class(method, raise_import_error=True): "not method.startswith('fluidfft.')\nmethod = {}".format(method) ) + if any(method.endswith(postfix) for postfix in ("pfft", "p3dfft")): + # for few methods, try before real import because importing can lead to + # a fatal error (Illegal instruction) + try: + subprocess.check_call([sys.executable, "-c", "import " + method]) + except subprocess.CalledProcessError as error: + if raise_import_error: + raise ImportError(method) from error + + printby0("ImportError:", method) + return None + try: - mod = _import_module(method) + mod = importlib.import_module(method) except ImportError: if raise_import_error: - raise ImportError(method) + raise - else: - printby0("ImportError:", method) - return None + printby0("ImportError:", method) + return None return mod.FFTclass