Skip to content

Commit 1732e3d

Browse files
committed
new function is_module_pickle()
1 parent da4cc07 commit 1732e3d

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

dill/__init__.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525

2626
from ._dill import (
2727
dump, dumps, load, loads, dump_module, load_module, load_module_asdict,
28-
dump_session, load_session, Pickler, Unpickler, register, copy, pickle,
29-
pickles, check, HIGHEST_PROTOCOL, DEFAULT_PROTOCOL, PicklingError,
30-
UnpicklingError, HANDLE_FMODE, CONTENTS_FMODE, FILE_FMODE, PickleError,
31-
PickleWarning, PicklingWarning, UnpicklingWarning,
28+
dump_session, load_session, is_module_pickle, Pickler, Unpickler, register,
29+
copy, pickle, pickles, check, HIGHEST_PROTOCOL, DEFAULT_PROTOCOL,
30+
PicklingError, UnpicklingError, HANDLE_FMODE, CONTENTS_FMODE, FILE_FMODE,
31+
PickleError, PickleWarning, PicklingWarning, UnpicklingWarning,
3232
)
3333
from . import source, temp, detect
3434

dill/_dill.py

+27-5
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
"""
1818
__all__ = [
1919
'dump', 'dumps', 'load', 'loads', 'dump_module', 'load_module',
20-
'load_module_asdict', 'dump_session', 'load_session', 'Pickler', 'Unpickler',
21-
'register', 'copy', 'pickle', 'pickles', 'check', 'HIGHEST_PROTOCOL',
22-
'DEFAULT_PROTOCOL', 'PicklingError', 'UnpicklingError', 'HANDLE_FMODE',
23-
'CONTENTS_FMODE', 'FILE_FMODE', 'PickleError', 'PickleWarning',
24-
'PicklingWarning', 'UnpicklingWarning'
20+
'load_module_asdict', 'dump_session', 'load_session', 'is_module_pickle',
21+
'Pickler', 'Unpickler', 'register', 'copy', 'pickle', 'pickles', 'check',
22+
'HIGHEST_PROTOCOL', 'DEFAULT_PROTOCOL', 'PicklingError', 'UnpicklingError',
23+
'HANDLE_FMODE', 'CONTENTS_FMODE', 'FILE_FMODE', 'PickleError',
24+
'PickleWarning', 'PicklingWarning', 'UnpicklingWarning',
2525
]
2626

2727
__module__ = 'dill'
@@ -606,6 +606,28 @@ def _identify_module(file, main=None):
606606
return None
607607
raise UnpicklingError("unable to identify main module") from error
608608

609+
def is_module_pickle(filename, importable: bool = True) -> bool:
610+
"""Check if a file is a module state pickle file.
611+
612+
Parameters:
613+
filename: a path-like object or a readable stream.
614+
importable: expected kind of the file's saved module. Use `True` for
615+
importable modules (the default) or `False` for module-type objects.
616+
617+
Returns:
618+
`True` if the pickle file at ``filename`` was generated with
619+
:py:func:`dump_module` **AND** the module whose state is saved in it is
620+
of the kind specified by the ``importable`` argument. `False` otherwise.
621+
"""
622+
with _open(filename, 'rb', peekable=True) as file:
623+
try:
624+
pickle_main = _identify_module(file)
625+
except UnpicklingError:
626+
return False
627+
else:
628+
is_runtime_mod = pickle_main.startswith('__runtime__.')
629+
return importable ^ is_runtime_mod
630+
609631
def load_module(
610632
filename = str(TEMPDIR/'session.pkl'),
611633
module: Union[ModuleType, str] = None,

0 commit comments

Comments
 (0)