MCPcopy
hub / github.com/cloudpipe/cloudpickle / test_module_importability

Method test_module_importability

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

Source from the content-addressed store, hash-verified

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