forked from iterative/dvc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dvc.objects: extend diskcache to handle pickle errors (iterative#7734)
dvc.objects: extend diskcache to handle pickle errors
- Loading branch information
Showing
8 changed files
with
151 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import os | ||
import pickle | ||
from functools import wraps | ||
from typing import Any | ||
|
||
import diskcache | ||
from diskcache import Disk as disk | ||
from diskcache import Index # noqa: F401, pylint: disable=unused-import | ||
from diskcache import Timeout # noqa: F401, pylint: disable=unused-import | ||
|
||
# pylint: disable=redefined-builtin | ||
|
||
|
||
class DiskError(Exception): | ||
def __init__(self, directory: str, type: str) -> None: | ||
self.directory = directory | ||
self.type = type | ||
super().__init__(f"Could not open disk '{type}' in {directory}") | ||
|
||
|
||
def translate_pickle_error(fn): | ||
@wraps(fn) | ||
def wrapped(self, *args, **kwargs): | ||
try: | ||
return fn(self, *args, **kwargs) | ||
except (pickle.PickleError, ValueError) as e: | ||
if isinstance(e, ValueError) and "pickle protocol" not in str(e): | ||
raise | ||
# pylint: disable=protected-access | ||
raise DiskError(self._directory, type=self._type) from e | ||
|
||
return wrapped | ||
|
||
|
||
class Disk(disk): | ||
"""Reraise pickle-related errors as DiskError.""" | ||
|
||
def __init__(self, *args, type=None, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self._type = type or os.path.basename(self._directory) | ||
|
||
put = translate_pickle_error(disk.put) | ||
get = translate_pickle_error(disk.get) | ||
store = translate_pickle_error(disk.store) | ||
fetch = translate_pickle_error(disk.fetch) | ||
|
||
|
||
class Cache(diskcache.Cache): | ||
"""Extended to handle pickle errors and use a constant pickle protocol.""" | ||
|
||
def __init__( | ||
self, | ||
directory: str = None, | ||
timeout: int = 60, | ||
disk: disk = Disk, # pylint: disable=redefined-outer-name | ||
**settings: Any, | ||
) -> None: | ||
settings.setdefault("disk_pickle_protocol", 4) | ||
super().__init__( | ||
directory=directory, timeout=timeout, disk=Disk, **settings | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from argparse import Namespace | ||
|
||
from funcy import raiser | ||
|
||
from dvc.cli import main | ||
from dvc.objects.cache import DiskError | ||
|
||
|
||
def test_state_pickle_errors_are_correctly_raised(tmp_dir, caplog, mocker): | ||
path = tmp_dir / "dir" / "test" | ||
mocker.patch( | ||
"dvc.cli.parse_args", | ||
return_value=Namespace( | ||
func=raiser(DiskError(path, "md5s")), | ||
quiet=False, | ||
verbose=True, | ||
), | ||
) | ||
|
||
assert main() == 255 | ||
assert ( | ||
f"Could not open pickled 'md5s' cache.\n" | ||
f"Remove the '{path.relative_to(tmp_dir)}' directory " | ||
"and then retry this command.\n" | ||
"See <https://error.dvc.org/pickle> for more information." | ||
) in caplog.text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters