mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-07 22:44:27 +08:00
Remove the module attribute from the parser.
This commit is contained in:
@@ -136,7 +136,7 @@ class Script(object):
|
|||||||
parser = FastParser(self._grammar, self._source, self.path)
|
parser = FastParser(self._grammar, self._source, self.path)
|
||||||
save_parser(self.path, parser, pickling=False)
|
save_parser(self.path, parser, pickling=False)
|
||||||
|
|
||||||
return parser.module
|
return parser.get_root_node()
|
||||||
|
|
||||||
@cache.memoize_method
|
@cache.memoize_method
|
||||||
def _get_module(self):
|
def _get_module(self):
|
||||||
|
|||||||
@@ -123,7 +123,7 @@ def parse(grammar, path):
|
|||||||
|
|
||||||
|
|
||||||
def _load_module(evaluator, path, python_object):
|
def _load_module(evaluator, path, python_object):
|
||||||
module = parse(evaluator.grammar, path).module
|
module = parse(evaluator.grammar, path).get_root_node()
|
||||||
python_module = inspect.getmodule(python_object)
|
python_module = inspect.getmodule(python_object)
|
||||||
|
|
||||||
evaluator.modules[python_module.__name__] = module
|
evaluator.modules[python_module.__name__] = module
|
||||||
|
|||||||
@@ -461,13 +461,13 @@ def _load_module(evaluator, path=None, source=None, sys_path=None, parent_module
|
|||||||
p = path
|
p = path
|
||||||
p = FastParser(evaluator.grammar, source_to_unicode(source), p)
|
p = FastParser(evaluator.grammar, source_to_unicode(source), p)
|
||||||
save_parser(path, p)
|
save_parser(path, p)
|
||||||
return p.module
|
return p.get_root_node()
|
||||||
|
|
||||||
if sys_path is None:
|
if sys_path is None:
|
||||||
sys_path = evaluator.sys_path
|
sys_path = evaluator.sys_path
|
||||||
|
|
||||||
cached = load_parser(path)
|
cached = load_parser(path)
|
||||||
module_node = load(source) if cached is None else cached.module
|
module_node = load(source) if cached is None else cached.get_root_node()
|
||||||
if isinstance(module_node, compiled.CompiledObject):
|
if isinstance(module_node, compiled.CompiledObject):
|
||||||
return module_node
|
return module_node
|
||||||
|
|
||||||
@@ -499,7 +499,8 @@ def get_modules_containing_name(evaluator, modules, name):
|
|||||||
except IOError:
|
except IOError:
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
return er.ModuleContext(evaluator, parser_cache_item.parser.module)
|
module_node = parser_cache_item.parser.get_root_node()
|
||||||
|
return er.ModuleContext(evaluator, module_node)
|
||||||
|
|
||||||
def check_fs(path):
|
def check_fs(path):
|
||||||
with open(path, 'rb') as f:
|
with open(path, 'rb') as f:
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ from jedi.evaluate import representation as er
|
|||||||
from jedi.evaluate.instance import InstanceFunctionExecution, \
|
from jedi.evaluate.instance import InstanceFunctionExecution, \
|
||||||
AbstractInstanceContext, CompiledInstance, BoundMethod
|
AbstractInstanceContext, CompiledInstance, BoundMethod
|
||||||
from jedi.evaluate import iterable
|
from jedi.evaluate import iterable
|
||||||
from jedi.parser import ParserWithRecovery
|
from jedi.parser.python import parse
|
||||||
from jedi import debug
|
from jedi import debug
|
||||||
from jedi.evaluate import precedence
|
from jedi.evaluate import precedence
|
||||||
from jedi.evaluate import param
|
from jedi.evaluate import param
|
||||||
@@ -265,7 +265,7 @@ def collections_namedtuple(evaluator, obj, arguments):
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Parse source
|
# Parse source
|
||||||
generated_class = ParserWithRecovery(evaluator.grammar, unicode(source)).module.subscopes[0]
|
generated_class = parse(source, grammar=evaluator.grammar).subscopes[0]
|
||||||
return set([er.ClassContext(evaluator, generated_class, evaluator.BUILTINS)])
|
return set([er.ClassContext(evaluator, generated_class, evaluator.BUILTINS)])
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -222,7 +222,7 @@ def _get_paths_from_buildout_script(evaluator, buildout_script):
|
|||||||
|
|
||||||
p = ParserWithRecovery(evaluator.grammar, source, buildout_script)
|
p = ParserWithRecovery(evaluator.grammar, source, buildout_script)
|
||||||
save_parser(buildout_script, p)
|
save_parser(buildout_script, p)
|
||||||
return p.module
|
return p.get_root_node()
|
||||||
|
|
||||||
cached = load_parser(buildout_script)
|
cached = load_parser(buildout_script)
|
||||||
module_node = cached and cached.module or load(buildout_script)
|
module_node = cached and cached.module or load(buildout_script)
|
||||||
|
|||||||
@@ -197,7 +197,6 @@ class ParserWithRecovery(Parser):
|
|||||||
def parse(self, tokenizer):
|
def parse(self, tokenizer):
|
||||||
root_node = super(ParserWithRecovery, self).parse(self._tokenize(tokenizer))
|
root_node = super(ParserWithRecovery, self).parse(self._tokenize(tokenizer))
|
||||||
root_node.path = self._module_path
|
root_node.path = self._module_path
|
||||||
self.module = root_node
|
|
||||||
return root_node
|
return root_node
|
||||||
|
|
||||||
def error_recovery(self, grammar, stack, arcs, typ, value, start_pos, prefix,
|
def error_recovery(self, grammar, stack, arcs, typ, value, start_pos, prefix,
|
||||||
|
|||||||
@@ -16,14 +16,14 @@ The easiest way to play with this module is to use :class:`parsing.Parser`.
|
|||||||
>>> from jedi.parser.python import load_grammar
|
>>> from jedi.parser.python import load_grammar
|
||||||
>>> from jedi.parser import ParserWithRecovery
|
>>> from jedi.parser import ParserWithRecovery
|
||||||
>>> parser = ParserWithRecovery(load_grammar(), u('import os'), 'example.py')
|
>>> parser = ParserWithRecovery(load_grammar(), u('import os'), 'example.py')
|
||||||
>>> submodule = parser.module
|
>>> module = parser.get_root_node()
|
||||||
>>> submodule
|
>>> module
|
||||||
<Module: example.py@1-1>
|
<Module: example.py@1-1>
|
||||||
|
|
||||||
Any subclasses of :class:`Scope`, including :class:`Module` has an attribute
|
Any subclasses of :class:`Scope`, including :class:`Module` has an attribute
|
||||||
:attr:`imports <Scope.imports>`:
|
:attr:`imports <Scope.imports>`:
|
||||||
|
|
||||||
>>> submodule.imports
|
>>> module.imports
|
||||||
[<ImportName: import os@1,0>]
|
[<ImportName: import os@1,0>]
|
||||||
|
|
||||||
See also :attr:`Scope.subscopes` and :attr:`Scope.statements`.
|
See also :attr:`Scope.subscopes` and :attr:`Scope.statements`.
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ def test_sys_path_with_modifications():
|
|||||||
path = os.path.abspath(os.path.join(os.curdir, 'module_name.py'))
|
path = os.path.abspath(os.path.join(os.curdir, 'module_name.py'))
|
||||||
grammar = load_grammar()
|
grammar = load_grammar()
|
||||||
p = ParserWithRecovery(grammar, code, module_path=path)
|
p = ParserWithRecovery(grammar, code, module_path=path)
|
||||||
module_context = ModuleContext(Evaluator(grammar), p.module)
|
module_context = ModuleContext(Evaluator(grammar), p.get_root_node())
|
||||||
paths = sys_path_with_modifications(module_context.evaluator, module_context)
|
paths = sys_path_with_modifications(module_context.evaluator, module_context)
|
||||||
assert '/tmp/.buildout/eggs/important_package.egg' in paths
|
assert '/tmp/.buildout/eggs/important_package.egg' in paths
|
||||||
|
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ class Differ(object):
|
|||||||
debug.dbg('differ: initialize', color='YELLOW')
|
debug.dbg('differ: initialize', color='YELLOW')
|
||||||
grammar = load_grammar()
|
grammar = load_grammar()
|
||||||
self.parser = ParserWithRecovery(grammar, source)
|
self.parser = ParserWithRecovery(grammar, source)
|
||||||
return self.parser.module
|
return self.parser.get_root_node()
|
||||||
|
|
||||||
def parse(self, source, copies=0, parsers=0, expect_error_leaves=False):
|
def parse(self, source, copies=0, parsers=0, expect_error_leaves=False):
|
||||||
debug.dbg('differ: parse copies=%s parsers=%s', copies, parsers, color='YELLOW')
|
debug.dbg('differ: parse copies=%s parsers=%s', copies, parsers, color='YELLOW')
|
||||||
|
|||||||
@@ -27,7 +27,8 @@ def test_carriage_return_splitting():
|
|||||||
'''))
|
'''))
|
||||||
source = source.replace('\n', '\r\n')
|
source = source.replace('\n', '\r\n')
|
||||||
p = FastParser(load_grammar(), source)
|
p = FastParser(load_grammar(), source)
|
||||||
assert [n.value for lst in p.module.used_names.values() for n in lst] == ['Foo']
|
module = p.get_root_node()
|
||||||
|
assert [n.value for lst in module.used_names.values() for n in lst] == ['Foo']
|
||||||
|
|
||||||
|
|
||||||
def test_class_in_docstr():
|
def test_class_in_docstr():
|
||||||
@@ -48,8 +49,8 @@ def check_p(src, number_parsers_used, number_of_splits=None, number_of_misses=0)
|
|||||||
p = FastParser(load_grammar(), u(src))
|
p = FastParser(load_grammar(), u(src))
|
||||||
save_parser(None, p, pickling=False)
|
save_parser(None, p, pickling=False)
|
||||||
|
|
||||||
assert src == p.module.get_code()
|
assert src == p.get_root_node().get_code()
|
||||||
return p.module
|
return p.get_root_node()
|
||||||
|
|
||||||
|
|
||||||
def test_if():
|
def test_if():
|
||||||
|
|||||||
@@ -90,13 +90,13 @@ class TestImports():
|
|||||||
|
|
||||||
|
|
||||||
def test_module():
|
def test_module():
|
||||||
module = ParserWithRecovery(load_grammar(), u('asdf'), 'example.py').module
|
module = ParserWithRecovery(load_grammar(), u('asdf'), 'example.py').get_root_node()
|
||||||
name = module.name
|
name = module.name
|
||||||
assert str(name) == 'example'
|
assert str(name) == 'example'
|
||||||
assert name.start_pos == (1, 0)
|
assert name.start_pos == (1, 0)
|
||||||
assert name.end_pos == (1, 7)
|
assert name.end_pos == (1, 7)
|
||||||
|
|
||||||
module = ParserWithRecovery(load_grammar(), u('asdf')).module
|
module = ParserWithRecovery(load_grammar(), u('asdf')).get_root_node()
|
||||||
name = module.name
|
name = module.name
|
||||||
assert str(name) == ''
|
assert str(name) == ''
|
||||||
assert name.start_pos == (1, 0)
|
assert name.start_pos == (1, 0)
|
||||||
@@ -190,7 +190,7 @@ def test_param_splitting():
|
|||||||
def check(src, result):
|
def check(src, result):
|
||||||
# Python 2 tuple params should be ignored for now.
|
# Python 2 tuple params should be ignored for now.
|
||||||
grammar = load_grammar('%s.%s' % sys.version_info[:2])
|
grammar = load_grammar('%s.%s' % sys.version_info[:2])
|
||||||
m = ParserWithRecovery(grammar, u(src)).module
|
m = ParserWithRecovery(grammar, u(src)).get_root_node()
|
||||||
if is_py3:
|
if is_py3:
|
||||||
assert not m.subscopes
|
assert not m.subscopes
|
||||||
else:
|
else:
|
||||||
|
|||||||
Reference in New Issue
Block a user