mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-16 10:37:52 +08:00
Save a module instead of a parser when pickling.
This commit is contained in:
@@ -166,7 +166,7 @@ def test_get_line_code():
|
||||
# With before/after
|
||||
line = ' foo'
|
||||
source = 'def foo():\n%s\nother_line' % line
|
||||
assert get_line_code(source, line=2) == line
|
||||
assert get_line_code(source, line=2) == line + '\n'
|
||||
assert get_line_code(source, line=2, after=1) == line + '\nother_line'
|
||||
assert get_line_code(source, line=2, after=1, before=1) == source
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ import pytest
|
||||
|
||||
import jedi
|
||||
from jedi import settings, cache
|
||||
from jedi.parser.utils import ParserCacheItem, ParserPickling
|
||||
from jedi.parser.utils import NodeCacheItem, ParserPickling
|
||||
from jedi.parser.python import load_grammar
|
||||
|
||||
|
||||
@@ -26,39 +26,40 @@ def test_modulepickling_change_cache_dir(monkeypatch, tmpdir):
|
||||
dir_1 = str(tmpdir.mkdir('first'))
|
||||
dir_2 = str(tmpdir.mkdir('second'))
|
||||
|
||||
item_1 = ParserCacheItem('fake parser 1')
|
||||
item_2 = ParserCacheItem('fake parser 2')
|
||||
item_1 = NodeCacheItem('bla', [])
|
||||
item_2 = NodeCacheItem('bla', [])
|
||||
path_1 = 'fake path 1'
|
||||
path_2 = 'fake path 2'
|
||||
|
||||
monkeypatch.setattr(settings, 'cache_directory', dir_1)
|
||||
grammar = load_grammar()
|
||||
ParserPickling.save_parser(grammar, path_1, item_1)
|
||||
ParserPickling.save_item(grammar, path_1, item_1)
|
||||
cached = load_stored_item(grammar, ParserPickling, path_1, item_1)
|
||||
assert cached == item_1.parser
|
||||
assert cached == item_1.node
|
||||
|
||||
monkeypatch.setattr(settings, 'cache_directory', dir_2)
|
||||
ParserPickling.save_parser(grammar, path_2, item_2)
|
||||
ParserPickling.save_item(grammar, path_2, item_2)
|
||||
cached = load_stored_item(grammar, ParserPickling, path_1, item_1)
|
||||
assert cached is None
|
||||
|
||||
|
||||
def load_stored_item(grammar, cache, path, item):
|
||||
"""Load `item` stored at `path` in `cache`."""
|
||||
return cache.load_parser(grammar, path, item.change_time - 1)
|
||||
item = cache.load_item(grammar, path, item.change_time - 1)
|
||||
return item and item.node
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("isolated_jedi_cache")
|
||||
def test_modulepickling_delete_incompatible_cache():
|
||||
item = ParserCacheItem('fake parser')
|
||||
item = NodeCacheItem('fake parser', [])
|
||||
path = 'fake path'
|
||||
|
||||
cache1 = ParserPicklingCls()
|
||||
cache1.version = 1
|
||||
grammar = load_grammar()
|
||||
cache1.save_parser(grammar, path, item)
|
||||
cache1.save_item(grammar, path, item)
|
||||
cached1 = load_stored_item(grammar, cache1, path, item)
|
||||
assert cached1 == item.parser
|
||||
assert cached1 == item.node
|
||||
|
||||
cache2 = ParserPicklingCls()
|
||||
cache2.version = 2
|
||||
@@ -81,15 +82,15 @@ def test_modulepickling_simulate_deleted_cache():
|
||||
|
||||
__ https://developer.apple.com/library/content/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html
|
||||
"""
|
||||
item = ParserCacheItem('fake parser')
|
||||
item = NodeCacheItem('fake parser', [])
|
||||
path = 'fake path'
|
||||
|
||||
cache = ParserPicklingCls()
|
||||
cache.version = 1
|
||||
grammar = load_grammar()
|
||||
cache.save_parser(grammar, path, item)
|
||||
cache.save_item(grammar, path, item)
|
||||
cached1 = load_stored_item(grammar, cache, path, item)
|
||||
assert cached1 == item.parser
|
||||
assert cached1 == item.node
|
||||
|
||||
unlink(cache._get_hashed_path(grammar, path))
|
||||
|
||||
|
||||
@@ -6,10 +6,10 @@ import jedi
|
||||
from jedi import debug
|
||||
from jedi.common import splitlines
|
||||
from jedi import cache
|
||||
from jedi.parser.utils import parser_cache
|
||||
from jedi.parser.python import load_grammar
|
||||
from jedi.parser.python.diff import DiffParser
|
||||
from jedi.parser.python.parser import Parser
|
||||
from jedi.parser.tokenize import source_tokens
|
||||
from jedi.parser.python import parse
|
||||
|
||||
|
||||
def _check_error_leaves_nodes(node):
|
||||
@@ -42,23 +42,24 @@ def _assert_valid_graph(node):
|
||||
|
||||
|
||||
class Differ(object):
|
||||
grammar = load_grammar()
|
||||
|
||||
def initialize(self, code):
|
||||
debug.dbg('differ: initialize', color='YELLOW')
|
||||
grammar = load_grammar()
|
||||
self.parser = Parser(grammar, code, error_recovery=True)
|
||||
tokens = source_tokens(self.parser.new_code, use_exact_op_types=True)
|
||||
return self.parser.parse(tokens)
|
||||
self.lines = splitlines(code, keepends=True)
|
||||
parser_cache.pop(None, None)
|
||||
self.module = parse(code, diff_cache=True, cache=True)
|
||||
return self.module
|
||||
|
||||
def parse(self, source, copies=0, parsers=0, expect_error_leaves=False):
|
||||
def parse(self, code, copies=0, parsers=0, expect_error_leaves=False):
|
||||
debug.dbg('differ: parse copies=%s parsers=%s', copies, parsers, color='YELLOW')
|
||||
lines = splitlines(source, keepends=True)
|
||||
diff_parser = DiffParser(self.parser)
|
||||
new_module = diff_parser.update(lines)
|
||||
assert source == new_module.get_code()
|
||||
lines = splitlines(code, keepends=True)
|
||||
diff_parser = DiffParser(self.grammar, self.module)
|
||||
new_module = diff_parser.update(self.lines, lines)
|
||||
self.lines = lines
|
||||
assert code == new_module.get_code()
|
||||
assert diff_parser._copy_count == copies
|
||||
assert diff_parser._parser_count == parsers
|
||||
self.parser.module = new_module
|
||||
self.parser._parsed = new_module
|
||||
|
||||
assert expect_error_leaves == _check_error_leaves_nodes(new_module)
|
||||
_assert_valid_graph(new_module)
|
||||
|
||||
Reference in New Issue
Block a user