Skip to content

Improved rclpy import failed message - #12

Merged
wjwwood merged 5 commits into
masterfrom
improved_rclpy_import_failed_message
Apr 26, 2016
Merged

wjwwood merged 5 commits into
masterfrom
improved_rclpy_import_failed_message

Conversation

@wjwwood

@wjwwood wjwwood commented Apr 6, 2016

Copy link
Copy Markdown
Member

I achieved this by adding a helpful message which prints when the import fails, but only if it goes unhandled.

I wanted to avoid printing it always incase someone is explicitly trying each of the implementations and wants to avoid the logging output. To accomplish this I added a custom excepthook for rclpy to override the built-in sys.excepthook. You can also add "addendum's" for uncaught exceptions, which will printed before the traceback. In the custom excepthook, it looks for a matching addendum, prints it if it finds one, and then passes along the unhandled exception to the original excepthook.

This is the observed behavior:

  • In the normal case you get something like:
% python3 -c "import talker_py; talker_py.main()"

Failed to import the Python extension for the 'rmw_fastrtps_cpp' rmw implementation.
A different rmw implementation can be selected using the 'RCLPY_IMPLEMENTATION' env variable.
These are the available rmw implementations:
  - rmw_fastrtps_cpp
  - rmw_opensplice_cpp

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/william/ros2_ws/build_isolated/rclpy_examples/talker_py.py", line 27, in main
    rclpy.init(args)
  File "/Users/william/ros2_ws/install_isolated/rclpy/lib/python3.5/site-packages/rclpy/__init__.py", line 47, in init
    rmw_implementation_tools.import_rmw_implementation()
  File "/Users/william/ros2_ws/install_isolated/rclpy/lib/python3.5/site-packages/rclpy/impl/rmw_implementation_tools.py", line 90, in import_rmw_implementation
    __rmw_implementation_module = importlib.import_module(module_name, package='rclpy')
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
ImportError: No module named 'rclpy._rclpy__rmw_fastrtps_cpp'
  • In the case that you catch it, you get:
% python3 -c "
import talker_py
try:
    talker_py.main()
except ImportError:
    print('failed to import')
"
failed to import

@wjwwood wjwwood added the in progress Actively being worked on (Kanban column) label Apr 6, 2016
@wjwwood
wjwwood force-pushed the improved_rclpy_import_failed_message branch from 50c8d03 to ae54ea7 Compare April 6, 2016 02:16
@wjwwood

wjwwood commented Apr 6, 2016

Copy link
Copy Markdown
Member Author

I also addressed the style problems from #10 (comment).

@wjwwood wjwwood added in review Waiting for review (Kanban column) and removed in progress Actively being worked on (Kanban column) labels Apr 6, 2016
@wjwwood
wjwwood force-pushed the improved_rclpy_import_failed_message branch from b2f62dd to ae54ea7 Compare April 6, 2016 02:38
@wjwwood

wjwwood commented Apr 6, 2016

Copy link
Copy Markdown
Member Author

Actually, scratch that. I remove the commit that cleans up #10 so I can put it in a different pr.

@dirk-thomas dirk-thomas mentioned this pull request Apr 6, 2016
@wjwwood wjwwood self-assigned this Apr 6, 2016
wjwwood added 3 commits April 12, 2016 13:23
this excepthook allows you to register something
to be printed for an exception, but only if it
goes unhandled.
@wjwwood
wjwwood force-pushed the improved_rclpy_import_failed_message branch from ca04d1f to d81e9fd Compare April 12, 2016 20:24
try:
__rmw_implementation_module = importlib.import_module(module_name, package='rclpy')
except ImportError as exc:
if "No module named 'rclpy._rclpy__" in str(exc):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if you could do slightly better than hardcoding the module prefix here. If the module name changed from rclpy._rclpy__rmw_.... to something else then this logic would break. Wouldn't something like "No module named 'rclpy.{0}".format(module_name) work?

@wjwwood

wjwwood commented Apr 13, 2016

Copy link
Copy Markdown
Member Author

I believe all the CI is satisfactory (with my pep257 fix), looking for final reviews before squash and merge.

@wjwwood

wjwwood commented Apr 26, 2016

Copy link
Copy Markdown
Member Author

I believe all the CI is satisfactory (with my pep257 fix), looking for final reviews before squash and merge.

bump

@jackieokay

Copy link
Copy Markdown
Contributor

+1

@wjwwood
wjwwood merged commit 18669b7 into master Apr 26, 2016
@wjwwood
wjwwood deleted the improved_rclpy_import_failed_message branch April 26, 2016 20:07
@wjwwood wjwwood removed the in review Waiting for review (Kanban column) label Apr 26, 2016
if available_implementations:
select_rmw_implementation(available_implementations[0]) # select the first one
else:
raise NoImplementationAvailableException()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest the opposite order:

if not available_implementations:
    raise NoImplementationAvailableException()
select_rmw_implementation(available_implementations[0])  # select the first one

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it makes a difference, but if you want to change it I'm fine with that.

@wjwwood wjwwood mentioned this pull request Apr 26, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants