From 6fef3857740ad80ef1a71409de739d4f1f0c332d Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Thu, 23 Mar 2017 08:52:25 +0100 Subject: [PATCH] Clean the path in pickling. --- jedi/parser/python/__init__.py | 7 ++++++- jedi/parser/utils.py | 5 +++++ test/test_parser/test_pgen2.py | 2 +- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/jedi/parser/python/__init__.py b/jedi/parser/python/__init__.py index 5e936ffe..9dfa7bde 100644 --- a/jedi/parser/python/__init__.py +++ b/jedi/parser/python/__init__.py @@ -45,7 +45,8 @@ def load_grammar(version=None): return load_grammar() -def parse(code, grammar=None, error_recovery=True, start_symbol='file_input'): +def parse(code=None, path=None, grammar=None, error_recovery=True, + start_symbol='file_input', cache=False): """ If you want to parse a Python file you want to start here, most likely. @@ -53,6 +54,7 @@ def parse(code, grammar=None, error_recovery=True, start_symbol='file_input'): other ways to access it. :param code: A unicode string that contains Python code. + :param path: The path to the file you want to open. Only needed for caching. :param grammar: A Python grammar file, created with load_grammar. :param error_recovery: If enabled, any code will be returned. If it is invalid, it will be returned as an error node. If disabled, you will @@ -73,6 +75,9 @@ def parse(code, grammar=None, error_recovery=True, start_symbol='file_input'): if grammar is None: grammar = load_grammar() + if cache and path and not code: + # In this case we do actual caching + path = os.path.expanduser(path) tokens = source_tokens(code, use_exact_op_types=True) kwargs = {} if error_recovery: diff --git a/jedi/parser/utils.py b/jedi/parser/utils.py index 2b9cd374..296c66f5 100644 --- a/jedi/parser/utils.py +++ b/jedi/parser/utils.py @@ -118,6 +118,7 @@ class ParserPickling(object): greater than the original pickling time. In which case the pickled parser is not up to date. """ + path = self._clean_path(path) try: pickle_changed_time = self._index[path] except KeyError: @@ -139,6 +140,7 @@ class ParserPickling(object): return parser_cache_item.parser def save_parser(self, grammar, path, parser_cache_item): + path = self._clean_path(path) self.__index = None try: files = self._index @@ -185,6 +187,9 @@ class ParserPickling(object): shutil.rmtree(self._cache_directory()) self.__index = {} + def _clean_path(self, path): + return os.path.expanduser(path) + def _get_hashed_path(self, grammar, path): file_hash = hashlib.sha256(path.encode("utf-8")).hexdigest() return self._get_path('%s-%s.pkl' % (grammar.sha256, file_hash)) diff --git a/test/test_parser/test_pgen2.py b/test/test_parser/test_pgen2.py index a203a395..62e7851a 100644 --- a/test/test_parser/test_pgen2.py +++ b/test/test_parser/test_pgen2.py @@ -19,7 +19,7 @@ from test.helpers import TestCase def parse(code, version='3.4'): code = dedent(code) + "\n\n" grammar = load_grammar(version=version) - return _parse(code, grammar, error_recovery=False) + return _parse(code, grammar=grammar, error_recovery=False) class TestDriver(TestCase):