From 694c8df784bf829618b476f42c6bbcd4a3602acf Mon Sep 17 00:00:00 2001 From: Marius van Niekerk Date: Mon, 14 Aug 2017 11:38:31 -0400 Subject: [PATCH] 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 --- cloudpickle/cloudpickle.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloudpickle/cloudpickle.py b/cloudpickle/cloudpickle.py index 17a1f9529..d6c140871 100644 --- a/cloudpickle/cloudpickle.py +++ b/cloudpickle/cloudpickle.py @@ -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():