Clean the path in pickling.

This commit is contained in:
Dave Halter
2017-03-23 08:52:25 +01:00
parent 26cce4d078
commit 6fef385774
3 changed files with 12 additions and 2 deletions

View File

@@ -45,7 +45,8 @@ def load_grammar(version=None):
return load_grammar() 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. 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. other ways to access it.
:param code: A unicode string that contains Python code. :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 grammar: A Python grammar file, created with load_grammar.
:param error_recovery: If enabled, any code will be returned. If it is :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 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: if grammar is None:
grammar = load_grammar() 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) tokens = source_tokens(code, use_exact_op_types=True)
kwargs = {} kwargs = {}
if error_recovery: if error_recovery:

View File

@@ -118,6 +118,7 @@ class ParserPickling(object):
greater than the original pickling time. In which case the pickled greater than the original pickling time. In which case the pickled
parser is not up to date. parser is not up to date.
""" """
path = self._clean_path(path)
try: try:
pickle_changed_time = self._index[path] pickle_changed_time = self._index[path]
except KeyError: except KeyError:
@@ -139,6 +140,7 @@ class ParserPickling(object):
return parser_cache_item.parser return parser_cache_item.parser
def save_parser(self, grammar, path, parser_cache_item): def save_parser(self, grammar, path, parser_cache_item):
path = self._clean_path(path)
self.__index = None self.__index = None
try: try:
files = self._index files = self._index
@@ -185,6 +187,9 @@ class ParserPickling(object):
shutil.rmtree(self._cache_directory()) shutil.rmtree(self._cache_directory())
self.__index = {} self.__index = {}
def _clean_path(self, path):
return os.path.expanduser(path)
def _get_hashed_path(self, grammar, path): def _get_hashed_path(self, grammar, path):
file_hash = hashlib.sha256(path.encode("utf-8")).hexdigest() file_hash = hashlib.sha256(path.encode("utf-8")).hexdigest()
return self._get_path('%s-%s.pkl' % (grammar.sha256, file_hash)) return self._get_path('%s-%s.pkl' % (grammar.sha256, file_hash))

View File

@@ -19,7 +19,7 @@ from test.helpers import TestCase
def parse(code, version='3.4'): def parse(code, version='3.4'):
code = dedent(code) + "\n\n" code = dedent(code) + "\n\n"
grammar = load_grammar(version=version) grammar = load_grammar(version=version)
return _parse(code, grammar, error_recovery=False) return _parse(code, grammar=grammar, error_recovery=False)
class TestDriver(TestCase): class TestDriver(TestCase):