Skip to content

Commit

Permalink
sync with branch 'session-excludes'
Browse files Browse the repository at this point in the history
  • Loading branch information
leogama committed Jul 22, 2022
1 parent 5e4d912 commit 00fa855
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
22 changes: 11 additions & 11 deletions dill/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,9 @@ def _restore_modules(unpickler, main_module):
def dump_module(
filename = str(TEMPDIR/'session.pkl'),
module: Union[ModuleType, str] = None,
refimported: bool = False,
refonfail: bool = False,
*,
refimported: bool = None,
refonfail: bool = None,
**kwds
) -> None:
R"""Pickle the current state of :py:mod:`__main__` or another module to a file.
Expand Down Expand Up @@ -285,8 +286,10 @@ def dump_module(

from .settings import settings
protocol = settings['protocol']
if refimported is None: refimported = settings['dump_module']['refimported']
if refonfail is None: refonfail = settings['dump_module']['refonfail']
if refimported is None:
refimported = settings['dump_module']['refimported']
if refonfail is None:
refonfail = settings['dump_module']['refonfail']
main = module
if main is None:
main = _main_module
Expand Down Expand Up @@ -486,20 +489,17 @@ def load_module(

# Resolve unpickler._main
pickle_main = _identify_module(file, main)
if main is None and pickle_main is not None:
if main is None:
main = pickle_main
if isinstance(main, str):
if main.startswith('__runtime__.'):
# Create runtime module to load the session into.
main = ModuleType(main.partition('.')[-1])
else:
main = _import_module(main)
if main is not None:
if not isinstance(main, ModuleType):
raise TypeError("%r is not a module" % main)
unpickler._main = main
else:
main = unpickler._main
if not isinstance(main, ModuleType):
raise TypeError("%r is not a module" % main)
unpickler._main = main

# Check against the pickle's main.
is_main_imported = _is_imported_module(main)
Expand Down
3 changes: 2 additions & 1 deletion dill/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
global settings for Pickler
"""

__all__ = ['settings']

from pickle import DEFAULT_PROTOCOL

settings = {
Expand All @@ -26,4 +28,3 @@
}

del DEFAULT_PROTOCOL

7 changes: 4 additions & 3 deletions dill/tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import __main__
from contextlib import suppress
from io import BytesIO
from types import ModuleType

import dill

Expand Down Expand Up @@ -192,12 +193,12 @@ def test_session_other():
assert module.selfref is module

def test_runtime_module():
from types import ModuleType
from dill.session import _stash_modules
modname = '__runtime__'
runtime = ModuleType(modname)
runtime.x = 42

mod = dill.session._stash_modules(runtime)
mod = _stash_modules(runtime, runtime)
if mod is not runtime:
print("There are objects to save by referenece that shouldn't be:",
mod.__dill_imported, mod.__dill_imported_as, mod.__dill_imported_top_level,
Expand Down Expand Up @@ -230,7 +231,7 @@ def test_refimported_imported_as():
import concurrent.futures
import types
import typing
mod = sys.modules['__test__'] = types.ModuleType('__test__')
mod = sys.modules['__test__'] = ModuleType('__test__')
dill.executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
mod.Dict = collections.UserDict # select by type
mod.AsyncCM = typing.AsyncContextManager # select by __module__
Expand Down
14 changes: 13 additions & 1 deletion docs/source/dill.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ dill module
:special-members:
:show-inheritance:
:imported-members:
:exclude-members: dump_session, load_session
.. :exclude-members:
detect module
-------------
Expand Down Expand Up @@ -49,6 +49,18 @@ pointers module
:imported-members:
.. :exclude-members:
session module
---------------

.. automodule:: dill.session
:members:
:undoc-members:
:private-members:
:special-members:
:show-inheritance:
:imported-members:
:exclude-members: dump_session, load_session

settings module
---------------

Expand Down

0 comments on commit 00fa855

Please sign in to comment.