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