-
Notifications
You must be signed in to change notification settings - Fork 187
Handle dynamic modules globals modification #201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
f022c41
2a0c3cb
780dee7
7561346
faab1f5
c82008b
21f05db
f1f4c31
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -442,7 +442,7 @@ def method(self, x): | |
| mod1, mod2 = pickle_depickle([mod, mod]) | ||
| self.assertEqual(id(mod1), id(mod2)) | ||
|
|
||
| def test_dynamic_modules_cache(self): | ||
| def test_dynamic_modules_globals(self): | ||
| # _dynamic_modules_globals is a WeakValueDictionary, so if this dynamic | ||
| # module has no other reference than in this # dict (whether on the | ||
| # child process or the parent process), this module will be garbage | ||
|
|
@@ -452,23 +452,37 @@ def test_dynamic_modules_cache(self): | |
| mod = imp.new_module('mod') | ||
| code = ''' | ||
| x = 1 | ||
| def f(): | ||
| def func(): | ||
| return | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 on this one
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or maybe
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is. I can merge the two test, but unsure if it is better. |
||
| ''' | ||
| exec(textwrap.dedent(code), mod.__dict__) | ||
|
|
||
| pickled_module_path = 'mod.pkl' | ||
| print(mod.func.__module__) | ||
|
||
|
|
||
| with open(pickled_module_path, 'wb') as f: | ||
| cloudpickle.dump(mod, f) | ||
| cloudpickle.dump(mod.func, f) | ||
|
|
||
| # _dynamic_modules_globals will have mod appended to its values | ||
| # in the child process only | ||
| child_process_script = ''' | ||
| import pickle | ||
| import cloudpickle | ||
| from cloudpickle.cloudpickle import _dynamic_modules_globals | ||
| import gc | ||
| with open("{pickled_module_path}", 'rb') as f: | ||
| _ = pickle.load(f) | ||
| func = pickle.load(f) | ||
|
|
||
| # A dictionnary storing the globals of the newly unpickled function | ||
| # should have been created | ||
| assert list(_dynamic_modules_globals.keys())==['mod'] | ||
|
||
|
|
||
| del func | ||
| gc.collect() | ||
|
|
||
| # There is no reference to the globals of func since func has been | ||
| # deleted and _dynamic_modules_globals is a WeakValueDictionary, | ||
| # so _dynamic_modules_globals should now be empty | ||
| assert list(_dynamic_modules_globals.keys())==[] | ||
|
||
| ''' | ||
| child_process_script = child_process_script.format( | ||
| pickled_module_path=pickled_module_path) | ||
|
|
@@ -477,12 +491,6 @@ def f(): | |
|
|
||
| # We remove the other references to the object and trigger a | ||
| # gc event | ||
| del mod | ||
| gc.collect() | ||
|
|
||
| # At this point, there is no other reference to this module, so | ||
| # _dynamic_modules_globals should be empty | ||
| assert list(_dynamic_modules_globals.keys()) == [] | ||
|
|
||
| def test_load_dynamic_module_in_grandchild_process(self): | ||
| # Make sure that when loaded, a dynamic module preserves its dynamic | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#leftover