Skip to content

Commit

Permalink
Dynamic modules don't necessarily have a __package__ attribute
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mariusvniekerk authored and rgbkrk committed Aug 15, 2017
1 parent 10b4b48 commit 694c8df
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion cloudpickle/cloudpickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ def _save_subimports(self, code, top_level_dependencies):
"""
# check if any known dependency is an imported package
for x in top_level_dependencies:
if isinstance(x, types.ModuleType) and x.__package__:
if isinstance(x, types.ModuleType) and hasattr(x, '__package__') and x.__package__:
# check if the package has any currently loaded sub-imports
prefix = x.__name__ + '.'
for name, module in sys.modules.items():
Expand Down

0 comments on commit 694c8df

Please sign in to comment.