|
| 1 | +from sys import version_info |
| 2 | +from textwrap import dedent |
| 3 | +from unittest import skipIf |
| 4 | + |
| 5 | +from torch.package import ( |
| 6 | + DeniedModuleError, |
| 7 | + EmptyMatchError, |
| 8 | + PackageExporter, |
| 9 | + PackageImporter, |
| 10 | +) |
| 11 | +from torch.testing._internal.common_utils import run_tests |
| 12 | + |
| 13 | +try: |
| 14 | + from .common import PackageTestCase |
| 15 | +except ImportError: |
| 16 | + # Support the case where we run this file directly. |
| 17 | + from common import PackageTestCase # type: ignore |
| 18 | + |
| 19 | + |
| 20 | +class TestDependencyAPI(PackageTestCase): |
| 21 | + """Dependency management API tests. |
| 22 | + - mock() |
| 23 | + - extern() |
| 24 | + - deny() |
| 25 | + """ |
| 26 | + |
| 27 | + def test_extern(self): |
| 28 | + filename = self.temp() |
| 29 | + with PackageExporter(filename, verbose=False) as he: |
| 30 | + he.extern(["package_a.subpackage", "module_a"]) |
| 31 | + he.require_module("package_a.subpackage") |
| 32 | + he.require_module("module_a") |
| 33 | + he.save_module("package_a") |
| 34 | + hi = PackageImporter(filename) |
| 35 | + import module_a |
| 36 | + import package_a.subpackage |
| 37 | + |
| 38 | + module_a_im = hi.import_module("module_a") |
| 39 | + hi.import_module("package_a.subpackage") |
| 40 | + package_a_im = hi.import_module("package_a") |
| 41 | + |
| 42 | + self.assertIs(module_a, module_a_im) |
| 43 | + self.assertIsNot(package_a, package_a_im) |
| 44 | + self.assertIs(package_a.subpackage, package_a_im.subpackage) |
| 45 | + |
| 46 | + def test_extern_glob(self): |
| 47 | + filename = self.temp() |
| 48 | + with PackageExporter(filename, verbose=False) as he: |
| 49 | + he.extern(["package_a.*", "module_*"]) |
| 50 | + he.save_module("package_a") |
| 51 | + he.save_source_string( |
| 52 | + "test_module", |
| 53 | + dedent( |
| 54 | + """\ |
| 55 | + import package_a.subpackage |
| 56 | + import module_a |
| 57 | + """ |
| 58 | + ), |
| 59 | + ) |
| 60 | + hi = PackageImporter(filename) |
| 61 | + import module_a |
| 62 | + import package_a.subpackage |
| 63 | + |
| 64 | + module_a_im = hi.import_module("module_a") |
| 65 | + hi.import_module("package_a.subpackage") |
| 66 | + package_a_im = hi.import_module("package_a") |
| 67 | + |
| 68 | + self.assertIs(module_a, module_a_im) |
| 69 | + self.assertIsNot(package_a, package_a_im) |
| 70 | + self.assertIs(package_a.subpackage, package_a_im.subpackage) |
| 71 | + |
| 72 | + def test_extern_glob_allow_empty(self): |
| 73 | + """ |
| 74 | + Test that an error is thrown when a extern glob is specified with allow_empty=True |
| 75 | + and no matching module is required during packaging. |
| 76 | + """ |
| 77 | + filename = self.temp() |
| 78 | + with self.assertRaisesRegex(EmptyMatchError, r"did not match any modules"): |
| 79 | + with PackageExporter(filename, verbose=False) as exporter: |
| 80 | + exporter.extern(include=["package_a.*"], allow_empty=False) |
| 81 | + exporter.save_module("package_b.subpackage") |
| 82 | + |
| 83 | + def test_deny(self): |
| 84 | + """ |
| 85 | + Test marking packages as "deny" during export. |
| 86 | + """ |
| 87 | + filename = self.temp() |
| 88 | + |
| 89 | + with self.assertRaisesRegex( |
| 90 | + DeniedModuleError, |
| 91 | + "required during packaging but has been explicitly blocklisted", |
| 92 | + ): |
| 93 | + with PackageExporter(filename, verbose=False) as exporter: |
| 94 | + exporter.deny(["package_a.subpackage", "module_a"]) |
| 95 | + exporter.require_module("package_a.subpackage") |
| 96 | + |
| 97 | + def test_deny_glob(self): |
| 98 | + """ |
| 99 | + Test marking packages as "deny" using globs instead of package names. |
| 100 | + """ |
| 101 | + filename = self.temp() |
| 102 | + with self.assertRaisesRegex( |
| 103 | + DeniedModuleError, |
| 104 | + "required during packaging but has been explicitly blocklisted", |
| 105 | + ): |
| 106 | + with PackageExporter(filename, verbose=False) as exporter: |
| 107 | + exporter.deny(["package_a.*", "module_*"]) |
| 108 | + exporter.save_source_string( |
| 109 | + "test_module", |
| 110 | + dedent( |
| 111 | + """\ |
| 112 | + import package_a.subpackage |
| 113 | + import module_a |
| 114 | + """ |
| 115 | + ), |
| 116 | + ) |
| 117 | + |
| 118 | + @skipIf(version_info < (3, 7), "mock uses __getattr__ a 3.7 feature") |
| 119 | + def test_mock(self): |
| 120 | + filename = self.temp() |
| 121 | + with PackageExporter(filename, verbose=False) as he: |
| 122 | + he.mock(["package_a.subpackage", "module_a"]) |
| 123 | + he.save_module("package_a") |
| 124 | + he.require_module("package_a.subpackage") |
| 125 | + he.require_module("module_a") |
| 126 | + hi = PackageImporter(filename) |
| 127 | + import package_a.subpackage |
| 128 | + |
| 129 | + _ = package_a.subpackage |
| 130 | + import module_a |
| 131 | + |
| 132 | + _ = module_a |
| 133 | + |
| 134 | + m = hi.import_module("package_a.subpackage") |
| 135 | + r = m.result |
| 136 | + with self.assertRaisesRegex(NotImplementedError, "was mocked out"): |
| 137 | + r() |
| 138 | + |
| 139 | + @skipIf(version_info < (3, 7), "mock uses __getattr__ a 3.7 feature") |
| 140 | + def test_mock_glob(self): |
| 141 | + filename = self.temp() |
| 142 | + with PackageExporter(filename, verbose=False) as he: |
| 143 | + he.mock(["package_a.*", "module*"]) |
| 144 | + he.save_module("package_a") |
| 145 | + he.save_source_string( |
| 146 | + "test_module", |
| 147 | + dedent( |
| 148 | + """\ |
| 149 | + import package_a.subpackage |
| 150 | + import module_a |
| 151 | + """ |
| 152 | + ), |
| 153 | + ) |
| 154 | + hi = PackageImporter(filename) |
| 155 | + import package_a.subpackage |
| 156 | + |
| 157 | + _ = package_a.subpackage |
| 158 | + import module_a |
| 159 | + |
| 160 | + _ = module_a |
| 161 | + |
| 162 | + m = hi.import_module("package_a.subpackage") |
| 163 | + r = m.result |
| 164 | + with self.assertRaisesRegex(NotImplementedError, "was mocked out"): |
| 165 | + r() |
| 166 | + |
| 167 | + def test_mock_glob_allow_empty(self): |
| 168 | + """ |
| 169 | + Test that an error is thrown when a mock glob is specified with allow_empty=True |
| 170 | + and no matching module is required during packaging. |
| 171 | + """ |
| 172 | + filename = self.temp() |
| 173 | + with self.assertRaisesRegex(EmptyMatchError, r"did not match any modules"): |
| 174 | + with PackageExporter(filename, verbose=False) as exporter: |
| 175 | + exporter.mock(include=["package_a.*"], allow_empty=False) |
| 176 | + exporter.save_module("package_b.subpackage") |
| 177 | + |
| 178 | + def test_module_glob(self): |
| 179 | + from torch.package.package_exporter import _GlobGroup |
| 180 | + |
| 181 | + def check(include, exclude, should_match, should_not_match): |
| 182 | + x = _GlobGroup(include, exclude) |
| 183 | + for e in should_match: |
| 184 | + self.assertTrue(x.matches(e)) |
| 185 | + for e in should_not_match: |
| 186 | + self.assertFalse(x.matches(e)) |
| 187 | + |
| 188 | + check( |
| 189 | + "torch.*", |
| 190 | + [], |
| 191 | + ["torch.foo", "torch.bar"], |
| 192 | + ["tor.foo", "torch.foo.bar", "torch"], |
| 193 | + ) |
| 194 | + check( |
| 195 | + "torch.**", |
| 196 | + [], |
| 197 | + ["torch.foo", "torch.bar", "torch.foo.bar", "torch"], |
| 198 | + ["what.torch", "torchvision"], |
| 199 | + ) |
| 200 | + check("torch.*.foo", [], ["torch.w.foo"], ["torch.hi.bar.baz"]) |
| 201 | + check( |
| 202 | + "torch.**.foo", [], ["torch.w.foo", "torch.hi.bar.foo"], ["torch.f.foo.z"] |
| 203 | + ) |
| 204 | + check("torch*", [], ["torch", "torchvision"], ["torch.f"]) |
| 205 | + check( |
| 206 | + "torch.**", |
| 207 | + ["torch.**.foo"], |
| 208 | + ["torch", "torch.bar", "torch.barfoo"], |
| 209 | + ["torch.foo", "torch.some.foo"], |
| 210 | + ) |
| 211 | + check("**.torch", [], ["torch", "bar.torch"], ["visiontorch"]) |
| 212 | + |
| 213 | + @skipIf(version_info < (3, 7), "mock uses __getattr__ a 3.7 feature") |
| 214 | + def test_pickle_mocked(self): |
| 215 | + import package_a.subpackage |
| 216 | + |
| 217 | + obj = package_a.subpackage.PackageASubpackageObject() |
| 218 | + obj2 = package_a.PackageAObject(obj) |
| 219 | + |
| 220 | + filename = self.temp() |
| 221 | + with PackageExporter(filename, verbose=False) as he: |
| 222 | + he.mock(include="package_a.subpackage") |
| 223 | + he.save_pickle("obj", "obj.pkl", obj2) |
| 224 | + |
| 225 | + hi = PackageImporter(filename) |
| 226 | + with self.assertRaises(NotImplementedError): |
| 227 | + hi.load_pickle("obj", "obj.pkl") |
| 228 | + |
| 229 | + |
| 230 | +if __name__ == "__main__": |
| 231 | + run_tests() |
0 commit comments