backport of: https://github.com/neovim/pynvim/commit/eaa862dec340cad523a691d6441e890b800f8b65 https://github.com/neovim/pynvim/commit/919217d9211adabd7fd57085657096a5060aece4 https://github.com/neovim/pynvim/commit/dd540b08e1a13c89557aa91a122ca6fbc9dfe2ef --- a/pynvim/compat.py +++ b/pynvim/compat.py @@ -2,7 +2,6 @@ import sys import warnings -from imp import find_module as original_find_module IS_PYTHON3 = sys.version_info >= (3, 0) @@ -27,6 +26,8 @@ else: newpath.append(element) path = newpath + + from imp import find_module as original_find_module return original_find_module(fullname, path) # There is no 'long' type in Python3 just int --- a/pynvim/plugin/script_host.py +++ b/pynvim/plugin/script_host.py @@ -1,5 +1,4 @@ """Legacy python/python3-vim emulation.""" -import imp import io import logging import os @@ -214,6 +213,7 @@ return discover_runtime_directories(nvim) def _find_module(fullname, oldtail, path): + import imp idx = oldtail.find('.') if idx > 0: name = oldtail[:idx] @@ -234,6 +234,7 @@ return sys.modules[fullname] except KeyError: pass + import imp return imp.load_module(fullname, *self.module) class VimPathFinder(object): --- a/pynvim/plugin/host.py +++ b/pynvim/plugin/host.py @@ -1,5 +1,4 @@ """Implements a Nvim host for python plugins.""" -import imp import inspect import logging import os @@ -9,7 +8,7 @@ from traceback import format_exc from pynvim.api import decode_if_bytes, walk -from pynvim.compat import IS_PYTHON3, find_module +from pynvim.compat import IS_PYTHON3 from pynvim.msgpack_rpc import ErrorResponse from pynvim.plugin import script_host from pynvim.util import format_exc_skip, get_client_info @@ -23,6 +22,26 @@ host_method_spec = {"poll": {}, "specs": {"nargs": 1}, "shutdown": {}} +def handle_import(directory, name): + """Import a python file given a known location. + Currently works on both python2 or 3. + """ + try: # Python3 + from importlib.util import module_from_spec, spec_from_file_location + except ImportError: # Python2.7 + import imp + from pynvim.compat import find_module + file, pathname, descr = find_module(name, [directory]) + module = imp.load_module(name, file, pathname, descr) + return module + else: + spec = spec_from_file_location(name, location=directory) + if spec is not None: + return module_from_spec(spec) + else: + raise ImportError + + class Host(object): """Nvim host for python plugins. @@ -161,8 +180,10 @@ has_script = True else: directory, name = os.path.split(os.path.splitext(path)[0]) - file, pathname, descr = find_module(name, [directory]) - module = imp.load_module(name, file, pathname, descr) + try: + module = handle_import(directory, name) + except ImportError: + return handlers = [] self._discover_classes(module, handlers, path) self._discover_functions(module, handlers, path, False)