You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The following test started to fail in the latest 0.5.6 release:
importnumpyasnpimportcloudpickledeftest_pickle_globals():
deff(x):
returnnp.sin(x) +np.cos(x) # requires access to global numpydefg(): # some other object not necessary for fpassd=cloudpickle.loads(cloudpickle.dumps(f)).__globals__assert'g'notindassertset(d) == {'np', '__builtins__'}
It now seems that cloudpickle brings in many more globals into the serialization.
I believe #188 already introduced the issue but only for functions defined in the main while #198 propagated it to functions defined in a local scope of a regular module.
This test does not really check that those extra objects where included in the pickle though. The __globals__ attribute of the unpickled function is now repopulated with the references of the module where it is unpickled. To check that we did not include any extra unwanted object, we need to depickle in a new vanilla python process.
In [1]: importnumpyasnp
...: importcloudpickle
...:
In [2]: deff(x):
...: returnnp.sin(x) +np.cos(x)
...:
In [3]: b=cloudpickle.dumps(f)
In [4]: defg():
...: pass
...:
In [5]: b2=cloudpickle.dumps(f)
In [6]: b==b2Out[6]: True
The current behavior is to try to retrieve the globals from the module where f lives before creating new variables. For the test you are referring to, as f is re-imported in the same module, f.__globals__ is set to vars(f.__module__) and no globals are changed because they all exists. You can check that with assert d is f.__globals__
So I think the test should be changed to test that importing a function from a __main__ module in a new subprocess does not import too many global variables.
Activity
ogrisel commentedon Sep 14, 2018
This is a consequence of #188 followed by #198.
I believe #188 already introduced the issue but only for functions defined in the main while #198 propagated it to functions defined in a local scope of a regular module.
ogrisel commentedon Sep 14, 2018
This test does not really check that those extra objects where included in the pickle though. The
__globals__attribute of the unpickled function is now repopulated with the references of the module where it is unpickled. To check that we did not include any extra unwanted object, we need to depickle in a new vanilla python process.ogrisel commentedon Sep 14, 2018
So maybe it's not a bug :) But we need to improve the tests in cloudpickle to make sure that we did no introduce a regression.
mrocklin commentedon Sep 14, 2018
Ah, interesting. Another way to test:
tomMoral commentedon Sep 14, 2018
The current behavior is to try to retrieve the
globalsfrom the module whereflives before creating new variables. For the test you are referring to, as f is re-imported in the same module,f.__globals__is set tovars(f.__module__)and noglobalsare changed because they all exists. You can check that withassert d is f.__globals__So I think the test should be changed to test that importing a function from a
__main__module in a new subprocess does not import too many global variables.ogrisel commentedon Sep 14, 2018
Here is another one that passes:
Change test_pickle_globals to not check __globals__ directly
Change test_pickle_globals to not check __globals__ directly
mrocklin commentedon Sep 14, 2018
OK. Closing as not-a-bug. Thanks everyone for the help.
ogrisel commentedon Sep 14, 2018
Great, I opened #203 to improve the cloudpickle tests.
Change test_pickle_globals to not check __globals__ directly (#3984)