Skip to content

Commit dd540b0

Browse files
refactor: remove usage of imp #461
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
1 parent 496e8eb commit dd540b0

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

pynvim/plugin/host.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
"""Implements a Nvim host for python plugins."""
2-
import imp
32
import inspect
43
import logging
54
import os
@@ -9,7 +8,7 @@
98
from traceback import format_exc
109

1110
from pynvim.api import decode_if_bytes, walk
12-
from pynvim.compat import IS_PYTHON3, find_module
11+
from pynvim.compat import IS_PYTHON3
1312
from pynvim.msgpack_rpc import ErrorResponse
1413
from pynvim.plugin import script_host
1514
from pynvim.util import format_exc_skip, get_client_info
@@ -23,6 +22,27 @@
2322
host_method_spec = {"poll": {}, "specs": {"nargs": 1}, "shutdown": {}}
2423

2524

25+
def handle_import(directory, name):
26+
"""Import a python file given a known location.
27+
28+
Currently works on both python2 or 3.
29+
"""
30+
try: # Python3
31+
from importlib.util import module_from_spec, spec_from_file_location
32+
except ImportError: # Python2.7
33+
import imp
34+
from pynvim.compat import find_module
35+
file, pathname, descr = find_module(name, [directory])
36+
module = imp.load_module(name, file, pathname, descr)
37+
return module
38+
else:
39+
spec = spec_from_file_location(name, location=directory)
40+
if spec is not None:
41+
return module_from_spec(spec)
42+
else:
43+
raise ImportError
44+
45+
2646
class Host(object):
2747

2848
"""Nvim host for python plugins.
@@ -161,8 +181,10 @@ def _load(self, plugins):
161181
has_script = True
162182
else:
163183
directory, name = os.path.split(os.path.splitext(path)[0])
164-
file, pathname, descr = find_module(name, [directory])
165-
module = imp.load_module(name, file, pathname, descr)
184+
try:
185+
module = handle_import(directory, name)
186+
except ImportError:
187+
return
166188
handlers = []
167189
self._discover_classes(module, handlers, path)
168190
self._discover_functions(module, handlers, path, False)
@@ -232,12 +254,12 @@ def predicate(o):
232254
if sync:
233255
if method in self._request_handlers:
234256
raise Exception(('Request handler for "{}" is '
235-
+ 'already registered').format(method))
257+
+ 'already registered').format(method))
236258
self._request_handlers[method] = fn_wrapped
237259
else:
238260
if method in self._notification_handlers:
239261
raise Exception(('Notification handler for "{}" is '
240-
+ 'already registered').format(method))
262+
+ 'already registered').format(method))
241263
self._notification_handlers[method] = fn_wrapped
242264
if hasattr(fn, '_nvim_rpc_spec'):
243265
specs.append(fn._nvim_rpc_spec)

0 commit comments

Comments
 (0)