Skip to content

Commit 5406906

Browse files
authored
Merge pull request #129 from dzil123/pickle_protocol_fix
If grammar cache was saved on unsupported pickle protocol, recreate the cache
2 parents d3687f6 + f143e0a commit 5406906

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

gdtoolkit/parser/parser.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,15 @@ def _get_parser(
8484
os.path.join(self._cache_dirpath, version, name) + ".pickle"
8585
)
8686
grammar_filepath: str = os.path.join(self._directory, grammar_filename)
87-
if not os.path.exists(cache_filepath) or not self._use_grammar_cache:
87+
88+
tree = None
89+
if os.path.exists(cache_filepath) and self._use_grammar_cache:
90+
try:
91+
tree = self.load(cache_filepath)
92+
except ValueError:
93+
# pickle errors on unsupported protocols - newer python versions (#93)
94+
pass
95+
if tree is None:
8896
tree = Lark.open(
8997
grammar_filepath,
9098
parser="lalr",
@@ -94,8 +102,7 @@ def _get_parser(
94102
maybe_placeholders=False,
95103
)
96104
self.save(tree, cache_filepath)
97-
else:
98-
tree = self.load(cache_filepath)
105+
99106
return tree
100107

101108
@cached_property
@@ -126,7 +133,7 @@ def save(a_parser: Tree, path: str) -> None:
126133
if not os.path.exists(dirpath):
127134
os.makedirs(dirpath)
128135
with open(path, "wb") as file_parser:
129-
pickle.dump(write_data, file_parser, pickle.HIGHEST_PROTOCOL)
136+
pickle.dump(write_data, file_parser)
130137

131138
@staticmethod
132139
def load(path: str) -> Tree:

0 commit comments

Comments
 (0)