Skip to content
Snippets Groups Projects
Commit 4a01d2ca authored by Dr. Felix Tobias Schindler's avatar Dr. Felix Tobias Schindler
Browse files

[python] warn on name overwrite during import

parent 6eab6f6a
No related branches found
No related tags found
No related merge requests found
......@@ -10,10 +10,40 @@
# René Fritze (2016, 2018)
# ~~~
# Note: This import makes sure that metis is imported before python bindings from dune-xt. Importing dune-xt python bindings at any point before metis will cause a segfault due to misscommunication between libmetis5 and libscotchmetis.
# Note: This import makes sure that metis is imported before python bindings from dune-xt. Importing dune-xt python
# bindings at any point before metis will cause a segfault due to misscommunication between libmetis5 and
# libscotchmetis.
try:
import metis
except ImportError:
pass
__import__('pkg_resources').declare_namespace(__name__)
from importlib import import_module
import os
import logging
def guarded_import(globs, base_name, mod_name):
# see https://stackoverflow.com/questions/43059267/how-to-do-from-module-import-using-importlib
try:
mod = import_module('.{}'.format(mod_name), base_name)
if "__all__" in mod.__dict__:
names = mod.__dict__["__all__"]
else:
# otherwise we import all names that don't begin with _
names = [x for x in mod.__dict__ if not x.startswith("_")]
for nm in names:
if nm in globs:
logging.error('{}: overwriting existing name \'{}\' when importing from \'{}\' (continuing
anyway)!'.format(base_name, nm, mod_name))
globs.update({k: getattr(mod, k) for k in names})
except ImportError as e:
logging.error('{}: could not import module \'{}\' (continuing anyway)!'.format(base_name, mod_name))
if os.environ.get('DXT_PYTHON_DEBUG', False):
raise e
......@@ -10,35 +10,13 @@
# René Fritze (2018)
# ~~~
from importlib import import_module
from dune.xt import guarded_import
# This is here on purpose, needs to be imported as early as possible!
try:
from mpi4py import MPI
except ImportError:
pass
# empty is missing here on purpose!
_modules = (
for mod_name in (
'_common',
'_logging',
'_timings',
)
# see https://stackoverflow.com/questions/43059267/how-to-do-from-module-import-using-importlib
for mod_name in _modules:
try:
mod = import_module('.{}'.format(mod_name), 'dune.xt.common')
if "__all__" in mod.__dict__:
names = mod.__dict__["__all__"]
else:
# otherwise we import all names that don't begin with _
names = [x for x in mod.__dict__ if not x.startswith("_")]
globals().update({k: getattr(mod, k) for k in names})
except ImportError as e:
import os
import logging
if os.environ.get('DXT_PYTHON_DEBUG', False):
raise e
logging.error('dune-xt-common: could not import {} module'.format(mod_name))
):
guarded_import(globals(), 'dune.xt.common', mod_name)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment