Skip to content

Commit 33087a2

Browse files
committed
Merge pull request #50 from equalsraf/tb-python3-fixes
python3 fixes for find_module, long, unicode
2 parents 9698cf1 + a93c1a5 commit 33087a2

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

neovim/compat.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,36 @@
11
"""Code for supporting compatibility across python versions."""
22

33
import sys
4+
from imp import find_module as original_find_module
45

56

67
IS_PYTHON3 = sys.version_info >= (3, 0)
8+
9+
10+
if IS_PYTHON3:
11+
def find_module(fullname, path):
12+
"""Compatibility wrapper for imp.find_module.
13+
14+
Automatically decodes arguments of find_module, in Python3
15+
they must be Unicode
16+
"""
17+
if isinstance(fullname, bytes):
18+
fullname = fullname.decode()
19+
if isinstance(path, bytes):
20+
path = path.decode()
21+
elif isinstance(path, list):
22+
newpath = []
23+
for element in path:
24+
if isinstance(element, bytes):
25+
newpath.append(element.decode())
26+
else:
27+
newpath.append(element)
28+
path = newpath
29+
return original_find_module(fullname, path)
30+
31+
# There is no 'long' type in Python3 just int
32+
long = int
33+
else:
34+
find_module = original_find_module
35+
36+
NUM_TYPES = (int, long, float)

neovim/plugins/plugin_host.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
import os
44
import os.path
55
import sys
6-
from imp import find_module, load_module
6+
from imp import load_module
77
from traceback import format_exc
88

9-
from ..compat import IS_PYTHON3
9+
from ..compat import IS_PYTHON3, find_module
1010

1111

1212
logger = logging.getLogger(__name__)

neovim/plugins/script_host.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
import logging
33
import sys
44

5-
from ..api.common import SessionHook
6-
from ..compat import IS_PYTHON3
5+
from ..api.common import SessionHook, DecodeHook
6+
from ..compat import NUM_TYPES, IS_PYTHON3
77

88

99
logger = logging.getLogger(__name__)
@@ -26,6 +26,8 @@ def __init__(self, nvim):
2626
# it seems some plugins assume 'sys' is already imported, so do it now
2727
exec('import sys', self.module.__dict__)
2828
sys.modules['vim'] = nvim.with_hook(LegacyEvalHook())
29+
if IS_PYTHON3:
30+
sys.modules['vim'] = sys.modules['vim'].with_hook(DecodeHook(encoding=nvim.options['encoding'].decode('ascii')))
2931

3032
def python_execute(self, script):
3133
exec(script, self.module.__dict__)
@@ -98,6 +100,6 @@ def __init__(self):
98100
super(LegacyEvalHook, self).__init__(from_nvim=self._string_eval)
99101

100102
def _string_eval(self, obj, session, method, kind):
101-
if method == 'vim_eval' and isinstance(obj, (int, long, float)):
103+
if method == 'vim_eval' and isinstance(obj, NUM_TYPES):
102104
return str(obj)
103105
return obj

0 commit comments

Comments
 (0)