Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 194
WIP refactor func globals to make external delegation possible#430
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
base:master
Are you sure you want to change the base?
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -49,6 +49,7 @@ | ||
| from cloudpickle.cloudpickle import _make_empty_cell, cell_set | ||
| from cloudpickle.cloudpickle import _extract_class_dict, _whichmodule | ||
| from cloudpickle.cloudpickle import _lookup_module_and_qualname | ||
| from cloudpickle.cloudpickle import _FuncMetadataGlobals, _FuncCodeGlobals | ||
| from .testutils import subprocess_pickle_echo | ||
| from .testutils import subprocess_pickle_string | ||
| @@ -2377,6 +2378,45 @@ def func_with_globals(): | ||
| "Expected a single deterministic payload, got %d/5" % len(vals) | ||
| ) | ||
| def test_externally_managed_function_globals(self): | ||
| common_globals = {"a": "foo"} | ||
| class CustomPickler(cloudpickle.CloudPickler): | ||
| @staticmethod | ||
| def persistent_id(obj): | ||
| if ( | ||
| isinstance(obj, _FuncMetadataGlobals) | ||
| ||
| and obj.func.__globals__ is common_globals | ||
| ): | ||
| return "common_globals" | ||
| elif ( | ||
| isinstance(obj, _FuncCodeGlobals) | ||
| and obj.func.__globals__ is common_globals | ||
| ): | ||
| return "empty_dict" | ||
| class CustomUnpickler(pickle.Unpickler): | ||
| @staticmethod | ||
| def persistent_load(pid): | ||
| return { | ||
| "common_globals": common_globals, | ||
| "empty_dict": {} | ||
| }[pid] | ||
| lookup_a = eval('lambda: a', common_globals) | ||
| assert lookup_a() == "foo" | ||
| file = io.BytesIO() | ||
| CustomPickler(file).dump(lookup_a) | ||
| dumped = file.getvalue() | ||
| assert b'foo' not in dumped | ||
| lookup_a_cloned = CustomUnpickler(io.BytesIO(dumped)).load() | ||
| assert lookup_a_cloned() == "foo" | ||
| assert lookup_a_cloned.__globals__ is common_globals | ||
| common_globals['a'] = 'bar' | ||
| assert lookup_a_cloned() == "bar" | ||
| class Protocol2CloudPickleTest(CloudPickleTest): | ||
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.
Do we want to make those directly importable from the top level
cloudpicklepackage even if they are private API?