mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-08 14:54:47 +08:00
Fix interpreter stuff, fix slicing with CompiledObject and a few other things.
This commit is contained in:
@@ -12,6 +12,7 @@ from jedi.parser import load_grammar
|
|||||||
from jedi.parser.fast import FastParser
|
from jedi.parser.fast import FastParser
|
||||||
from jedi.evaluate import helpers
|
from jedi.evaluate import helpers
|
||||||
from jedi.evaluate import iterable
|
from jedi.evaluate import iterable
|
||||||
|
from jedi.evaluate import representation as er
|
||||||
|
|
||||||
|
|
||||||
class InterpreterNamespace(pt.Module):
|
class InterpreterNamespace(pt.Module):
|
||||||
@@ -67,9 +68,6 @@ class LazyName(helpers.FakeName):
|
|||||||
if inspect.ismodule(obj):
|
if inspect.ismodule(obj):
|
||||||
module = obj
|
module = obj
|
||||||
else:
|
else:
|
||||||
class FakeParent(pt.Base):
|
|
||||||
parent = compiled.builtin
|
|
||||||
|
|
||||||
names = []
|
names = []
|
||||||
try:
|
try:
|
||||||
o = obj.__objclass__
|
o = obj.__objclass__
|
||||||
@@ -85,10 +83,12 @@ class LazyName(helpers.FakeName):
|
|||||||
# Unfortunately in some cases like `int` there's no __module__
|
# Unfortunately in some cases like `int` there's no __module__
|
||||||
module = builtins
|
module = builtins
|
||||||
else:
|
else:
|
||||||
|
# TODO this import is wrong. Yields x for x.y.z instead of z
|
||||||
module = __import__(module_name)
|
module = __import__(module_name)
|
||||||
parser_path = [helpers.FakeName(n, FakeParent()) for n in names]
|
parser_path = names
|
||||||
raw_module = get_module(self._value)
|
raw_module = get_module(self._value)
|
||||||
|
|
||||||
|
found = []
|
||||||
try:
|
try:
|
||||||
path = module.__file__
|
path = module.__file__
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
@@ -100,24 +100,27 @@ class LazyName(helpers.FakeName):
|
|||||||
with open(path) as f:
|
with open(path) as f:
|
||||||
source = source_to_unicode(f.read())
|
source = source_to_unicode(f.read())
|
||||||
mod = FastParser(load_grammar(), source, path[:-1]).module
|
mod = FastParser(load_grammar(), source, path[:-1]).module
|
||||||
if not parser_path:
|
if parser_path:
|
||||||
return mod
|
assert len(parser_path) == 1
|
||||||
assert len(parser_path) == 1
|
found = self._evaluator.find_types(mod, parser_path[0], search_global=True)
|
||||||
found = self._evaluator.find_types(mod, parser_path[0], search_global=True)
|
else:
|
||||||
#found = self._evaluator.eval_call_path(iter(parser_path), mod, None)
|
found = [er.wrap(self._evaluator, mod)]
|
||||||
if found:
|
|
||||||
content = iterable.AlreadyEvaluated(found)
|
|
||||||
s = pt.ExprStmt([self, pt.Operator('=', (0, 0), ''), content])
|
|
||||||
s.parent = self._module
|
|
||||||
return s
|
|
||||||
debug.warning('Interpreter lookup for Python code failed %s',
|
|
||||||
mod)
|
|
||||||
|
|
||||||
module = compiled.CompiledObject(raw_module)
|
if not found:
|
||||||
if raw_module == builtins:
|
debug.warning('Possibly an interpreter lookup for Python code failed %s',
|
||||||
# The builtins module is special and always cached.
|
parser_path)
|
||||||
module = compiled.builtin
|
|
||||||
return compiled.create(self._evaluator, self._value, module, module)
|
if not found:
|
||||||
|
evaluated = compiled.CompiledObject(obj)
|
||||||
|
if evaluated == builtins:
|
||||||
|
# The builtins module is special and always cached.
|
||||||
|
evaluated = compiled.builtin
|
||||||
|
found = [evaluated]
|
||||||
|
|
||||||
|
content = iterable.AlreadyEvaluated(found)
|
||||||
|
stmt = pt.ExprStmt([self, pt.Operator('=', (0, 0), ''), content])
|
||||||
|
stmt.parent = self._module
|
||||||
|
return stmt
|
||||||
|
|
||||||
@parent.setter
|
@parent.setter
|
||||||
def parent(self, value):
|
def parent(self, value):
|
||||||
|
|||||||
@@ -340,7 +340,9 @@ class FakeSequence(_FakeArray):
|
|||||||
|
|
||||||
class AlreadyEvaluated(frozenset):
|
class AlreadyEvaluated(frozenset):
|
||||||
"""A simple container to add already evaluated objects to an array."""
|
"""A simple container to add already evaluated objects to an array."""
|
||||||
pass
|
def get_code(self):
|
||||||
|
# For debugging purposes.
|
||||||
|
return str(self)
|
||||||
|
|
||||||
|
|
||||||
class MergedNodes(frozenset):
|
class MergedNodes(frozenset):
|
||||||
@@ -658,7 +660,7 @@ class Slice(object):
|
|||||||
if element is None:
|
if element is None:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
result = precedence.process_precedence_element(self._evaluator, element)
|
result = self._evaluator.eval_element(element)
|
||||||
if len(result) != 1:
|
if len(result) != 1:
|
||||||
# We want slices to be clear defined with just one type.
|
# We want slices to be clear defined with just one type.
|
||||||
# Otherwise we will return an empty slice object.
|
# Otherwise we will return an empty slice object.
|
||||||
@@ -679,13 +681,14 @@ def create_indexes_or_slices(evaluator, index):
|
|||||||
start, stop, step = None, None, None
|
start, stop, step = None, None, None
|
||||||
result = []
|
result = []
|
||||||
for el in index.children:
|
for el in index.children:
|
||||||
if el == ':' and not result:
|
if el == ':':
|
||||||
result.append(None)
|
if not result:
|
||||||
|
result.append(None)
|
||||||
elif pr.is_node(el, 'sliceop'):
|
elif pr.is_node(el, 'sliceop'):
|
||||||
if len(el.children) == 2:
|
if len(el.children) == 2:
|
||||||
result.append(el.children[1])
|
result.append(el.children[1])
|
||||||
else:
|
else:
|
||||||
result.append(result)
|
result.append(el)
|
||||||
result += [None] * (3 - len(result))
|
result += [None] * (3 - len(result))
|
||||||
|
|
||||||
return (Slice(evaluator, *result),)
|
return (Slice(evaluator, *result),)
|
||||||
|
|||||||
Reference in New Issue
Block a user