From a2cf2291dad62b72dda3a75fe156800be3a11b26 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sun, 6 Aug 2017 21:57:10 +0200 Subject: [PATCH] Inline Python init script Fixes https://github.com/davidhalter/jedi-vim/issues/726. --- autoload/jedi.vim | 38 ++++++++++++++++++++++++++++---------- jedi_vim_init.py | 26 -------------------------- 2 files changed, 28 insertions(+), 36 deletions(-) delete mode 100644 jedi_vim_init.py diff --git a/autoload/jedi.vim b/autoload/jedi.vim index 5b0254c..f1f2c11 100644 --- a/autoload/jedi.vim +++ b/autoload/jedi.vim @@ -141,11 +141,9 @@ endfunction let s:python_version = 'null' function! jedi#setup_py_version(py_version) abort if a:py_version == 2 - let cmd_init = 'pyfile' let cmd_exec = 'python' let s:python_version = 2 elseif a:py_version == 3 - let cmd_init = 'py3file' let cmd_exec = 'python3' let s:python_version = 3 else @@ -153,15 +151,35 @@ function! jedi#setup_py_version(py_version) abort endif execute 'command! -nargs=1 PythonJedi '.cmd_exec.' ' - let init_script = s:script_path.'/jedi_vim_init.py' - if !filereadable(init_script) - throw printf('jedi#setup_py_version: init-script is not readable (%s).', init_script) + + let s:init_outcome = 0 + PythonJedi << EOF +try: + import vim + import os, sys + jedi_path = os.path.join(vim.eval('expand(s:script_path)'), 'jedi') + sys.path.insert(0, jedi_path) + + jedi_vim_path = vim.eval('expand(s:script_path)') + if jedi_vim_path not in sys.path: # Might happen when reloading. + sys.path.insert(0, jedi_vim_path) +except Exception as excinfo: + vim.command('let s:init_outcome = "error when adding to sys.path: {0}: {1}"'.format(excinfo.__class__.__name__, excinfo)) +else: + try: + import jedi_vim + except Exception as excinfo: + vim.command('let s:init_outcome = "error when importing jedi_vim: {0}: {1}"'.format(excinfo.__class__.__name__, excinfo)) + else: + vim.command('let s:init_outcome = 1') + finally: + sys.path.remove(jedi_path) +EOF + if !exists('s:init_outcome') + throw 'jedi#setup_py_version: failed to run Python for initialization.' + elseif s:init_outcome isnot 1 + throw printf('jedi#setup_py_version: %s.', s:init_outcome) endif - try - execute cmd_init.' '.init_script - catch - throw 'jedi#setup_py_version: '.v:exception - endtry return 1 endfunction diff --git a/jedi_vim_init.py b/jedi_vim_init.py deleted file mode 100644 index 0636889..0000000 --- a/jedi_vim_init.py +++ /dev/null @@ -1,26 +0,0 @@ -"""Python initialization for jedi module.""" - -try: - import traceback -except Exception as excinfo: - raise Exception('Failed to import traceback: {0}'.format(excinfo)) - -try: - import os, sys, vim - jedi_path = os.path.join(vim.eval('expand(s:script_path)'), 'jedi') - sys.path.insert(0, jedi_path) - - jedi_vim_path = vim.eval('expand(s:script_path)') - if jedi_vim_path not in sys.path: # Might happen when reloading. - sys.path.insert(0, jedi_vim_path) -except Exception as excinfo: - raise Exception('Failed to add to sys.path: {0}\n{1}'.format( - excinfo, traceback.format_exc())) - -try: - import jedi_vim -except Exception as excinfo: - raise Exception('Failed to import jedi_vim: {0}\n{1}'.format( - excinfo, traceback.format_exc())) -finally: - sys.path.remove(jedi_path)