MCPcopy Create free account
hub / github.com/cloudpipe/cloudpickle / test_module_importability

Method test_module_importability

tests/cloudpickle_test.py:765–824  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

763 assert b"math" not in b
764
765 def test_module_importability(self):
766 import pickle
767 import os.path
768 import collections
769 import collections.abc
770
771 assert _should_pickle_by_reference(pickle)
772 assert _should_pickle_by_reference(os.path) # fake (aliased) module
773 assert _should_pickle_by_reference(collections) # package
774 assert _should_pickle_by_reference(collections.abc) # module in package
775
776 dynamic_module = types.ModuleType("dynamic_module")
777 assert not _should_pickle_by_reference(dynamic_module)
778
779 if platform.python_implementation() == "PyPy":
780 import _codecs
781
782 assert _should_pickle_by_reference(_codecs)
783
784 # #354: Check that modules created dynamically during the import of
785 # their parent modules are considered importable by cloudpickle.
786 # See the mod_with_dynamic_submodule documentation for more
787 # details of this use case.
788 m = pytest.importorskip(
789 "_cloudpickle_testpkg.mod.dynamic_submodule"
790 ) # noqa F841
791 assert _should_pickle_by_reference(m)
792 assert pickle_depickle(m, protocol=self.protocol) is m
793
794 # Check for similar behavior for a module that cannot be imported by
795 # attribute lookup.
796 from _cloudpickle_testpkg.mod import dynamic_submodule_two as m2
797
798 assert _should_pickle_by_reference(m2)
799 assert pickle_depickle(m2, protocol=self.protocol) is m2
800
801 # Submodule_three is a dynamic module only importable via module lookup
802 with pytest.raises(ImportError):
803 import _cloudpickle_testpkg.mod.submodule_three # noqa
804 from _cloudpickle_testpkg.mod import submodule_three as m3
805
806 assert not _should_pickle_by_reference(m3)
807
808 # This module cannot be pickled using attribute lookup (as it does not
809 # have a `__module__` attribute like classes and functions.
810 assert not hasattr(m3, "__module__")
811 depickled_m3 = pickle_depickle(m3, protocol=self.protocol)
812 assert depickled_m3 is not m3
813 assert m3.f(1) == depickled_m3.f(1)
814
815 # Do the same for an importable dynamic submodule inside a dynamic
816 # module inside a file-backed module.
817 import _cloudpickle_testpkg.mod.dynamic_submodule.dynamic_subsubmodule as sm # noqa
818
819 assert _should_pickle_by_reference(sm)
820 assert pickle_depickle(sm, protocol=self.protocol) is sm
821
822 expected = "cannot check importability of object instances"

Callers

nothing calls this directly

Calls 3

pickle_depickleFunction · 0.85
fMethod · 0.45

Tested by

no test coverage detected