from test
import test_support
reload_src
= test_src
+"""\
test_co
= compile(test_src
, "<???>", "exec")
reload_co
= compile(reload_src
, "<???>", "exec")
test_path
= "!!!_test_!!!"
"""Importer that only tracks attempted imports."""
def find_module(self
, fullname
, path
=None):
self
.imports
.append(fullname
)
"hooktestmodule": (False, test_co
),
"hooktestpackage": (True, test_co
),
"hooktestpackage.sub": (True, test_co
),
"hooktestpackage.sub.subber": (False, test_co
),
"reloadmodule": (False, test_co
),
def __init__(self
, path
=test_path
):
# if out class is on sys.path_hooks, we must raise
# ImportError for any path item that we can't handle.
raise NotImplementedError
def find_module(self
, fullname
, path
=None):
if fullname
in self
.modules
:
def load_module(self
, fullname
):
ispkg
, code
= self
.modules
[fullname
]
mod
= sys
.modules
.setdefault(fullname
,imp
.new_module(fullname
))
mod
.__file
__ = "<%s>" % self
.__class
__.__name
__
mod
.__path
__ = self
._get
__path
__()
exec code
in mod
.__dict
__
class MetaImporter(TestImporter
):
class PathImporter(TestImporter
):
"""Place an ImportBlocker instance on sys.meta_path and you
can be sure the modules you specified can't be imported, even
def __init__(self
, *namestoblock
):
self
.namestoblock
= dict.fromkeys(namestoblock
)
def find_module(self
, fullname
, path
=None):
if fullname
in self
.namestoblock
:
def load_module(self
, fullname
):
raise ImportError, "I dare you"
def __init__(self
, path
=None):
if path
is not None and not os
.path
.isdir(path
):
def find_module(self
, fullname
, path
=None):
subname
= fullname
.split(".")[-1]
if subname
!= fullname
and self
.path
is None:
file, filename
, stuff
= imp
.find_module(subname
, path
)
return ImpLoader(file, filename
, stuff
)
def __init__(self
, file, filename
, stuff
):
def load_module(self
, fullname
):
mod
= imp
.load_module(fullname
, self
.file, self
.filename
, self
.stuff
)
mod
.__loader
__ = self
# for introspection
class ImportHooksBaseTestCase(unittest
.TestCase
):
self
.meta_path
= sys
.meta_path
[:]
self
.path_hooks
= sys
.path_hooks
[:]
sys
.path_importer_cache
.clear()
self
.tracker
= ImportTracker()
sys
.meta_path
.insert(0, self
.tracker
)
sys
.meta_path
[:] = self
.meta_path
sys
.path_hooks
[:] = self
.path_hooks
sys
.path_importer_cache
.clear()
for fullname
in self
.tracker
.imports
:
if fullname
in sys
.modules
:
del sys
.modules
[fullname
]
class ImportHooksTestCase(ImportHooksBaseTestCase
):
def doTestImports(self
, importer
=None):
import hooktestpackage
.sub
import hooktestpackage
.sub
.subber
self
.assertEqual(hooktestmodule
.get_name(),
self
.assertEqual(hooktestpackage
.get_name(),
self
.assertEqual(hooktestpackage
.sub
.get_name(),
self
.assertEqual(hooktestpackage
.sub
.subber
.get_name(),
"hooktestpackage.sub.subber")
self
.assertEqual(hooktestmodule
.__loader
__, importer
)
self
.assertEqual(hooktestpackage
.__loader
__, importer
)
self
.assertEqual(hooktestpackage
.sub
.__loader
__, importer
)
self
.assertEqual(hooktestpackage
.sub
.subber
.__loader
__, importer
)
TestImporter
.modules
['reloadmodule'] = (False, test_co
)
self
.failIf(hasattr(reloadmodule
,'reloaded'))
TestImporter
.modules
['reloadmodule'] = (False, reload_co
)
self
.failUnless(hasattr(reloadmodule
,'reloaded'))
sys
.path_hooks
.append(PathImporter
)
sys
.path
.append(test_path
)
mname
= "exceptions" # an arbitrary harmless builtin module
sys
.meta_path
.append(ImportBlocker(mname
))
self
.fail("'%s' was not supposed to be importable" % mname
)
def testImpWrapper(self
):
sys
.path_hooks
.append(ImpWrapper
)
mnames
= ("colorsys", "urlparse", "distutils.core", "compiler.misc")
parent
= mname
.split(".")[0]
for n
in sys
.modules
.keys():
m
= __import__(mname
, globals(), locals(), ["__dummy__"])
m
.__loader
__ # to make sure we actually handled the import
test_support
.run_unittest(ImportHooksTestCase
)
if __name__
== "__main__":