2015-03-20 15:55:39 -07:00
|
|
|
" The Python provider helper
|
|
|
|
if exists('s:loaded_pythonx_provider')
|
|
|
|
finish
|
|
|
|
endif
|
|
|
|
|
|
|
|
let s:loaded_pythonx_provider = 1
|
|
|
|
|
2016-03-01 13:06:36 -07:00
|
|
|
function! provider#pythonx#Require(host) abort
|
|
|
|
let ver = (a:host.orig_name ==# 'python') ? 2 : 3
|
|
|
|
|
|
|
|
" Python host arguments
|
2016-05-12 13:25:15 -07:00
|
|
|
let prog = (ver == '2' ? provider#python#Prog() : provider#python3#Prog())
|
2019-10-27 14:27:22 -07:00
|
|
|
let args = [prog, '-c', 'import sys; sys.path = list(filter(lambda x: x != "", sys.path)); import neovim; neovim.start_host()']
|
|
|
|
|
2016-03-01 13:06:36 -07:00
|
|
|
|
|
|
|
" 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
|
|
|
|
|
2020-02-05 00:04:45 -07:00
|
|
|
return provider#Poll(args, a:host.orig_name, '$NVIM_PYTHON_LOG_FILE', {'overlapped': v:true})
|
2016-03-01 13:06:36 -07:00
|
|
|
endfunction
|
|
|
|
|
2019-01-06 09:19:57 -07:00
|
|
|
function! s:get_python_executable_from_host_var(major_version) abort
|
2021-05-07 02:07:07 -07:00
|
|
|
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'],
|
2020-10-08 02:52:17 -07:00
|
|
|
\ 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
|
2019-01-07 06:46:58 -07:00
|
|
|
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)
|
2021-05-07 02:07:07 -07:00
|
|
|
return [exepath(expand(python_exe, v:true)), '']
|
2015-03-20 15:55:39 -07:00
|
|
|
endif
|
|
|
|
|
2019-01-06 09:19:57 -07:00
|
|
|
let candidates = s:get_python_candidates(a:major_version)
|
2015-05-24 10:05:09 -07:00
|
|
|
let errors = []
|
2016-12-09 07:08:18 -07:00
|
|
|
|
2019-01-06 09:19:57 -07:00
|
|
|
for exe in candidates
|
2019-01-07 06:46:58 -07:00
|
|
|
let [result, error] = provider#pythonx#CheckForModule(exe, a:module, a:major_version)
|
2015-05-24 10:05:09 -07:00
|
|
|
if result
|
2019-01-06 09:19:57 -07:00
|
|
|
return [exe, error]
|
2015-03-20 15:55:39 -07:00
|
|
|
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)
|
2015-03-20 15:55:39 -07:00
|
|
|
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")]
|
2015-03-20 15:55:39 -07:00
|
|
|
endfunction
|
|
|
|
|
2018-11-20 01:57:43 -07:00
|
|
|
" 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; ' .
|
2019-10-27 14:27:22 -07:00
|
|
|
\ '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))',
|
2018-11-20 01:57:43 -07:00
|
|
|
\ a:module)])
|
2018-11-18 07:46:55 -07:00
|
|
|
return [v:shell_error, prog_version]
|
|
|
|
endfunction
|
|
|
|
|
2018-11-20 01:57:43 -07:00
|
|
|
" Returns array: [was_success, error_message]
|
|
|
|
function! provider#pythonx#CheckForModule(prog, module, major_version) abort
|
2015-06-05 02:16:52 -07:00
|
|
|
let prog_path = exepath(a:prog)
|
2016-05-28 08:16:24 -07:00
|
|
|
if prog_path ==# ''
|
2015-06-05 02:16:52 -07:00
|
|
|
return [0, a:prog . ' not found in search path or not executable.']
|
2015-03-20 15:55:39 -07:00
|
|
|
endif
|
|
|
|
|
2018-11-18 07:46:55 -07:00
|
|
|
let min_version = (a:major_version == 2) ? '2.6' : '3.3'
|
2015-09-16 14:38:34 -07:00
|
|
|
|
2019-01-01 12:19:42 -07:00
|
|
|
" Try to load module, and output Python version.
|
2018-11-18 07:46:55 -07:00
|
|
|
" Exit codes:
|
2019-01-01 12:19:42 -07:00
|
|
|
" 0 module can be loaded.
|
|
|
|
" 2 module cannot be loaded.
|
2016-05-28 08:07:27 -07:00
|
|
|
" Otherwise something else went wrong (e.g. 1 or 127).
|
2019-01-01 12:19:42 -07:00
|
|
|
let [prog_exitcode, prog_version] = s:import_module(a:prog, a:module)
|
2015-09-16 14:38:34 -07:00
|
|
|
|
2018-11-18 07:46:55 -07:00
|
|
|
if prog_exitcode == 2 || prog_exitcode == 0
|
2016-05-28 08:07:27 -07:00
|
|
|
" 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 . '.']
|
2021-10-07 14:27:40 -07:00
|
|
|
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 >= '
|
2015-09-16 14:38:34 -07:00
|
|
|
\ . min_version . '.']
|
|
|
|
endif
|
2015-06-04 09:43:12 -07:00
|
|
|
endif
|
|
|
|
|
2018-11-18 07:46:55 -07:00
|
|
|
if prog_exitcode == 2
|
2019-01-01 12:19:42 -07:00
|
|
|
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
|
2016-05-28 08:07:27 -07:00
|
|
|
" 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
|
2016-05-28 08:07:27 -07:00
|
|
|
return [0, 'Checking ' . prog_path . ' caused an unknown error. '
|
2018-11-18 07:46:55 -07:00
|
|
|
\ . '(' . prog_exitcode . ', output: ' . prog_version . ')'
|
2017-04-17 16:57:19 -07:00
|
|
|
\ . ' Report this at https://github.com/neovim/neovim']
|
2015-05-02 11:56:09 -07:00
|
|
|
endif
|
2015-03-20 15:55:39 -07:00
|
|
|
|
2015-09-16 14:38:34 -07:00
|
|
|
return [1, '']
|
2015-06-04 09:43:12 -07:00
|
|
|
endfunction
|