# Copyright 2016-2017 Open Source Robotics Foundation, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import importlib
import os

from rpyutils import add_dll_directories_from_env


def _import(name):
    try:
        # Since Python 3.8, on Windows we should ensure DLL directories are explicitly added
        # to the search path.
        # See https://docs.python.org/3/whatsnew/3.8.html#bpo-36085-whatsnew
        with add_dll_directories_from_env('PATH'):
            return importlib.import_module(name, package='rclpy')
    except ImportError as e:
        if e.path is not None and os.path.isfile(e.path):
            e.msg += \
                "\nThe C extension '%s' failed to be imported while being present on the system." \
                " Please refer to '%s' for possible solutions" % \
                (e.path, 'https://index.ros.org/doc/ros2/Troubleshooting/'
                         '#import-failing-even-with-library-present-on-the-system')
        raise

@@ -626,16 +626,41 @@
 mport os
+%0Afrom pathlib import Path
 %0A%0Afrom r
@@ -1079,16 +1079,606 @@
 r as e:%0A
+        if e.path is None:%0A            import sysconfig%0A            expected_path = Path(__file__).parents%5B1%5D / (%0A                name%5B1:%5D + sysconfig.get_config_var('EXT_SUFFIX'))%0A            assert not expected_path.is_file()%0A            e.msg += %5C%0A                f%22%5CnThe C extension '%7Bexpected_path%7D' isn't present on the %22 %5C%0A                %22system. Please refer to 'https://index.ros.org/doc/ros2/%22 %5C%0A                'Troubleshooting/Installation-Troubleshooting/#import-' %5C%0A                %22failing-without-library-present-on-the-system' for %22 %5C%0A                'possible solutions'%0A
         

improve error message if rclpy C extensions are not found (#580)