Fix dill._dill submodule being saved as GLOBAL "dill._shims" "_dill"#490
Merged
mmckerns merged 1 commit intouqfoundation:masterfrom Jun 8, 2022
Merged
Fix dill._dill submodule being saved as GLOBAL "dill._shims" "_dill"#490mmckerns merged 1 commit intouqfoundation:masterfrom
dill._dill submodule being saved as GLOBAL "dill._shims" "_dill"#490mmckerns merged 1 commit intouqfoundation:masterfrom
Conversation
I'm tracing some problems with my "portable" mode prototype and hit a bug with references the `_dill` submodule saved as global: ```python >>> import dill >>> dill.dumps(dill._dill, 0) b'cdill._shims\n_dill\np0\n.' ``` The `_dill` submodule is saved as an attribute of the `dill._shims` submodule, which is valid, but it should be just `dill._dill`. The `_dill` submodule is special-cased to be saved as global: https://github.com/uqfoundation/dill/blob/8e5e450b9ed8dff164fc259c468365e2235c6581/dill/_dill.py#L1807-L1810 But `pickle.whichmodule` misidentifies it as pertaining to `dill._shims`, because this entry is set earlier in `sys.modules` and it picks the first match. --- The change fixes things for this especial case, but there is potential for new bugs related to other submodules: ```python >>> import dill, glob, importlib, os, pickle, pprint >>> os.chdir(dill.__path__[0]) >>> modules = [mod.rpartition('.')[0] for mod in glob.glob('*py') if not mod.startswith('__')] >>> modules = {mod: importlib.import_module('dill.' + mod) for mod in modules} >>> pprint.pprint({name: pickle.whichmodule(mod, name) for name, mod in modules.items()}) {'_dill': 'dill._shims', '_objects': 'dill', '_shims': 'dill._dill', 'detect': 'dill', 'objtypes': 'dill', 'pointers': 'dill', 'settings': '__main__', 'source': 'dill', 'temp': 'dill'} ``` Note how `_shims` is attributed to `dill._dill` and `settings` is attributed to `__main__`(???). But currently they are not saved as globals.
Contributor
|
That behavior is bizarre. I guess in CPython, they assumed that every global would have a |
Member
|
Nice find. I can't wait to see what unintended consequences this has... |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I'm tracing some problems with my "portable" mode prototype and hit a bug with references to the
_dillsubmodule saved as global:The
_dillsubmodule is saved as an attribute of thedill._shimssubmodule, which is valid, but it should be justdill._dill.The
_dillsubmodule is special-cased to be saved as global:dill/dill/_dill.py
Lines 1807 to 1810 in 8e5e450
But
pickle.whichmodulemisidentifies it as pertaining todill._shims, because this entry is set earlier insys.modulesand it picks the first match.The change fixes things for this especial case, but there is potential for new bugs related to other submodules:
Note how
_shimsis attributed todill._dillandsettingsis attributed to__main__(???). But currently they are not saved as globals.