Skip to content
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

Function namespace preservation #466

Open
anivegesana opened this issue Apr 30, 2022 · 4 comments · May be fixed by #534
Open

Function namespace preservation #466

anivegesana opened this issue Apr 30, 2022 · 4 comments · May be fixed by #534

Comments

@anivegesana
Copy link
Contributor

Continuing the discussion from #462 (comment)

Even cloudpickle's behavior is a little bit finicky and has some unintuitive behavior. I think that there are design decisions that need to be made with regards to how we handle global dictionaries and functions.

import cloudpickle

def f():
    global x
    x = 5

def g():
    global x
    x = 0
    f()
    assert x == 5


G, GG = cloudpickle.loads(cloudpickle.dumps((g, g.__globals__)))

G.__globals__ != GG # Seems odd
@anivegesana
Copy link
Contributor Author

anivegesana commented May 16, 2022

So far, I have just written a proxy object that is supposed to effectively trap whenever the global variables dictionary is pickled (analogous to trapping segfaults to load memory in demand paging) and update the cache with the keys that have already been written out. I still need to handle pickling the GlobalVars object itself, pickling a globals dictionary directly outside of the __globals__ attribute of a function, and updating the _save_function to use this mechanism instead of the copying that it does now.

https://github.com/anivegesana/dill/tree/shared_namespace

@mmckerns
Copy link
Member

mmckerns commented Jul 29, 2022

@anivegesana: the following is the current behavior in dill:

Python 3.8.13 (default, May 10 2022, 11:26:38) 
[Clang 10.0.1 (clang-1001.0.46.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import dill
>>> def f():
...   global x
...   x = 5
... 
>>>
>>> def g():
...   global x
...   x = 0
...   f()
...   assert x == 5
... 
>>> G,GG = dill.loads(dill.dumps((g, g.__globals__)))
>>> G.__globals__ == GG
True
>>> g()
>>> G,GG = dill.loads(dill.dumps((g, g.__globals__)))
>>> G.__globals__ == GG
True
>>> 

Which is different than what you are reporting from cloudpickle. It's not clear what you want done in this issue. Can you clarify what this issue is reporting?

@anivegesana
Copy link
Contributor Author

That piece of code works when recurse is off. The issue is that the same equalities don't hold when recurse is on.

@mmckerns
Copy link
Member

Aha... using recuse=True is when dill is the most like cloudpickle... so that makes sense. Ok, got it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants