-
Notifications
You must be signed in to change notification settings - Fork 168
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
WIP refactor func globals to make external delegation possible #430
base: master
Are you sure you want to change the base?
Changes from 1 commit
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 |
---|---|---|
|
@@ -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) | ||
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.
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. I feel like this is worse than my solution:
My solution does not bring any more APIs as it follows official python docs. It also does not complicate serialization for people who don't use 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. But I don't like your solution because it's calling If we don't like the wrapping, we could instead inject a specific key inside the dict with the id of the full 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.
Are you talking about
I don't see how it is. When you replace import io
import pickle
__globals__ = {"a": "foo"}
class Pickler(pickle.Pickler):
@staticmethod
def persistent_id(obj):
if id(obj) == id(__globals__):
return "__globals__"
class Unpickler(pickle.Unpickler):
@staticmethod
def persistent_load(pid):
return {"__globals__": __globals__}[pid]
exec("""
def get():
return a
""", __globals__)
get = __globals__['get']
file = io.BytesIO()
Pickler(file).dump(get)
dumped = file.getvalue()
if b'foo' in dumped:
raise Exception(repr(dumped))
get = Unpickler(io.BytesIO(dumped)).load()
if id(__globals__) != id(get.__globals__):
raise Exception(f"id(__globals__) != id(get.__globals__)")
print(f"get() == {get()}")
__globals__['a'] = 'bar'
print(f"get() == {get()}")
That's right, users don't need that info. 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.
Your example here works without defining In [9]: import io
...: import pickle
...:
...: __globals__ = {"a": "foo"}
...:
...:
...: class Pickler(pickle.Pickler):
...: pass
...:
...: class Unpickler(pickle.Unpickler):
...: pass
...:
...: exec("""
...: def get():
...: return a
...: """, __globals__)
...: get = __globals__['get']
...: file = io.BytesIO()
...: Pickler(file).dump(get)
...: dumped = file.getvalue()
...: if b'foo' in dumped:
...: raise Exception(repr(dumped))
...: get = Unpickler(io.BytesIO(dumped)).load()
...: if id(__globals__) != id(get.__globals__):
...: raise Exception(f"id(__globals__) != id(get.__globals__)")
...: print(f"get() == {get()}")
...: __globals__['a'] = 'bar'
...: print(f"get() == {get()}") I'll try to explain one more time why your PR does not use
In other words, 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.
"pickles full globals dict" in which setting? import pickle
global_var = 1
def f():
return global_var
pickle.dumps(f) does not pickle any global variable, or any dictionary containing global variables. It pickles |
||
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
cloudpickle
package even if they are private API?