Skip to content

Commit

Permalink
fix _stash_modules() arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
leogama committed Sep 14, 2022
1 parent 71b4f77 commit 4244d3b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
2 changes: 2 additions & 0 deletions dill/_dill.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from dill import logging
from .logging import adapter as logger
from .logging import trace as _trace
_logger = logging.getLogger(__name__)

import os
import sys
Expand Down Expand Up @@ -1350,6 +1351,7 @@ def _save_module_dict(pickler, main_dict):
is_builtin = _is_builtin_module(main)
pickler.write(MARK + DICT) # don't need to memoize
for name, value in main_dict.items():
_logger.debug("Pickling %r (%s)", name, type(value).__name__)
pickler.save(name)
try:
if pickler.save(value):
Expand Down
11 changes: 8 additions & 3 deletions dill/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@

BUILTIN_CONSTANTS = (None, False, True, NotImplemented)

def _stash_modules(main_module):
def _stash_modules(main_module, original_main):
"""pop imported variables to be saved by reference in the __dill_imported* attributes"""
modmap = _module_map(main_module)
modmap = _module_map(original_main)
newmod = ModuleType(main_module.__name__)
original = {}
imported = []
Expand Down Expand Up @@ -136,6 +136,7 @@ def _stash_modules(main_module):
refimported += [(name, mod) for mod, name in imported_top_level]
message = "[dump_module] Variables saved by reference (refimported):\n"
logger.info(message + _format_log_dict(dict(refimported)))
logger.debug("main namespace after _stash_modules(): %s", dir(newmod))

return newmod, modmap
else:
Expand Down Expand Up @@ -176,6 +177,7 @@ def _filter_vars(main_module, exclude, include, base_rules):
newmod = ModuleType(main_module.__name__)
newmod.__dict__.update(namespace)
_discard_added_variables(newmod, namespace)
logger.debug("main namespace after _filter_vars(): %s", dir(newmod))
return newmod

def _discard_added_variables(main, original_namespace):
Expand Down Expand Up @@ -363,9 +365,12 @@ def dump_module(
if not isinstance(main, ModuleType):
raise TypeError("%r is not a module" % main)
original_main = main

logger.debug("original main namespace: %s", dir(main))
main = _filter_vars(main, exclude, include, base_rules)
if refimported:
main, modmap = _stash_modules(original_main)
main, modmap = _stash_modules(main, original_main)

with _open(filename, 'wb', seekable=True) as file:
pickler = Pickler(file, protocol, **kwds)
pickler._main = main #FIXME: dill.settings are disabled
Expand Down
2 changes: 1 addition & 1 deletion dill/tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def test_runtime_module():
runtime_mod = ModuleType(modname)
runtime_mod.x = 42

mod, _ = dill.session._stash_modules(runtime_mod)
mod, _ = dill.session._stash_modules(runtime_mod, runtime_mod)
if mod is not runtime_mod:
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

0 comments on commit 4244d3b

Please sign in to comment.