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

Dynamic modules don't necessarily have a __package__ attribute #116

Merged
merged 2 commits into from
Aug 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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__:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @mariusvniekerk, I just happened to see this PR and I am interested in this change. Would it make sense to add a simple test here if it is not too difficult?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

# check if the package has any currently loaded sub-imports
prefix = x.__name__ + '.'
for name, module in sys.modules.items():
Expand Down
16 changes: 16 additions & 0 deletions tests/cloudpickle_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,22 @@ def foo():
finally:
sys.modules.pop("_fake_module", None)

def test_dynamic_pytest_module(self):
# Test case for pull request https://github.com/cloudpipe/cloudpickle/pull/116
import py

def f():
s = py.builtin.set([1])
return s.pop()

# some setup is required to allow pytest apimodules to be correctly serializable.
from cloudpickle import CloudPickler
from py._apipkg import ApiModule
CloudPickler.dispatch[ApiModule] = CloudPickler.save_module
g = cloudpickle.loads(cloudpickle.dumps(f))

result = g()
self.assertEqual(1, result)

if __name__ == '__main__':
unittest.main()