Skip to content

Commit

Permalink
add message when the rmw implementation import failure goes unhandled
Browse files Browse the repository at this point in the history
  • Loading branch information
wjwwood committed Apr 6, 2016
1 parent acdd604 commit ae54ea7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
3 changes: 2 additions & 1 deletion rclpy/rclpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand Down
26 changes: 24 additions & 2 deletions rclpy/rclpy/impl/rmw_implementation_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions rclpy/test/test_select_rmw_implementation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ae54ea7

Please sign in to comment.