provider/pythonx: Improve detection code and error messages.

"python -c" returns 1 in case of an error. Use a return code of 2 if
the Neovim module is not found to distinguish these cases.

Verify the interpreter version before checking for an installed Neovim
module. Show a new error message if the Python interpreter version
is below the minimum required version.

Always use "pkgutil" to determine if the Neovim module is installed.
In contrast to "importlib", which was used for Python 3,
"pkgutil.find_loader" is available for all Python versions [1,2].
"pkgutil.find_loader" internally uses "importlib" for Python >= 3.3 [2].
Also, the previously used "importlib.find_loader" is only available
since Python 3.3 (so checking the major Python version was not enough)
and deprecated since Python 3.4 [3].
Finally, conditioning on the major version in Vimscript was incorrect,
as checking the Neovim module for a certain Python major version does
not mean that the tested interpreters are actually of that version.
For example, we test the "python" executable, which is Python 2 on
Ubuntu and Python 3 on Arch Linux.

[1] https://docs.python.org/2/library/pkgutil.html#pkgutil.find_loader
[2] https://docs.python.org/3/library/pkgutil.html#pkgutil.find_loader
[3] https://docs.python.org/3/library/importlib.html#importlib.find_loader
This commit is contained in:
Florian Walch 2015-09-16 23:38:34 +02:00
parent c416e6874e
commit e3540a430b

View File

@ -52,24 +52,38 @@ function! s:check_interpreter(prog, major_ver, skip) abort
return [1, '']
endif
let min_version = (a:major_ver == 2) ? '2.6' : '3.3'
" Try to load neovim module, and output Python version.
" Return codes:
" 0 Neovim module can be loaded.
" 1 Something else went wrong.
" 2 Neovim module cannot be loaded.
let prog_ver = system([ a:prog , '-c' ,
\ 'import sys; sys.path.remove(""); sys.stdout.write(str(sys.version_info[0]) + '.
\ '"." + str(sys.version_info[1])); '.
\ (a:major_ver == 2
\ ? 'import pkgutil; exit(pkgutil.get_loader("neovim") is None)'
\ : 'import importlib; exit(importlib.find_loader("neovim") is None)')
\ 'import sys; ' .
\ 'sys.path.remove(""); ' .
\ 'sys.stdout.write(str(sys.version_info[0]) + "." + str(sys.version_info[1])); ' .
\ 'import pkgutil; ' .
\ 'exit(2*int(pkgutil.get_loader("neovim") is None))'
\ ])
if v:shell_error
if prog_ver
if prog_ver !~ '^' . a:major_ver
return [0, prog_path . ' is Python ' . prog_ver . ' and cannot provide Python '
\ . a:major_ver . '.']
elseif prog_ver =~ '^' . a:major_ver && prog_ver < min_version
return [0, prog_path . ' is Python ' . prog_ver . ' and cannot provide Python >= '
\ . min_version . '.']
endif
endif
if v:shell_error == 1
return [0, 'Checking ' . prog_path . ' caused an unknown error. '
\ . 'Please report this at github.com/neovim/neovim.']
elseif v:shell_error == 2
return [0, prog_path . ' does have not have the neovim module installed. '
\ . 'See ":help nvim-python".']
endif
let min_version = (a:major_ver == 2) ? '2.6' : '3.3'
if prog_ver =~ '^' . a:major_ver && prog_ver >= min_version
return [1, '']
endif
return [0, prog_path . ' is Python ' . prog_ver . ' and cannot provide Python '
\ . a:major_ver . '.']
return [1, '']
endfunction