neovim/runtime/autoload/provider/pythonx.vim

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

118 lines
4.2 KiB
VimL
Raw Normal View History

" The Python provider helper
if exists('s:loaded_pythonx_provider')
finish
endif
let s:loaded_pythonx_provider = 1
function! provider#pythonx#Require(host) abort
let ver = (a:host.orig_name ==# 'python') ? 2 : 3
" Python host arguments
let prog = (ver == '2' ? provider#python#Prog() : provider#python3#Prog())
let args = [prog, '-c', 'import sys; sys.path = list(filter(lambda x: x != "", sys.path)); import neovim; neovim.start_host()']
" Collect registered Python plugins into args
let python_plugins = remote#host#PluginsForHost(a:host.name)
for plugin in python_plugins
call add(args, plugin.path)
endfor
return provider#Poll(args, a:host.orig_name, '$NVIM_PYTHON_LOG_FILE', {'overlapped': v:true})
endfunction
2019-01-06 09:19:57 -07:00
function! s:get_python_executable_from_host_var(major_version) abort
return expand(get(g:, 'python'.(a:major_version == 3 ? '3' : '').'_host_prog', ''), v:true)
2019-01-06 09:19:57 -07:00
endfunction
function! s:get_python_candidates(major_version) abort
return {
\ 2: ['python2', 'python2.7', 'python2.6', 'python'],
\ 3: ['python3', 'python3.10', 'python3.9', 'python3.8', 'python3.7',
\ 'python3.6', 'python']
2019-01-06 09:19:57 -07:00
\ }[a:major_version]
endfunction
" Returns [path_to_python_executable, error_message]
function! provider#pythonx#Detect(major_version) abort
return provider#pythonx#DetectByModule('neovim', a:major_version)
endfunction
" Returns [path_to_python_executable, error_message]
function! provider#pythonx#DetectByModule(module, major_version) abort
2019-01-06 09:19:57 -07:00
let python_exe = s:get_python_executable_from_host_var(a:major_version)
if !empty(python_exe)
return [exepath(expand(python_exe, v:true)), '']
endif
2019-01-06 09:19:57 -07:00
let candidates = s:get_python_candidates(a:major_version)
let errors = []
2019-01-06 09:19:57 -07:00
for exe in candidates
let [result, error] = provider#pythonx#CheckForModule(exe, a:module, a:major_version)
if result
2019-01-06 09:19:57 -07:00
return [exe, error]
endif
2019-01-06 09:19:57 -07:00
" Accumulate errors in case we don't find any suitable Python executable.
call add(errors, error)
endfor
2019-01-06 09:19:57 -07:00
" No suitable Python executable found.
return ['', 'provider/pythonx: Could not load Python '.a:major_version.":\n".join(errors, "\n")]
endfunction
" Returns array: [prog_exitcode, prog_version]
function! s:import_module(prog, module) abort
2018-11-18 07:46:55 -07:00
let prog_version = system([a:prog, '-c' , printf(
\ 'import sys; ' .
\ 'sys.path = list(filter(lambda x: x != "", sys.path)); ' .
2018-11-18 07:46:55 -07:00
\ 'sys.stdout.write(str(sys.version_info[0]) + "." + str(sys.version_info[1])); ' .
\ 'import pkgutil; ' .
\ 'exit(2*int(pkgutil.get_loader("%s") is None))',
\ a:module)])
2018-11-18 07:46:55 -07:00
return [v:shell_error, prog_version]
endfunction
" Returns array: [was_success, error_message]
function! provider#pythonx#CheckForModule(prog, module, major_version) abort
let prog_path = exepath(a:prog)
if prog_path ==# ''
return [0, a:prog . ' not found in search path or not executable.']
endif
2018-11-18 07:46:55 -07:00
let min_version = (a:major_version == 2) ? '2.6' : '3.3'
" Try to load module, and output Python version.
2018-11-18 07:46:55 -07:00
" Exit codes:
" 0 module can be loaded.
" 2 module cannot be loaded.
" Otherwise something else went wrong (e.g. 1 or 127).
let [prog_exitcode, prog_version] = s:import_module(a:prog, a:module)
2018-11-18 07:46:55 -07:00
if prog_exitcode == 2 || prog_exitcode == 0
" Check version only for expected return codes.
2018-11-18 07:46:55 -07:00
if prog_version !~ '^' . a:major_version
return [0, prog_path . ' is Python ' . prog_version . ' and cannot provide Python '
\ . a:major_version . '.']
elseif prog_version =~ '^' . a:major_version && str2nr(prog_version[2:]) < str2nr(min_version[2:])
2018-11-18 07:46:55 -07:00
return [0, prog_path . ' is Python ' . prog_version . ' and cannot provide Python >= '
\ . min_version . '.']
endif
endif
2018-11-18 07:46:55 -07:00
if prog_exitcode == 2
return [0, prog_path.' does not have the "' . a:module . '" module. :help provider-python']
2018-11-18 07:46:55 -07:00
elseif prog_exitcode == 127
" This can happen with pyenv's shims.
2018-11-18 07:46:55 -07:00
return [0, prog_path . ' does not exist: ' . prog_version]
elseif prog_exitcode
return [0, 'Checking ' . prog_path . ' caused an unknown error. '
2018-11-18 07:46:55 -07:00
\ . '(' . prog_exitcode . ', output: ' . prog_version . ')'
\ . ' Report this at https://github.com/neovim/neovim']
endif
return [1, '']
endfunction