Skip to content

Commit a29f34e

Browse files
committed
hamster wheel: avoid "imp" module
"imp" was deprecated in python 3.7. closes #351
1 parent 22e5919 commit a29f34e

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

pynvim/compat.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,23 @@
22

33
import sys
44
import warnings
5-
from imp import find_module as original_find_module
5+
if sys.version_info >= (3, 4):
6+
from importlib.machinery import PathFinder
7+
original_find_module = PathFinder.find_spec
8+
else:
9+
from imp import find_module as original_find_module
610

711

812
IS_PYTHON3 = sys.version_info >= (3, 0)
13+
IS_PYTHON3_7 = sys.version_info >= (3, 7)
914

1015

1116
if IS_PYTHON3:
1217
def find_module(fullname, path):
13-
"""Compatibility wrapper for imp.find_module.
18+
"""Compatibility wrapper for imp.find_module
19+
(or find_spec in Python 3.7+).
1420
15-
Automatically decodes arguments of find_module, in Python3
16-
they must be Unicode
21+
Automatically decodes arguments (must be Unicode in Python3).
1722
"""
1823
if isinstance(fullname, bytes):
1924
fullname = fullname.decode()

pynvim/plugin/script_host.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
"""Legacy python/python3-vim emulation."""
2-
import imp
2+
try:
3+
import importlib
4+
except ImportError:
5+
import imp
36
import io
47
import logging
58
import os
69
import sys
710

811
from .decorators import plugin, rpc_export
912
from ..api import Nvim, walk
13+
from ..compat import IS_PYTHON3, IS_PYTHON3_7, find_module
1014
from ..msgpack_rpc import ErrorResponse
1115
from ..util import format_exc_skip
1216

@@ -16,8 +20,6 @@
1620
logger = logging.getLogger(__name__)
1721
debug, info, warn = (logger.debug, logger.info, logger.warn,)
1822

19-
IS_PYTHON3 = sys.version_info >= (3, 0)
20-
2123
if IS_PYTHON3:
2224
basestring = str
2325

@@ -38,7 +40,10 @@ def __init__(self, nvim):
3840
"""Initialize the legacy python-vim environment."""
3941
self.setup(nvim)
4042
# context where all code will run
41-
self.module = imp.new_module('__main__')
43+
if IS_PYTHON3_7:
44+
self.module = importlib.util.module_from_spec('__main__')
45+
else:
46+
self.module = imp.new_module('__main__')
4247
nvim.script_context = self.module
4348
# it seems some plugins assume 'sys' is already imported, so do it now
4449
exec('import sys', self.module.__dict__)
@@ -205,7 +210,7 @@ def eval(self, expr):
205210
return walk(num_to_str, obj)
206211

207212

208-
# This was copied/adapted from nvim-python help
213+
# Copied/adapted from :help if_pyth.
209214
def path_hook(nvim):
210215
def _get_paths():
211216
if nvim._thread_invalid():
@@ -217,11 +222,11 @@ def _find_module(fullname, oldtail, path):
217222
if idx > 0:
218223
name = oldtail[:idx]
219224
tail = oldtail[idx + 1:]
220-
fmr = imp.find_module(name, path)
221-
module = imp.find_module(fullname[:-len(oldtail)] + name, *fmr)
225+
fmr = find_module(name, path)
226+
module = find_module(fullname[:-len(oldtail)] + name, *fmr)
222227
return _find_module(fullname, tail, module.__path__)
223228
else:
224-
return imp.find_module(fullname, path)
229+
return find_module(fullname, path)
225230

226231
class VimModuleLoader(object):
227232
def __init__(self, module):
@@ -233,7 +238,10 @@ def load_module(self, fullname, path=None):
233238
return sys.modules[fullname]
234239
except KeyError:
235240
pass
236-
return imp.load_module(fullname, *self.module)
241+
if IS_PYTHON3_7:
242+
return importlib.import_module(fullname, *self.module)
243+
else:
244+
return imp.load_module(fullname, *self.module)
237245

238246
class VimPathFinder(object):
239247
@staticmethod

0 commit comments

Comments
 (0)