|
1 | 1 | """Implements a Nvim host for python plugins.""" |
2 | | -import imp |
3 | 2 | import inspect |
4 | 3 | import logging |
5 | 4 | import os |
|
9 | 8 | from traceback import format_exc |
10 | 9 |
|
11 | 10 | from pynvim.api import decode_if_bytes, walk |
12 | | -from pynvim.compat import IS_PYTHON3, find_module |
| 11 | +from pynvim.compat import IS_PYTHON3 |
13 | 12 | from pynvim.msgpack_rpc import ErrorResponse |
14 | 13 | from pynvim.plugin import script_host |
15 | 14 | from pynvim.util import format_exc_skip, get_client_info |
|
23 | 22 | host_method_spec = {"poll": {}, "specs": {"nargs": 1}, "shutdown": {}} |
24 | 23 |
|
25 | 24 |
|
| 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 | + |
26 | 46 | class Host(object): |
27 | 47 |
|
28 | 48 | """Nvim host for python plugins. |
@@ -161,8 +181,10 @@ def _load(self, plugins): |
161 | 181 | has_script = True |
162 | 182 | else: |
163 | 183 | 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 |
166 | 188 | handlers = [] |
167 | 189 | self._discover_classes(module, handlers, path) |
168 | 190 | self._discover_functions(module, handlers, path, False) |
@@ -232,12 +254,12 @@ def predicate(o): |
232 | 254 | if sync: |
233 | 255 | if method in self._request_handlers: |
234 | 256 | raise Exception(('Request handler for "{}" is ' |
235 | | - + 'already registered').format(method)) |
| 257 | + + 'already registered').format(method)) |
236 | 258 | self._request_handlers[method] = fn_wrapped |
237 | 259 | else: |
238 | 260 | if method in self._notification_handlers: |
239 | 261 | raise Exception(('Notification handler for "{}" is ' |
240 | | - + 'already registered').format(method)) |
| 262 | + + 'already registered').format(method)) |
241 | 263 | self._notification_handlers[method] = fn_wrapped |
242 | 264 | if hasattr(fn, '_nvim_rpc_spec'): |
243 | 265 | specs.append(fn._nvim_rpc_spec) |
|
0 commit comments