Skip to content

Commit 8a6bfa2

Browse files
committed
refactor: Move some compat code to compat.py
1 parent 8df2861 commit 8a6bfa2

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

pynvim/compat.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
"""Code for supporting compatibility across python versions."""
1+
"""Code for compatibility across Python versions."""
22

33
import sys
44
import warnings
5+
if sys.version_info >= (3, 7):
6+
import types
7+
import importlib
8+
else:
9+
import imp # Deprecated in Python 3.7+
510
if sys.version_info >= (3, 4):
611
from importlib.machinery import PathFinder
712
original_find_module = PathFinder.find_spec
@@ -10,13 +15,17 @@
1015

1116

1217
IS_PYTHON3 = sys.version_info >= (3, 0)
13-
IS_PYTHON3_7 = sys.version_info >= (3, 7)
1418

19+
if sys.version_info >= (3, 7):
20+
load_module = importlib.import_module
21+
new_module = types.ModuleType
22+
else:
23+
load_module = imp.load_module
24+
new_module = imp.new_module
1525

1626
if IS_PYTHON3:
1727
def find_module(fullname, path):
18-
"""Compatibility wrapper for imp.find_module
19-
(or find_spec in Python 3.7+).
28+
"""Compat wrapper for imp.find_module, or find_spec in Python 3.7+.
2029
2130
Automatically decodes arguments (must be Unicode in Python3).
2231
"""

pynvim/plugin/script_host.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
"""Legacy python/python3-vim emulation."""
2-
try:
3-
import types
4-
import importlib
5-
except ImportError:
6-
import imp
72
import io
83
import logging
94
import os
105
import sys
116

127
from .decorators import plugin, rpc_export
138
from ..api import Nvim, walk
14-
from ..compat import IS_PYTHON3, IS_PYTHON3_7, find_module
9+
from ..compat import IS_PYTHON3, find_module, load_module, new_module
1510
from ..msgpack_rpc import ErrorResponse
1611
from ..util import format_exc_skip
1712

@@ -41,10 +36,7 @@ def __init__(self, nvim):
4136
"""Initialize the legacy python-vim environment."""
4237
self.setup(nvim)
4338
# context where all code will run
44-
if IS_PYTHON3_7:
45-
self.module = types.ModuleType('__main__')
46-
else:
47-
self.module = imp.new_module('__main__')
39+
self.module = new_module('__main__')
4840
nvim.script_context = self.module
4941
# it seems some plugins assume 'sys' is already imported, so do it now
5042
exec('import sys', self.module.__dict__)
@@ -239,10 +231,7 @@ def load_module(self, fullname, path=None):
239231
return sys.modules[fullname]
240232
except KeyError:
241233
pass
242-
if IS_PYTHON3_7:
243-
return importlib.import_module(fullname, *self.module)
244-
else:
245-
return imp.load_module(fullname, *self.module)
234+
return load_module(fullname, *self.module)
246235

247236
class VimPathFinder(object):
248237
@staticmethod

0 commit comments

Comments
 (0)