mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-09 23:34:45 +08:00
sys path modifications working not only on imports, but on everything
This commit is contained in:
@@ -179,17 +179,18 @@ class ImportPath(object):
|
|||||||
debug.dbg('search_module', string, self.file_path)
|
debug.dbg('search_module', string, self.file_path)
|
||||||
# Override the sys.path. It works only good that way.
|
# Override the sys.path. It works only good that way.
|
||||||
# Injecting the path directly into `find_module` did not work.
|
# Injecting the path directly into `find_module` did not work.
|
||||||
sys.path, temp = builtin.module_find_path, sys.path
|
sys.path, temp = sys_path_mod, sys.path
|
||||||
try:
|
try:
|
||||||
i = imp.find_module(string)
|
i = imp.find_module(string)
|
||||||
except:
|
except ImportError:
|
||||||
sys.path = temp
|
sys.path = temp
|
||||||
raise
|
raise
|
||||||
sys.path = temp
|
sys.path = temp
|
||||||
return i
|
return i
|
||||||
|
|
||||||
|
sys_path_mod = self.sys_path_with_modifications()
|
||||||
current_namespace = None
|
current_namespace = None
|
||||||
builtin.module_find_path.insert(0, self.file_path)
|
sys_path_mod.insert(0, self.file_path)
|
||||||
# now execute those paths
|
# now execute those paths
|
||||||
rest = []
|
rest = []
|
||||||
for i, s in enumerate(self.import_path):
|
for i, s in enumerate(self.import_path):
|
||||||
@@ -202,7 +203,7 @@ class ImportPath(object):
|
|||||||
raise ModuleNotFound(
|
raise ModuleNotFound(
|
||||||
'The module you searched has not been found')
|
'The module you searched has not been found')
|
||||||
|
|
||||||
builtin.module_find_path.pop(0)
|
sys_path_mod.pop(0)
|
||||||
path = current_namespace[1]
|
path = current_namespace[1]
|
||||||
is_package_directory = current_namespace[2][2] == imp.PKG_DIRECTORY
|
is_package_directory = current_namespace[2][2] == imp.PKG_DIRECTORY
|
||||||
|
|
||||||
|
|||||||
28
modules.py
28
modules.py
@@ -181,27 +181,24 @@ class ModuleWithCursor(Module):
|
|||||||
def sys_path_with_modifications(module):
|
def sys_path_with_modifications(module):
|
||||||
def execute_code(code):
|
def execute_code(code):
|
||||||
c = "import os; from os.path import *; result=%s"
|
c = "import os; from os.path import *; result=%s"
|
||||||
variables = {}
|
variables = {'__file__': module.path}
|
||||||
try:
|
try:
|
||||||
exec_function(c % code, variables)
|
exec_function(c % code, variables)
|
||||||
except Exception:
|
except Exception:
|
||||||
|
debug.warning('sys path detected, but failed to evaluate')
|
||||||
return None
|
return None
|
||||||
try:
|
try:
|
||||||
return os.path.abspath(variables['result'])
|
return os.path.abspath(variables['result'])
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
curdir = os.path.abspath(os.curdir)
|
def check_module(module):
|
||||||
try:
|
|
||||||
os.chdir(os.path.dirname(module.path))
|
|
||||||
except OSError:
|
|
||||||
pass
|
|
||||||
sys_path = list(builtin.module_find_path) # copy
|
|
||||||
try:
|
try:
|
||||||
possible_stmts = module.used_names['path']
|
possible_stmts = module.used_names['path']
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return []
|
return builtin.module_find_path
|
||||||
|
|
||||||
|
sys_path = list(builtin.module_find_path) # copy
|
||||||
for p in possible_stmts:
|
for p in possible_stmts:
|
||||||
try:
|
try:
|
||||||
call = p.get_assignment_calls().get_only_subelement()
|
call = p.get_assignment_calls().get_only_subelement()
|
||||||
@@ -224,15 +221,24 @@ def sys_path_with_modifications(module):
|
|||||||
exe_type, exe.type = exe.type, parsing.Array.NOARRAY
|
exe_type, exe.type = exe.type, parsing.Array.NOARRAY
|
||||||
exe_pop = exe.values.pop(0)
|
exe_pop = exe.values.pop(0)
|
||||||
res = execute_code(exe.get_code())
|
res = execute_code(exe.get_code())
|
||||||
if res:
|
if res is not None:
|
||||||
sys_path.insert(0, res)
|
sys_path.insert(0, res)
|
||||||
exe.type = exe_type
|
exe.type = exe_type
|
||||||
exe.values.insert(0, exe_pop)
|
exe.values.insert(0, exe_pop)
|
||||||
elif array_cmd == 'append':
|
elif array_cmd == 'append':
|
||||||
res = execute_code(exe.get_code())
|
res = execute_code(exe.get_code())
|
||||||
if res:
|
if res is not None:
|
||||||
sys_path.append(res)
|
sys_path.append(res)
|
||||||
|
return sys_path
|
||||||
|
|
||||||
|
curdir = os.path.abspath(os.curdir)
|
||||||
|
try:
|
||||||
|
os.chdir(os.path.dirname(module.path))
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
result = check_module(module)
|
||||||
|
|
||||||
# cleanup, back to old directory
|
# cleanup, back to old directory
|
||||||
os.chdir(curdir)
|
os.chdir(curdir)
|
||||||
return sys_path
|
return result
|
||||||
|
|||||||
@@ -18,5 +18,8 @@ import evaluate
|
|||||||
#? ['goto']
|
#? ['goto']
|
||||||
evaluate.goto
|
evaluate.goto
|
||||||
|
|
||||||
#? ['pylab_']
|
#? ['jedi_']
|
||||||
import pylab_
|
import jedi_
|
||||||
|
|
||||||
|
#? ['el']
|
||||||
|
jedi_.el
|
||||||
|
|||||||
@@ -3,9 +3,10 @@ import os
|
|||||||
import sys
|
import sys
|
||||||
import re
|
import re
|
||||||
import traceback
|
import traceback
|
||||||
|
from os.path import abspath, dirname
|
||||||
|
|
||||||
os.chdir(os.path.dirname(os.path.abspath(__file__)) + '/..')
|
sys.path.append(abspath(dirname(abspath(__file__)) + '/..'))
|
||||||
sys.path.append('.')
|
os.chdir(dirname(abspath(__file__)) + '/..')
|
||||||
|
|
||||||
from _compatibility import unicode, BytesIO, reduce, literal_eval, is_py25
|
from _compatibility import unicode, BytesIO, reduce, literal_eval, is_py25
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user