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

pickling a dynamic module remove its __builtins__ as a side effect #425

Closed
pierreglaser opened this issue Jun 15, 2021 · 0 comments · Fixed by #426
Closed

pickling a dynamic module remove its __builtins__ as a side effect #425

pierreglaser opened this issue Jun 15, 2021 · 0 comments · Fixed by #426

Comments

@pierreglaser
Copy link
Member

Reproducer below:

(test) ~/repos/cloudpickle (master)❯_ ipython
Python 3.9.2 | packaged by conda-forge | (default, Feb 21 2021, 05:00:30)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.22.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from types import ModuleType

In [2]: m = ModuleType('mod')

In [3]: exec("""
   ...: def f():
   ...:     import math
   ...:     return math.sqrt(4)
   ...: """, vars(m))

In [4]: m.f()
Out[4]: 2.0

In [5]: import cloudpickle

In [6]: roundtripped_m = cloudpickle.loads(cloudpickle.dumps(m))

In [7]: roundtripped_m.f()
Out[7]: 2.0

In [8]: m.f()
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-8-ada232509f74> in <module>
----> 1 m.f()

<string> in f()

ImportError: __import__ not found

The issue comes from the _module_reduce function, that removes the item corresponding to the __builtins__ key from the module being pickled (see #325). We should instead copy the module state before removing the item corresponding to the __builtins__ key.

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.

1 participant