2012-05-11 15:48:41 -03:00
|
|
|
from importlib import machinery
|
2009-02-06 22:06:43 -04:00
|
|
|
from . import util as ext_util
|
2009-01-31 20:49:41 -04:00
|
|
|
from .. import abc
|
2009-02-01 00:00:05 -04:00
|
|
|
from .. import util
|
2009-01-17 20:24:28 -04:00
|
|
|
|
2012-08-10 14:47:54 -03:00
|
|
|
import os.path
|
2009-01-17 20:24:28 -04:00
|
|
|
import sys
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
|
2009-01-31 20:49:41 -04:00
|
|
|
class LoaderTests(abc.LoaderTests):
|
2009-01-17 20:24:28 -04:00
|
|
|
|
|
|
|
"""Test load_module() for extension modules."""
|
|
|
|
|
2012-05-11 15:48:41 -03:00
|
|
|
def setUp(self):
|
|
|
|
self.loader = machinery.ExtensionFileLoader(ext_util.NAME,
|
|
|
|
ext_util.FILEPATH)
|
|
|
|
|
2009-01-17 20:24:28 -04:00
|
|
|
def load_module(self, fullname):
|
2012-05-11 15:48:41 -03:00
|
|
|
return self.loader.load_module(fullname)
|
|
|
|
|
|
|
|
def test_load_module_API(self):
|
|
|
|
# Test the default argument for load_module().
|
|
|
|
self.loader.load_module()
|
|
|
|
self.loader.load_module(None)
|
|
|
|
with self.assertRaises(ImportError):
|
|
|
|
self.load_module('XXX')
|
|
|
|
|
2009-01-17 20:24:28 -04:00
|
|
|
|
2009-01-31 20:49:41 -04:00
|
|
|
def test_module(self):
|
2009-02-06 22:06:43 -04:00
|
|
|
with util.uncache(ext_util.NAME):
|
|
|
|
module = self.load_module(ext_util.NAME)
|
|
|
|
for attr, value in [('__name__', ext_util.NAME),
|
|
|
|
('__file__', ext_util.FILEPATH),
|
2009-02-06 21:52:25 -04:00
|
|
|
('__package__', '')]:
|
2009-01-17 20:24:28 -04:00
|
|
|
self.assertEqual(getattr(module, attr), value)
|
2012-06-27 16:26:26 -03:00
|
|
|
self.assertIn(ext_util.NAME, sys.modules)
|
|
|
|
self.assertIsInstance(module.__loader__,
|
|
|
|
machinery.ExtensionFileLoader)
|
2009-01-17 20:24:28 -04:00
|
|
|
|
2009-01-31 20:49:41 -04:00
|
|
|
def test_package(self):
|
2012-08-10 14:47:54 -03:00
|
|
|
# No extension module as __init__ available for testing.
|
2009-01-31 20:49:41 -04:00
|
|
|
pass
|
|
|
|
|
|
|
|
def test_lacking_parent(self):
|
2012-08-10 14:47:54 -03:00
|
|
|
# No extension module in a package available for testing.
|
2009-01-31 20:49:41 -04:00
|
|
|
pass
|
|
|
|
|
|
|
|
def test_module_reuse(self):
|
2009-02-06 22:06:43 -04:00
|
|
|
with util.uncache(ext_util.NAME):
|
|
|
|
module1 = self.load_module(ext_util.NAME)
|
|
|
|
module2 = self.load_module(ext_util.NAME)
|
2012-06-27 16:26:26 -03:00
|
|
|
self.assertIs(module1, module2)
|
2009-01-31 20:49:41 -04:00
|
|
|
|
|
|
|
def test_state_after_failure(self):
|
|
|
|
# No easy way to trigger a failure after a successful import.
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_unloadable(self):
|
2012-04-12 22:09:01 -03:00
|
|
|
name = 'asdfjkl;'
|
|
|
|
with self.assertRaises(ImportError) as cm:
|
|
|
|
self.load_module(name)
|
|
|
|
self.assertEqual(cm.exception.name, name)
|
2009-01-17 20:24:28 -04:00
|
|
|
|
2012-08-10 14:47:54 -03:00
|
|
|
def test_is_package(self):
|
|
|
|
self.assertFalse(self.loader.is_package(ext_util.NAME))
|
|
|
|
for suffix in machinery.EXTENSION_SUFFIXES:
|
|
|
|
path = os.path.join('some', 'path', 'pkg', '__init__' + suffix)
|
|
|
|
loader = machinery.ExtensionFileLoader('pkg', path)
|
|
|
|
self.assertTrue(loader.is_package('pkg'))
|
|
|
|
|
2009-01-17 20:24:28 -04:00
|
|
|
|
|
|
|
def test_main():
|
|
|
|
from test.support import run_unittest
|
|
|
|
run_unittest(LoaderTests)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
test_main()
|