Alternative fix for bug when pickling function from unloaded module#536
Alternative fix for bug when pickling function from unloaded module#536leogama wants to merge 1 commit intouqfoundation:masterfrom
Conversation
|
I am confused about the purpose of caching the namespace. I have seen situations similar to #532 in popular packages (torch.package) but never for standard library modules, probably because you shouldn't change them, so there is no reason to reload them in the first place. Plus, what if someone changes a file and calls |
I used a stdlib module for demonstration, but this mechanism would be triggered mainly by third-party packages, as in the example you mentioned. Reloaded modules are not covered by this patch, only "unloaded" ones. With a reloaded module, modified or not, dill (pickle) would still raise an "object is not the same as XXX.YYY" error.
This conflict would only happen with a series of events like: import importlib
import sys
import dill
from module import func
dill.dumps(func) # save as global
del sys.modules['module']
dill.dumps(func) # add 'module' to _modules_copy's cache, save as global (this is the difference in behavior)
#--- Modify func in module.py ---#
import module # re-import into sys.modules (creates a new module instance)
importlib.reload(module) # required to update module.func?
dill.dumps(func) # func is not module.func, raise error
del sys.modules['module']
dill.dumps(func) # func is different from cached _modules_copy('module').func, save by valueIn this last statement, the functions wouldn't match in the Note: I have no preference for this or any of the other proposed solutions, I'm mostly throwing things against the wall. |
This patch modifies
_locate_function()andsave_function()to allow dill to save a function from a module that was unloaded, i.e. deleted from thesys.modulesdictionary, by reference (if it would be saved as global had the module not been unloaded). Note: this is independent of the shared namespace problem.It may be possible to do the same for classes and methods, but these cases are more complex.
Fixes: #532