5 from test
import test_support
15 test_co
= compile(test_src
, "<???>", "exec")
16 test_path
= "!!!_test_!!!"
20 """Importer that only tracks attempted imports."""
23 def find_module(self
, fullname
, path
=None):
24 self
.imports
.append(fullname
)
31 "hooktestmodule": (False, test_co
),
32 "hooktestpackage": (True, test_co
),
33 "hooktestpackage.sub": (True, test_co
),
34 "hooktestpackage.sub.subber": (False, test_co
),
37 def __init__(self
, path
=test_path
):
39 # if out class is on sys.path_hooks, we must raise
40 # ImportError for any path item that we can't handle.
44 def _get__path__(self
):
45 raise NotImplementedError
47 def find_module(self
, fullname
, path
=None):
48 if fullname
in self
.modules
:
53 def load_module(self
, fullname
):
54 ispkg
, code
= self
.modules
[fullname
]
55 mod
= imp
.new_module(fullname
)
56 sys
.modules
[fullname
] = mod
57 mod
.__file
__ = "<%s>" % self
.__class
__.__name
__
60 mod
.__path
__ = self
._get
__path
__()
61 exec code
in mod
.__dict
__
65 class MetaImporter(TestImporter
):
66 def _get__path__(self
):
69 class PathImporter(TestImporter
):
70 def _get__path__(self
):
75 """Place an ImportBlocker instance on sys.meta_path and you
76 can be sure the modules you specified can't be imported, even
78 def __init__(self
, *namestoblock
):
79 self
.namestoblock
= dict.fromkeys(namestoblock
)
80 def find_module(self
, fullname
, path
=None):
81 if fullname
in self
.namestoblock
:
84 def load_module(self
, fullname
):
85 raise ImportError, "I dare you"
90 def __init__(self
, path
=None):
91 if path
is not None and not os
.path
.isdir(path
):
95 def find_module(self
, fullname
, path
=None):
96 subname
= fullname
.split(".")[-1]
97 if subname
!= fullname
and self
.path
is None:
104 file, filename
, stuff
= imp
.find_module(subname
, path
)
107 return ImpLoader(file, filename
, stuff
)
112 def __init__(self
, file, filename
, stuff
):
114 self
.filename
= filename
117 def load_module(self
, fullname
):
118 mod
= imp
.load_module(fullname
, self
.file, self
.filename
, self
.stuff
)
121 mod
.__loader
__ = self
# for introspection
125 class ImportHooksBaseTestCase(unittest
.TestCase
):
128 self
.path
= sys
.path
[:]
129 self
.meta_path
= sys
.meta_path
[:]
130 self
.path_hooks
= sys
.path_hooks
[:]
131 sys
.path_importer_cache
.clear()
132 self
.tracker
= ImportTracker()
133 sys
.meta_path
.insert(0, self
.tracker
)
136 sys
.path
[:] = self
.path
137 sys
.meta_path
[:] = self
.meta_path
138 sys
.path_hooks
[:] = self
.path_hooks
139 sys
.path_importer_cache
.clear()
140 for fullname
in self
.tracker
.imports
:
141 if fullname
in sys
.modules
:
142 del sys
.modules
[fullname
]
145 class ImportHooksTestCase(ImportHooksBaseTestCase
):
147 def doTestImports(self
, importer
=None):
148 import hooktestmodule
149 import hooktestpackage
150 import hooktestpackage
.sub
151 import hooktestpackage
.sub
.subber
152 self
.assertEqual(hooktestmodule
.get_name(),
154 self
.assertEqual(hooktestpackage
.get_name(),
156 self
.assertEqual(hooktestpackage
.sub
.get_name(),
157 "hooktestpackage.sub")
158 self
.assertEqual(hooktestpackage
.sub
.subber
.get_name(),
159 "hooktestpackage.sub.subber")
161 self
.assertEqual(hooktestmodule
.__loader
__, importer
)
162 self
.assertEqual(hooktestpackage
.__loader
__, importer
)
163 self
.assertEqual(hooktestpackage
.sub
.__loader
__, importer
)
164 self
.assertEqual(hooktestpackage
.sub
.subber
.__loader
__, importer
)
166 def testMetaPath(self
):
168 sys
.meta_path
.append(i
)
169 self
.doTestImports(i
)
171 def testPathHook(self
):
172 sys
.path_hooks
.append(PathImporter
)
173 sys
.path
.append(test_path
)
176 def testBlocker(self
):
177 mname
= "exceptions" # an arbitrary harmless builtin module
178 if mname
in sys
.modules
:
179 del sys
.modules
[mname
]
180 sys
.meta_path
.append(ImportBlocker(mname
))
186 self
.fail("'%s' was not supposed to be importable" % mname
)
188 def testImpWrapper(self
):
190 sys
.meta_path
.append(i
)
191 sys
.path_hooks
.append(ImpWrapper
)
192 mnames
= ("colorsys", "urlparse", "distutils.core", "compiler.misc")
194 parent
= mname
.split(".")[0]
195 for n
in sys
.modules
.keys():
196 if n
.startswith(parent
):
199 m
= __import__(mname
, globals(), locals(), ["__dummy__"])
200 m
.__loader
__ # to make sure we actually handled the import
203 test_support
.run_unittest(ImportHooksTestCase
)
205 if __name__
== "__main__":