| 1 | n/a | import sys |
|---|
| 2 | n/a | import imp |
|---|
| 3 | n/a | import os |
|---|
| 4 | n/a | import unittest |
|---|
| 5 | n/a | from test import support |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | test_src = """\ |
|---|
| 9 | n/a | def get_name(): |
|---|
| 10 | n/a | return __name__ |
|---|
| 11 | n/a | def get_file(): |
|---|
| 12 | n/a | return __file__ |
|---|
| 13 | n/a | """ |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | absimp = "import sub\n" |
|---|
| 16 | n/a | relimp = "from . import sub\n" |
|---|
| 17 | n/a | deeprelimp = "from .... import sub\n" |
|---|
| 18 | n/a | futimp = "from __future__ import absolute_import\n" |
|---|
| 19 | n/a | |
|---|
| 20 | n/a | reload_src = test_src+"""\ |
|---|
| 21 | n/a | reloaded = True |
|---|
| 22 | n/a | """ |
|---|
| 23 | n/a | |
|---|
| 24 | n/a | test_co = compile(test_src, "<???>", "exec") |
|---|
| 25 | n/a | reload_co = compile(reload_src, "<???>", "exec") |
|---|
| 26 | n/a | |
|---|
| 27 | n/a | test2_oldabs_co = compile(absimp + test_src, "<???>", "exec") |
|---|
| 28 | n/a | test2_newabs_co = compile(futimp + absimp + test_src, "<???>", "exec") |
|---|
| 29 | n/a | test2_newrel_co = compile(relimp + test_src, "<???>", "exec") |
|---|
| 30 | n/a | test2_deeprel_co = compile(deeprelimp + test_src, "<???>", "exec") |
|---|
| 31 | n/a | test2_futrel_co = compile(futimp + relimp + test_src, "<???>", "exec") |
|---|
| 32 | n/a | |
|---|
| 33 | n/a | test_path = "!!!_test_!!!" |
|---|
| 34 | n/a | |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | class TestImporter: |
|---|
| 37 | n/a | |
|---|
| 38 | n/a | modules = { |
|---|
| 39 | n/a | "hooktestmodule": (False, test_co), |
|---|
| 40 | n/a | "hooktestpackage": (True, test_co), |
|---|
| 41 | n/a | "hooktestpackage.sub": (True, test_co), |
|---|
| 42 | n/a | "hooktestpackage.sub.subber": (True, test_co), |
|---|
| 43 | n/a | "hooktestpackage.oldabs": (False, test2_oldabs_co), |
|---|
| 44 | n/a | "hooktestpackage.newabs": (False, test2_newabs_co), |
|---|
| 45 | n/a | "hooktestpackage.newrel": (False, test2_newrel_co), |
|---|
| 46 | n/a | "hooktestpackage.sub.subber.subest": (True, test2_deeprel_co), |
|---|
| 47 | n/a | "hooktestpackage.futrel": (False, test2_futrel_co), |
|---|
| 48 | n/a | "sub": (False, test_co), |
|---|
| 49 | n/a | "reloadmodule": (False, test_co), |
|---|
| 50 | n/a | } |
|---|
| 51 | n/a | |
|---|
| 52 | n/a | def __init__(self, path=test_path): |
|---|
| 53 | n/a | if path != test_path: |
|---|
| 54 | n/a | # if our class is on sys.path_hooks, we must raise |
|---|
| 55 | n/a | # ImportError for any path item that we can't handle. |
|---|
| 56 | n/a | raise ImportError |
|---|
| 57 | n/a | self.path = path |
|---|
| 58 | n/a | |
|---|
| 59 | n/a | def _get__path__(self): |
|---|
| 60 | n/a | raise NotImplementedError |
|---|
| 61 | n/a | |
|---|
| 62 | n/a | def find_module(self, fullname, path=None): |
|---|
| 63 | n/a | if fullname in self.modules: |
|---|
| 64 | n/a | return self |
|---|
| 65 | n/a | else: |
|---|
| 66 | n/a | return None |
|---|
| 67 | n/a | |
|---|
| 68 | n/a | def load_module(self, fullname): |
|---|
| 69 | n/a | ispkg, code = self.modules[fullname] |
|---|
| 70 | n/a | mod = sys.modules.setdefault(fullname,imp.new_module(fullname)) |
|---|
| 71 | n/a | mod.__file__ = "<%s>" % self.__class__.__name__ |
|---|
| 72 | n/a | mod.__loader__ = self |
|---|
| 73 | n/a | if ispkg: |
|---|
| 74 | n/a | mod.__path__ = self._get__path__() |
|---|
| 75 | n/a | exec(code, mod.__dict__) |
|---|
| 76 | n/a | return mod |
|---|
| 77 | n/a | |
|---|
| 78 | n/a | |
|---|
| 79 | n/a | class MetaImporter(TestImporter): |
|---|
| 80 | n/a | def _get__path__(self): |
|---|
| 81 | n/a | return [] |
|---|
| 82 | n/a | |
|---|
| 83 | n/a | class PathImporter(TestImporter): |
|---|
| 84 | n/a | def _get__path__(self): |
|---|
| 85 | n/a | return [self.path] |
|---|
| 86 | n/a | |
|---|
| 87 | n/a | |
|---|
| 88 | n/a | class ImportBlocker: |
|---|
| 89 | n/a | """Place an ImportBlocker instance on sys.meta_path and you |
|---|
| 90 | n/a | can be sure the modules you specified can't be imported, even |
|---|
| 91 | n/a | if it's a builtin.""" |
|---|
| 92 | n/a | def __init__(self, *namestoblock): |
|---|
| 93 | n/a | self.namestoblock = dict.fromkeys(namestoblock) |
|---|
| 94 | n/a | def find_module(self, fullname, path=None): |
|---|
| 95 | n/a | if fullname in self.namestoblock: |
|---|
| 96 | n/a | return self |
|---|
| 97 | n/a | return None |
|---|
| 98 | n/a | def load_module(self, fullname): |
|---|
| 99 | n/a | raise ImportError("I dare you") |
|---|
| 100 | n/a | |
|---|
| 101 | n/a | |
|---|
| 102 | n/a | class ImpWrapper: |
|---|
| 103 | n/a | |
|---|
| 104 | n/a | def __init__(self, path=None): |
|---|
| 105 | n/a | if path is not None and not os.path.isdir(path): |
|---|
| 106 | n/a | raise ImportError |
|---|
| 107 | n/a | self.path = path |
|---|
| 108 | n/a | |
|---|
| 109 | n/a | def find_module(self, fullname, path=None): |
|---|
| 110 | n/a | subname = fullname.split(".")[-1] |
|---|
| 111 | n/a | if subname != fullname and self.path is None: |
|---|
| 112 | n/a | return None |
|---|
| 113 | n/a | if self.path is None: |
|---|
| 114 | n/a | path = None |
|---|
| 115 | n/a | else: |
|---|
| 116 | n/a | path = [self.path] |
|---|
| 117 | n/a | try: |
|---|
| 118 | n/a | file, filename, stuff = imp.find_module(subname, path) |
|---|
| 119 | n/a | except ImportError: |
|---|
| 120 | n/a | return None |
|---|
| 121 | n/a | return ImpLoader(file, filename, stuff) |
|---|
| 122 | n/a | |
|---|
| 123 | n/a | |
|---|
| 124 | n/a | class ImpLoader: |
|---|
| 125 | n/a | |
|---|
| 126 | n/a | def __init__(self, file, filename, stuff): |
|---|
| 127 | n/a | self.file = file |
|---|
| 128 | n/a | self.filename = filename |
|---|
| 129 | n/a | self.stuff = stuff |
|---|
| 130 | n/a | |
|---|
| 131 | n/a | def load_module(self, fullname): |
|---|
| 132 | n/a | mod = imp.load_module(fullname, self.file, self.filename, self.stuff) |
|---|
| 133 | n/a | if self.file: |
|---|
| 134 | n/a | self.file.close() |
|---|
| 135 | n/a | mod.__loader__ = self # for introspection |
|---|
| 136 | n/a | return mod |
|---|
| 137 | n/a | |
|---|
| 138 | n/a | |
|---|
| 139 | n/a | class ImportHooksBaseTestCase(unittest.TestCase): |
|---|
| 140 | n/a | |
|---|
| 141 | n/a | def setUp(self): |
|---|
| 142 | n/a | self.path = sys.path[:] |
|---|
| 143 | n/a | self.meta_path = sys.meta_path[:] |
|---|
| 144 | n/a | self.path_hooks = sys.path_hooks[:] |
|---|
| 145 | n/a | sys.path_importer_cache.clear() |
|---|
| 146 | n/a | self.modules_before = support.modules_setup() |
|---|
| 147 | n/a | |
|---|
| 148 | n/a | def tearDown(self): |
|---|
| 149 | n/a | sys.path[:] = self.path |
|---|
| 150 | n/a | sys.meta_path[:] = self.meta_path |
|---|
| 151 | n/a | sys.path_hooks[:] = self.path_hooks |
|---|
| 152 | n/a | sys.path_importer_cache.clear() |
|---|
| 153 | n/a | support.modules_cleanup(*self.modules_before) |
|---|
| 154 | n/a | |
|---|
| 155 | n/a | |
|---|
| 156 | n/a | class ImportHooksTestCase(ImportHooksBaseTestCase): |
|---|
| 157 | n/a | |
|---|
| 158 | n/a | def doTestImports(self, importer=None): |
|---|
| 159 | n/a | import hooktestmodule |
|---|
| 160 | n/a | import hooktestpackage |
|---|
| 161 | n/a | import hooktestpackage.sub |
|---|
| 162 | n/a | import hooktestpackage.sub.subber |
|---|
| 163 | n/a | self.assertEqual(hooktestmodule.get_name(), |
|---|
| 164 | n/a | "hooktestmodule") |
|---|
| 165 | n/a | self.assertEqual(hooktestpackage.get_name(), |
|---|
| 166 | n/a | "hooktestpackage") |
|---|
| 167 | n/a | self.assertEqual(hooktestpackage.sub.get_name(), |
|---|
| 168 | n/a | "hooktestpackage.sub") |
|---|
| 169 | n/a | self.assertEqual(hooktestpackage.sub.subber.get_name(), |
|---|
| 170 | n/a | "hooktestpackage.sub.subber") |
|---|
| 171 | n/a | if importer: |
|---|
| 172 | n/a | self.assertEqual(hooktestmodule.__loader__, importer) |
|---|
| 173 | n/a | self.assertEqual(hooktestpackage.__loader__, importer) |
|---|
| 174 | n/a | self.assertEqual(hooktestpackage.sub.__loader__, importer) |
|---|
| 175 | n/a | self.assertEqual(hooktestpackage.sub.subber.__loader__, importer) |
|---|
| 176 | n/a | |
|---|
| 177 | n/a | TestImporter.modules['reloadmodule'] = (False, test_co) |
|---|
| 178 | n/a | import reloadmodule |
|---|
| 179 | n/a | self.assertFalse(hasattr(reloadmodule,'reloaded')) |
|---|
| 180 | n/a | |
|---|
| 181 | n/a | import hooktestpackage.newrel |
|---|
| 182 | n/a | self.assertEqual(hooktestpackage.newrel.get_name(), |
|---|
| 183 | n/a | "hooktestpackage.newrel") |
|---|
| 184 | n/a | self.assertEqual(hooktestpackage.newrel.sub, |
|---|
| 185 | n/a | hooktestpackage.sub) |
|---|
| 186 | n/a | |
|---|
| 187 | n/a | import hooktestpackage.sub.subber.subest as subest |
|---|
| 188 | n/a | self.assertEqual(subest.get_name(), |
|---|
| 189 | n/a | "hooktestpackage.sub.subber.subest") |
|---|
| 190 | n/a | self.assertEqual(subest.sub, |
|---|
| 191 | n/a | hooktestpackage.sub) |
|---|
| 192 | n/a | |
|---|
| 193 | n/a | import hooktestpackage.futrel |
|---|
| 194 | n/a | self.assertEqual(hooktestpackage.futrel.get_name(), |
|---|
| 195 | n/a | "hooktestpackage.futrel") |
|---|
| 196 | n/a | self.assertEqual(hooktestpackage.futrel.sub, |
|---|
| 197 | n/a | hooktestpackage.sub) |
|---|
| 198 | n/a | |
|---|
| 199 | n/a | import sub |
|---|
| 200 | n/a | self.assertEqual(sub.get_name(), "sub") |
|---|
| 201 | n/a | |
|---|
| 202 | n/a | import hooktestpackage.oldabs |
|---|
| 203 | n/a | self.assertEqual(hooktestpackage.oldabs.get_name(), |
|---|
| 204 | n/a | "hooktestpackage.oldabs") |
|---|
| 205 | n/a | self.assertEqual(hooktestpackage.oldabs.sub, sub) |
|---|
| 206 | n/a | |
|---|
| 207 | n/a | import hooktestpackage.newabs |
|---|
| 208 | n/a | self.assertEqual(hooktestpackage.newabs.get_name(), |
|---|
| 209 | n/a | "hooktestpackage.newabs") |
|---|
| 210 | n/a | self.assertEqual(hooktestpackage.newabs.sub, sub) |
|---|
| 211 | n/a | |
|---|
| 212 | n/a | def testMetaPath(self): |
|---|
| 213 | n/a | i = MetaImporter() |
|---|
| 214 | n/a | sys.meta_path.append(i) |
|---|
| 215 | n/a | self.doTestImports(i) |
|---|
| 216 | n/a | |
|---|
| 217 | n/a | def testPathHook(self): |
|---|
| 218 | n/a | sys.path_hooks.insert(0, PathImporter) |
|---|
| 219 | n/a | sys.path.append(test_path) |
|---|
| 220 | n/a | self.doTestImports() |
|---|
| 221 | n/a | |
|---|
| 222 | n/a | def testBlocker(self): |
|---|
| 223 | n/a | mname = "exceptions" # an arbitrary harmless builtin module |
|---|
| 224 | n/a | support.unload(mname) |
|---|
| 225 | n/a | sys.meta_path.append(ImportBlocker(mname)) |
|---|
| 226 | n/a | self.assertRaises(ImportError, __import__, mname) |
|---|
| 227 | n/a | |
|---|
| 228 | n/a | def testImpWrapper(self): |
|---|
| 229 | n/a | i = ImpWrapper() |
|---|
| 230 | n/a | sys.meta_path.append(i) |
|---|
| 231 | n/a | sys.path_hooks.insert(0, ImpWrapper) |
|---|
| 232 | n/a | mnames = ( |
|---|
| 233 | n/a | "colorsys", "urllib.parse", "distutils.core", "sys", |
|---|
| 234 | n/a | ) |
|---|
| 235 | n/a | for mname in mnames: |
|---|
| 236 | n/a | parent = mname.split(".")[0] |
|---|
| 237 | n/a | for n in list(sys.modules): |
|---|
| 238 | n/a | if n.startswith(parent): |
|---|
| 239 | n/a | del sys.modules[n] |
|---|
| 240 | n/a | for mname in mnames: |
|---|
| 241 | n/a | m = __import__(mname, globals(), locals(), ["__dummy__"]) |
|---|
| 242 | n/a | # to make sure we actually handled the import |
|---|
| 243 | n/a | self.assertTrue(hasattr(m, "__loader__")) |
|---|
| 244 | n/a | |
|---|
| 245 | n/a | |
|---|
| 246 | n/a | def test_main(): |
|---|
| 247 | n/a | support.run_unittest(ImportHooksTestCase) |
|---|
| 248 | n/a | |
|---|
| 249 | n/a | if __name__ == "__main__": |
|---|
| 250 | n/a | test_main() |
|---|