diff --git a/lark/lark.py b/lark/lark.py index 0df60f4..2e0b101 100644 --- a/lark/lark.py +++ b/lark/lark.py @@ -23,10 +23,6 @@ try: import regex except ImportError: regex = None -try: - import atomicwrites -except ImportError: - atomicwrites = None ###{standalone @@ -283,19 +279,20 @@ class Lark(Serialize): for name in (set(options) - _LOAD_ALLOWED_OPTIONS): del options[name] with FS.open(cache_fn, 'rb') as f: - file_md5 = f.readline().rstrip(b'\n') - if file_md5 == cache_md5.encode('utf8') and verify_used_files(pickle.load(f)): - old_options = self.options - try: - self._load(f, **options) - except Exception: # We should probably narrow done which errors we catch here. - logger.exception("Failed to load Lark from cache: %r. We will try to carry on." % cache_fn) - - # In theory, the Lark instance might have been messed up by the call to `_load`. - # In practice the only relevant thing that might have been overriden should be `options` - self.options = old_options - else: + old_options = self.options + try: + file_md5 = f.readline().rstrip(b'\n') + cached_used_files = pickle.load(f) + if file_md5 == cache_md5.encode('utf8') and verify_used_files(cached_used_files): + cached_parser_data = pickle.load(f) + self._load(cached_parser_data, **options) return + except Exception: # We should probably narrow done which errors we catch here. + logger.exception("Failed to load Lark from cache: %r. We will try to carry on." % cache_fn) + + # In theory, the Lark instance might have been messed up by the call to `_load`. + # In practice the only relevant thing that might have been overriden should be `options` + self.options = old_options # Parse the grammar file and compose the grammars diff --git a/lark/utils.py b/lark/utils.py index c9bdf88..04fe713 100644 --- a/lark/utils.py +++ b/lark/utils.py @@ -294,7 +294,7 @@ class FS: @staticmethod def open(name, mode="r", **kwargs): if atomicwrites and "w" in mode: - return atomicwrites.atomic_write(name, mode=mode, override=True, **kwargs) + return atomicwrites.atomic_write(name, mode=mode, overwrite=True, **kwargs) else: return open(name, mode, **kwargs) diff --git a/setup.py b/setup.py index 0dc3784..f23eb0b 100644 --- a/setup.py +++ b/setup.py @@ -17,7 +17,7 @@ setup( extras_require = { "regex": ["regex"], "nearley": ["js2py"], - "atomicwrites": ["atomicwrites"], + "atomic_cache": ["atomicwrites"], }, package_data = {'': ['*.md', '*.lark'], 'lark-stubs': ['*.pyi']},