Skip to content

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

Description

@pierreglaser

Reproducer below:

(test) ~/repos/cloudpickle (master)❯_ ipythonPython3.9.2|packagedbyconda-forge| (default, Feb212021, 05:00:30)
Type'copyright', 'credits'or'license'formoreinformationIPython7.22.0--AnenhancedInteractivePython. Type'?'forhelp.
In [1]: fromtypesimportModuleTypeIn [2]: m=ModuleType('mod')
In [3]: exec(""" ...: def f(): ...: import math ...: return math.sqrt(4) ...: """, vars(m))
In [4]: m.f()
Out[4]: 2.0In [5]: importcloudpickleIn [6]: roundtripped_m=cloudpickle.loads(cloudpickle.dumps(m))
In [7]: roundtripped_m.f()
Out[7]: 2.0In [8]: m.f()
---------------------------------------------------------------------------ImportErrorTraceback (mostrecentcalllast)
<ipython-input-8-ada232509f74>in<module>---->1m.f()
<string>inf()
ImportError: __import__notfound

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions