Skip to content

Commit 95e6995

Browse files
authored
Merge pull request #364 from justinmk/avoid-deprecated
2 parents e29e4df + c771bfd commit 95e6995

File tree

5 files changed

+49
-26
lines changed

5 files changed

+49
-26
lines changed

README.md

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,23 @@ See the [Python Plugin API](http://pynvim.readthedocs.io/en/latest/usage/python-
5656

5757
#### Development
5858

59-
If you change the code, you need to run
60-
```sh
61-
pip2 install .
62-
pip3 install .
63-
```
64-
for the changes to have effect. For instructions of testing and troubleshooting,
65-
see the [development](http://pynvim.readthedocs.io/en/latest/development.html) documentation.
59+
Use (and activate) a local virtualenv.
60+
61+
python3 -m venv env36
62+
source env36/bin/activate
63+
64+
If you change the code, you must reinstall for the changes to take effect:
65+
66+
pip install .
67+
68+
Use `pytest` to run the tests. Invoking with `python -m` prepends the current
69+
directory to `sys.path` (otherwise `pytest` might find other versions!):
70+
71+
python -m pytest
72+
73+
For details about testing and troubleshooting, see the
74+
[development](http://pynvim.readthedocs.io/en/latest/development.html)
75+
documentation.
6676

6777
#### Usage through the python REPL
6878

docs/development.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ in order for Neovim to use it for the plugin host.
1919

2020
To run the tests execute::
2121

22-
pytest
22+
python -m pytest
23+
24+
This will run the tests in an embedded instance of Neovim, with the current
25+
directory added to ``sys.path``.
2326

24-
This will run the tests in an embedded instance of Neovim.
2527
If you want to test a different version than ``nvim`` in ``$PATH`` use::
2628

2729
NVIM_CHILD_ARGV='["/path/to/nvim", "-u", "NONE", "--embed"]' pytest
@@ -30,7 +32,7 @@ Alternatively, if you want to see the state of nvim, you could use::
3032

3133
export NVIM_LISTEN_ADDRESS=/tmp/nvimtest
3234
xterm -e "nvim -u NONE"&
33-
pytest
35+
python -m pytest
3436

3537
But note you need to restart Neovim every time you run the tests!
3638
Substitute your favorite terminal emulator for ``xterm``.

pynvim/compat.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,33 @@
1-
"""Code for supporting compatibility across python versions."""
1+
"""Code for compatibility across Python versions."""
22

33
import sys
44
import warnings
5-
from imp import find_module as original_find_module
5+
if sys.version_info >= (3, 7):
6+
import types
7+
import importlib
8+
else:
9+
import imp # Deprecated in Python 3.7+
10+
if sys.version_info >= (3, 4):
11+
from importlib.machinery import PathFinder
12+
original_find_module = PathFinder.find_spec
13+
else:
14+
from imp import find_module as original_find_module
615

716

817
IS_PYTHON3 = sys.version_info >= (3, 0)
918

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
1025

1126
if IS_PYTHON3:
1227
def find_module(fullname, path):
13-
"""Compatibility wrapper for imp.find_module.
28+
"""Compat wrapper for imp.find_module, or find_spec in Python 3.7+.
1429
15-
Automatically decodes arguments of find_module, in Python3
16-
they must be Unicode
30+
Automatically decodes arguments (must be Unicode in Python3).
1731
"""
1832
if isinstance(fullname, bytes):
1933
fullname = fullname.decode()

pynvim/msgpack_rpc/msgpack_stream.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ class MsgpackStream(object):
2020
def __init__(self, event_loop):
2121
"""Wrap `event_loop` on a msgpack-aware interface."""
2222
self.loop = event_loop
23-
self._packer = Packer(encoding='utf-8',
24-
unicode_errors=unicode_errors_default)
23+
self._packer = Packer(unicode_errors=unicode_errors_default)
2524
self._unpacker = Unpacker()
2625
self._message_cb = None
2726

pynvim/plugin/script_host.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
"""Legacy python/python3-vim emulation."""
2-
import imp
32
import io
43
import logging
54
import os
65
import sys
76

87
from .decorators import plugin, rpc_export
98
from ..api import Nvim, walk
9+
from ..compat import IS_PYTHON3, find_module, load_module, new_module
1010
from ..msgpack_rpc import ErrorResponse
1111
from ..util import format_exc_skip
1212

@@ -16,8 +16,6 @@
1616
logger = logging.getLogger(__name__)
1717
debug, info, warn = (logger.debug, logger.info, logger.warn,)
1818

19-
IS_PYTHON3 = sys.version_info >= (3, 0)
20-
2119
if IS_PYTHON3:
2220
basestring = str
2321

@@ -38,7 +36,7 @@ def __init__(self, nvim):
3836
"""Initialize the legacy python-vim environment."""
3937
self.setup(nvim)
4038
# context where all code will run
41-
self.module = imp.new_module('__main__')
39+
self.module = new_module('__main__')
4240
nvim.script_context = self.module
4341
# it seems some plugins assume 'sys' is already imported, so do it now
4442
exec('import sys', self.module.__dict__)
@@ -205,7 +203,7 @@ def eval(self, expr):
205203
return walk(num_to_str, obj)
206204

207205

208-
# This was copied/adapted from nvim-python help
206+
# Copied/adapted from :help if_pyth.
209207
def path_hook(nvim):
210208
def _get_paths():
211209
if nvim._thread_invalid():
@@ -217,11 +215,11 @@ def _find_module(fullname, oldtail, path):
217215
if idx > 0:
218216
name = oldtail[:idx]
219217
tail = oldtail[idx + 1:]
220-
fmr = imp.find_module(name, path)
221-
module = imp.find_module(fullname[:-len(oldtail)] + name, *fmr)
218+
fmr = find_module(name, path)
219+
module = find_module(fullname[:-len(oldtail)] + name, *fmr)
222220
return _find_module(fullname, tail, module.__path__)
223221
else:
224-
return imp.find_module(fullname, path)
222+
return find_module(fullname, path)
225223

226224
class VimModuleLoader(object):
227225
def __init__(self, module):
@@ -233,7 +231,7 @@ def load_module(self, fullname, path=None):
233231
return sys.modules[fullname]
234232
except KeyError:
235233
pass
236-
return imp.load_module(fullname, *self.module)
234+
return load_module(fullname, *self.module)
237235

238236
class VimPathFinder(object):
239237
@staticmethod

0 commit comments

Comments
 (0)