Skip to content

Commit

Permalink
refactor: remove usage of imp #461
Browse files Browse the repository at this point in the history
Since there are no reasonable alternatives to imp in python2.7,
try importing python3 modules and if that fails, fall back to the
previous method. However this will remove that annoying
DeprecationWarning for everyone using python3.

fix #478
  • Loading branch information
farisachugthai committed Jul 14, 2023
1 parent 496e8eb commit dd540b0
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions pynvim/plugin/host.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Implements a Nvim host for python plugins."""
import imp
import inspect
import logging
import os
Expand All @@ -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
Expand All @@ -23,6 +22,27 @@
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.
Expand Down Expand Up @@ -161,8 +181,10 @@ def _load(self, plugins):
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)
Expand Down Expand Up @@ -232,12 +254,12 @@ def predicate(o):
if sync:
if method in self._request_handlers:
raise Exception(('Request handler for "{}" is '
+ 'already registered').format(method))
+ 'already registered').format(method))
self._request_handlers[method] = fn_wrapped
else:
if method in self._notification_handlers:
raise Exception(('Notification handler for "{}" is '
+ 'already registered').format(method))
+ 'already registered').format(method))
self._notification_handlers[method] = fn_wrapped
if hasattr(fn, '_nvim_rpc_spec'):
specs.append(fn._nvim_rpc_spec)
Expand Down

0 comments on commit dd540b0

Please sign in to comment.