-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
2 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,11 @@ | ||
from io import IOBase | ||
import pickle | ||
from typing import Any | ||
from checkpointing.config import defaults | ||
|
||
try: | ||
import pickle5 | ||
except ImportError: | ||
pass | ||
|
||
|
||
def get_pickle_module(protocol: int): | ||
""" | ||
Returns: | ||
built-in pickle module if the given protocol is supported. | ||
Otherwise check if the protocol = 5, if so, use pickle5. | ||
""" | ||
|
||
if protocol <= pickle.HIGHEST_PROTOCOL: | ||
return pickle | ||
|
||
elif protocol == 5: | ||
return pickle5 | ||
|
||
else: | ||
raise RuntimeError(f"pickle protocol {protocol} is not supported") | ||
|
||
|
||
def dump(obj: Any, file: IOBase, protocol: int) -> None: | ||
return get_pickle_module(protocol).dump(obj, file, protocol) | ||
return pickle.dump(obj, file, protocol) | ||
|
||
|
||
def load(file: IOBase, protocol: int) -> Any: | ||
return get_pickle_module(protocol).load(file) | ||
return pickle.load(file) |