Browse Source

Removed safe_cache option

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.11.3
MegaIng1 4 years ago
parent
commit
2eb5c746ff
2 changed files with 14 additions and 28 deletions
  1. +0
    -2
      lark-stubs/lark.pyi
  2. +14
    -26
      lark/lark.py

+ 0
- 2
lark-stubs/lark.pyi View File

@@ -38,7 +38,6 @@ class LarkOptions:
use_bytes: bool use_bytes: bool
import_paths: List[Union[str, Callable[[Union[None, str, PackageResource], str], Tuple[str, str]]]] import_paths: List[Union[str, Callable[[Union[None, str, PackageResource], str], Tuple[str, str]]]]
source_path: Optional[str] source_path: Optional[str]
safe_cache: Literal[False, True, "atomic"]




class PackageResource(object): class PackageResource(object):
@@ -82,7 +81,6 @@ class Lark:
use_bytes: bool = False, use_bytes: bool = False,
import_paths: List[Union[str, Callable[[Union[None, str, PackageResource], str], Tuple[str, str]]]] = ..., import_paths: List[Union[str, Callable[[Union[None, str, PackageResource], str], Tuple[str, str]]]] = ...,
source_path: Optional[str]=None, source_path: Optional[str]=None,
safe_cache: Literal[False, True, "atomic"]=True,
): ):
... ...




+ 14
- 26
lark/lark.py View File

@@ -105,11 +105,6 @@ class LarkOptions(Serialize):
A List of either paths or loader functions to specify from where grammars are imported A List of either paths or loader functions to specify from where grammars are imported
source_path source_path
Override the source of from where the grammar was loaded. Useful for relative imports and unconventional grammar loading Override the source of from where the grammar was loaded. Useful for relative imports and unconventional grammar loading
safe_cache
Controls how exactly the cache is saved & verified
- False: simple read/write, no extend file checking
- True: use atomicwrites if available and check if any of the imported files were modified
- "atomic": same as True, but require atomicwrites to be installed
**=== End Options ===** **=== End Options ===**
""" """
if __doc__: if __doc__:
@@ -145,7 +140,6 @@ class LarkOptions(Serialize):
'use_bytes': False, 'use_bytes': False,
'import_paths': [], 'import_paths': [],
'source_path': None, 'source_path': None,
'safe_cache': True,
} }


def __init__(self, options_dict): def __init__(self, options_dict):
@@ -155,7 +149,7 @@ class LarkOptions(Serialize):
for name, default in self._defaults.items(): for name, default in self._defaults.items():
if name in o: if name in o:
value = o.pop(name) value = o.pop(name)
if isinstance(default, bool) and name not in ('cache', 'use_bytes', 'safe_cache'):
if isinstance(default, bool) and name not in ('cache', 'use_bytes'):
value = bool(value) value = bool(value)
else: else:
value = default value = default
@@ -268,10 +262,6 @@ class Lark(Serialize):
if self.options.cache: if self.options.cache:
if self.options.parser != 'lalr': if self.options.parser != 'lalr':
raise ConfigurationError("cache only works with parser='lalr' for now") raise ConfigurationError("cache only works with parser='lalr' for now")
if self.options.safe_cache == "atomic":
if not atomicwrites:
raise ConfigurationError("safe_cache='atomic' requires atomicwrites to be installed")


unhashable = ('transformer', 'postlex', 'lexer_callbacks', 'edit_terminals') unhashable = ('transformer', 'postlex', 'lexer_callbacks', 'edit_terminals')
options_str = ''.join(k+str(v) for k, v in options.items() if k not in unhashable) options_str = ''.join(k+str(v) for k, v in options.items() if k not in unhashable)
@@ -294,19 +284,18 @@ class Lark(Serialize):
del options[name] del options[name]
with FS.open(cache_fn, 'rb') as f: with FS.open(cache_fn, 'rb') as f:
file_md5 = f.readline().rstrip(b'\n') file_md5 = f.readline().rstrip(b'\n')
if file_md5 == cache_md5.encode('utf8'):
if (not self.options.safe_cache) or 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:
return
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:
return




# Parse the grammar file and compose the grammars # Parse the grammar file and compose the grammars
@@ -396,8 +385,7 @@ class Lark(Serialize):
logger.debug('Saving grammar to cache: %s', cache_fn) logger.debug('Saving grammar to cache: %s', cache_fn)
with FS.open(cache_fn, 'wb') as f: with FS.open(cache_fn, 'wb') as f:
f.write(b'%s\n' % cache_md5.encode('utf8')) f.write(b'%s\n' % cache_md5.encode('utf8'))
if self.options.safe_cache:
pickle.dump(used_files, f)
pickle.dump(used_files, f)
self.save(f) self.save(f)


if __doc__: if __doc__:


Loading…
Cancel
Save