1
1
"""Legacy python/python3-vim emulation."""
2
- import imp
2
+ try :
3
+ import importlib
4
+ except ImportError :
5
+ import imp
3
6
import io
4
7
import logging
5
8
import os
6
9
import sys
7
10
8
11
from .decorators import plugin , rpc_export
9
12
from ..api import Nvim , walk
13
+ from ..compat import IS_PYTHON3 , IS_PYTHON3_7 , find_module
10
14
from ..msgpack_rpc import ErrorResponse
11
15
from ..util import format_exc_skip
12
16
16
20
logger = logging .getLogger (__name__ )
17
21
debug , info , warn = (logger .debug , logger .info , logger .warn ,)
18
22
19
- IS_PYTHON3 = sys .version_info >= (3 , 0 )
20
-
21
23
if IS_PYTHON3 :
22
24
basestring = str
23
25
@@ -38,7 +40,10 @@ def __init__(self, nvim):
38
40
"""Initialize the legacy python-vim environment."""
39
41
self .setup (nvim )
40
42
# 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__' )
42
47
nvim .script_context = self .module
43
48
# it seems some plugins assume 'sys' is already imported, so do it now
44
49
exec ('import sys' , self .module .__dict__ )
@@ -205,7 +210,7 @@ def eval(self, expr):
205
210
return walk (num_to_str , obj )
206
211
207
212
208
- # This was copied /adapted from nvim-python help
213
+ # Copied /adapted from : help if_pyth.
209
214
def path_hook (nvim ):
210
215
def _get_paths ():
211
216
if nvim ._thread_invalid ():
@@ -217,11 +222,11 @@ def _find_module(fullname, oldtail, path):
217
222
if idx > 0 :
218
223
name = oldtail [:idx ]
219
224
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 )
222
227
return _find_module (fullname , tail , module .__path__ )
223
228
else :
224
- return imp . find_module (fullname , path )
229
+ return find_module (fullname , path )
225
230
226
231
class VimModuleLoader (object ):
227
232
def __init__ (self , module ):
@@ -233,7 +238,10 @@ def load_module(self, fullname, path=None):
233
238
return sys .modules [fullname ]
234
239
except KeyError :
235
240
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 )
237
245
238
246
class VimPathFinder (object ):
239
247
@staticmethod
0 commit comments