mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-15 18:17:07 +08:00
Make the new project API fully work in tests
This commit is contained in:
@@ -121,7 +121,7 @@ class Script(object):
|
|||||||
project = get_default_project()
|
project = get_default_project()
|
||||||
# TODO deprecate and remove sys_path from the Script API.
|
# TODO deprecate and remove sys_path from the Script API.
|
||||||
project._sys_path = sys_path
|
project._sys_path = sys_path
|
||||||
self._evaluator = Evaluator(project, environment)
|
self._evaluator = Evaluator(project, environment=environment, script_path=path)
|
||||||
self._project = project
|
self._project = project
|
||||||
debug.speed('init')
|
debug.speed('init')
|
||||||
|
|
||||||
|
|||||||
@@ -87,12 +87,12 @@ class Project(object):
|
|||||||
one is used like a public method.
|
one is used like a public method.
|
||||||
"""
|
"""
|
||||||
sys_path = list(self._get_base_sys_path(environment))
|
sys_path = list(self._get_base_sys_path(environment))
|
||||||
if self._smart_sys_path:
|
if evaluator.script_path is None or not self._smart_sys_path:
|
||||||
return sys_path
|
return sys_path
|
||||||
|
|
||||||
added_paths = map(
|
added_paths = map(
|
||||||
force_unicode,
|
force_unicode,
|
||||||
detect_additional_paths(self._evaluator, self._script_path)
|
detect_additional_paths(evaluator, evaluator.script_path)
|
||||||
)
|
)
|
||||||
return sys_path + list(added_paths)
|
return sys_path + list(added_paths)
|
||||||
|
|
||||||
|
|||||||
@@ -67,7 +67,6 @@ import parso
|
|||||||
|
|
||||||
from jedi import debug
|
from jedi import debug
|
||||||
from jedi import parser_utils
|
from jedi import parser_utils
|
||||||
from jedi.api.environment import get_default_environment
|
|
||||||
from jedi.evaluate.utils import unite
|
from jedi.evaluate.utils import unite
|
||||||
from jedi.evaluate import imports
|
from jedi.evaluate import imports
|
||||||
from jedi.evaluate import recursion
|
from jedi.evaluate import recursion
|
||||||
@@ -85,10 +84,11 @@ from jedi.evaluate.syntax_tree import eval_trailer, eval_expr_stmt, \
|
|||||||
|
|
||||||
|
|
||||||
class Evaluator(object):
|
class Evaluator(object):
|
||||||
def __init__(self, project, environment=None):
|
def __init__(self, project, environment=None, script_path=None):
|
||||||
if environment is None:
|
if environment is None:
|
||||||
environment = get_default_environment()
|
environment = project.get_environment()
|
||||||
self.environment = environment
|
self.environment = environment
|
||||||
|
self.script_path = script_path
|
||||||
self.compiled_subprocess = environment.get_evaluator_subprocess(self)
|
self.compiled_subprocess = environment.get_evaluator_subprocess(self)
|
||||||
self.grammar = environment.get_grammar()
|
self.grammar = environment.get_grammar()
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import subprocess
|
|||||||
import socket
|
import socket
|
||||||
import errno
|
import errno
|
||||||
import weakref
|
import weakref
|
||||||
|
import traceback
|
||||||
from functools import partial
|
from functools import partial
|
||||||
|
|
||||||
from jedi._compatibility import queue, is_py3, force_unicode, pickle_dump, pickle_load
|
from jedi._compatibility import queue, is_py3, force_unicode, pickle_dump, pickle_load
|
||||||
@@ -195,12 +196,15 @@ class _CompiledSubprocess(object):
|
|||||||
raise InternalError("The subprocess was killed. Maybe out of memory?")
|
raise InternalError("The subprocess was killed. Maybe out of memory?")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
is_exception, result = pickle_load(self._process.stdout)
|
is_exception, traceback, result = pickle_load(self._process.stdout)
|
||||||
except EOFError:
|
except EOFError:
|
||||||
self.kill()
|
self.kill()
|
||||||
raise InternalError("The subprocess crashed.")
|
raise InternalError("The subprocess crashed.")
|
||||||
|
|
||||||
if is_exception:
|
if is_exception:
|
||||||
|
# Replace the attribute error message with a the traceback. It's
|
||||||
|
# way more informative.
|
||||||
|
result.args = (traceback,)
|
||||||
raise result
|
raise result
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@@ -270,11 +274,9 @@ class Listener(object):
|
|||||||
# here and just exit.
|
# here and just exit.
|
||||||
exit(1)
|
exit(1)
|
||||||
try:
|
try:
|
||||||
result = False, self._run(*payload)
|
result = False, None, self._run(*payload)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
#import traceback
|
result = True, traceback.format_exc(), e
|
||||||
#print_to_stderr(traceback.format_exc())
|
|
||||||
result = True, e
|
|
||||||
|
|
||||||
pickle_dump(result, file=stdout)
|
pickle_dump(result, file=stdout)
|
||||||
stdout.flush()
|
stdout.flush()
|
||||||
|
|||||||
@@ -517,10 +517,11 @@ def get_modules_containing_name(evaluator, modules, name):
|
|||||||
with open(path, 'rb') as f:
|
with open(path, 'rb') as f:
|
||||||
code = python_bytes_to_unicode(f.read(), errors='replace')
|
code = python_bytes_to_unicode(f.read(), errors='replace')
|
||||||
if name in code:
|
if name in code:
|
||||||
module = _load_module(evaluator, path, code)
|
e_sys_path = evaluator.get_sys_path()
|
||||||
|
module = _load_module(evaluator, path, code, sys_path=e_sys_path)
|
||||||
|
|
||||||
module_name = sys_path.dotted_path_in_sys_path(
|
module_name = sys_path.dotted_path_in_sys_path(
|
||||||
evaluator.get_sys_path(), path
|
e_sys_path, path
|
||||||
)
|
)
|
||||||
if module_name is not None:
|
if module_name is not None:
|
||||||
add_module(evaluator, module_name, module)
|
add_module(evaluator, module_name, module)
|
||||||
|
|||||||
@@ -43,7 +43,10 @@ def test_versions(version):
|
|||||||
|
|
||||||
|
|
||||||
def test_load_module(evaluator):
|
def test_load_module(evaluator):
|
||||||
access_path = evaluator.compiled_subprocess.load_module(name=u'math')
|
access_path = evaluator.compiled_subprocess.load_module(
|
||||||
|
name=u'math',
|
||||||
|
sys_path=evaluator.get_sys_path()
|
||||||
|
)
|
||||||
name, access_handle = access_path.accesses[0]
|
name, access_handle = access_path.accesses[0]
|
||||||
|
|
||||||
assert access_handle.py__bool__() is True
|
assert access_handle.py__bool__() is True
|
||||||
|
|||||||
Reference in New Issue
Block a user