From ae54ea733218c6566030171733d4946f5adf255b Mon Sep 17 00:00:00 2001 From: William Woodall Date: Tue, 5 Apr 2016 19:10:15 -0700 Subject: [PATCH] add message when the rmw implementation import failure goes unhandled --- rclpy/rclpy/__init__.py | 3 ++- rclpy/rclpy/impl/rmw_implementation_tools.py | 26 ++++++++++++++++++-- rclpy/test/test_select_rmw_implementation.py | 4 +-- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/rclpy/rclpy/__init__.py b/rclpy/rclpy/__init__.py index cd0a0bb45..a3d1693c3 100644 --- a/rclpy/rclpy/__init__.py +++ b/rclpy/rclpy/__init__.py @@ -19,6 +19,7 @@ from rclpy.exceptions import InvalidRCLPYImplementation from rclpy.impl import implementation_singleton from rclpy.impl import rmw_implementation_tools +from rclpy.impl.rmw_implementation_tools import RCLPY_IMPLEMENTATION_ENV_NAME from rclpy.node import Node from rclpy.impl import excepthook @@ -29,7 +30,7 @@ def init(args=None): - rclpy_rmw_env = os.getenv('RCLPY_IMPLEMENTATION', None) + rclpy_rmw_env = os.getenv(RCLPY_IMPLEMENTATION_ENV_NAME, None) if rclpy_rmw_env is not None: available_rmw_implementations = rmw_implementation_tools.get_rmw_implementations() if rclpy_rmw_env not in available_rmw_implementations: diff --git a/rclpy/rclpy/impl/rmw_implementation_tools.py b/rclpy/rclpy/impl/rmw_implementation_tools.py index abe0527d9..6aee1433a 100644 --- a/rclpy/rclpy/impl/rmw_implementation_tools.py +++ b/rclpy/rclpy/impl/rmw_implementation_tools.py @@ -19,11 +19,14 @@ from rclpy.exceptions import ImplementationAlreadyImportedException from rclpy.exceptions import InvalidRCLPYImplementation +from rclpy.impl.excepthook import add_unhandled_exception_addendum __rmw_implementations = None __selected_rmw_implementation = None __rmw_implementation_module = None +RCLPY_IMPLEMENTATION_ENV_NAME = 'RCLPY_IMPLEMENTATION' + def reload_rmw_implementations(): """(Re)Load the available rmw implementations by inspecting the ament index.""" @@ -77,13 +80,32 @@ def import_rmw_implementation(): global __rmw_implementation_module if __rmw_implementation_module is not None: return __rmw_implementation_module + available_implementations = get_rmw_implementations() if __selected_rmw_implementation is None: logger = logging.getLogger('rclpy') - select_rmw_implementation(get_rmw_implementations()[0]) # select the first one + select_rmw_implementation(available_implementations[0]) # select the first one logger.debug("Implicitly selecting the '{0}' rmw implementation." .format(__selected_rmw_implementation)) module_name = '._rclpy__{rmw_implementation}'.format( rmw_implementation=__selected_rmw_implementation, ) - __rmw_implementation_module = importlib.import_module(module_name, package='rclpy') + try: + __rmw_implementation_module = importlib.import_module(module_name, package='rclpy') + except ImportError as exc: + if "No module named 'rclpy._rclpy__" in str(exc): + log_args = [ + __selected_rmw_implementation, + RCLPY_IMPLEMENTATION_ENV_NAME, + '\n'.join([' - {0}'.format(x) for x in available_implementations]), + ] + # This message will only be printed if this exception goes unhandled. + add_unhandled_exception_addendum( + exc, + "\n" + "Failed to import the Python extension for the '{0}' rmw implementation.\n" + "A different rmw implementation can be selected using the '{1}' env variable.\n" + "These are the available rmw implementations:\n" + "{2}\n".format(*log_args) + ) + raise return __rmw_implementation_module diff --git a/rclpy/test/test_select_rmw_implementation.py b/rclpy/test/test_select_rmw_implementation.py index a02484fe2..179b86b2a 100644 --- a/rclpy/test/test_select_rmw_implementation.py +++ b/rclpy/test/test_select_rmw_implementation.py @@ -46,7 +46,7 @@ def func_import_each_available_rmw_implementation(rmw_implementation): try: rclpy.init([]) except ImportError as exc: - if "No module named 'rclpy._rclpy__" in '{0}'.format(exc): + if "No module named 'rclpy._rclpy__" in str(exc): # Not all rmw implementations generate a Python implementation. # TODO(wjwwood): prune get_rmw_implementations by # implementations that have generated a Python implementation @@ -96,7 +96,7 @@ def func_select_rmw_implementation_by_environment(rmw_implementation): try: rclpy.init([]) except ImportError as exc: - if "No module named 'rclpy._rclpy__" in '{0}'.format(exc): + if "No module named 'rclpy._rclpy__" in str(exc): # Not all rmw implementations generate a Python implementation. # TODO(wjwwood): prune get_rmw_implementations by # implementations that have generated a Python implementation