Skip to content

Commit 694c8df

Browse files
mariusvniekerkrgbkrk
authored andcommitted
Dynamic modules don't necessarily have a __package__ attribute
When trying to serialize some of the dynamically generated modules that pytest generates, errors like these are encountered. ``` File "/Users/mvanniekerk/miniconda3/envs/py36/lib/python3.6/pickle.py", line 476, in save f(self, obj) # Call unbound method with explicit self File "/Users/mvanniekerk/src/pytest-dask/.tox/py36/lib/python3.6/site-packages/cloudpickle/cloudpickle.py", line 366, in save_function self.save_function_tuple(obj) File "/Users/mvanniekerk/src/pytest-dask/.tox/py36/lib/python3.6/site-packages/cloudpickle/cloudpickle.py", line 494, in save_function_tuple itertools.chain(f_globals.values(), closure_values or ()), File "/Users/mvanniekerk/src/pytest-dask/.tox/py36/lib/python3.6/site-packages/cloudpickle/cloudpickle.py", line 388, in _save_subimports if isinstance(x, types.ModuleType) and x.__package__: File "/Users/mvanniekerk/src/pytest-dask/.tox/py36/lib/python3.6/site-packages/py/_apipkg.py", line 123, in __makeattr raise AttributeError(name) AttributeError: __package__ ``` After applying this change the resulting serialized objects work correctly
1 parent 10b4b48 commit 694c8df

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

cloudpickle/cloudpickle.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ def _save_subimports(self, code, top_level_dependencies):
385385
"""
386386
# check if any known dependency is an imported package
387387
for x in top_level_dependencies:
388-
if isinstance(x, types.ModuleType) and x.__package__:
388+
if isinstance(x, types.ModuleType) and hasattr(x, '__package__') and x.__package__:
389389
# check if the package has any currently loaded sub-imports
390390
prefix = x.__name__ + '.'
391391
for name, module in sys.modules.items():

0 commit comments

Comments
 (0)