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
|
|
|
|
2013-10-25 16:39:02 -03:00
|
|
|
machinery = util.import_importlib('importlib.machinery')
|
|
|
|
|
2012-08-10 14:47:54 -03:00
|
|
|
import os.path
|
2009-01-17 20:24:28 -04:00
|
|
|
import sys
|
2013-11-22 12:05:39 -04:00
|
|
|
import types
|
2009-01-17 20:24:28 -04:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
|
2013-10-25 16:39:02 -03: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):
|
2013-10-25 16:39:02 -03:00
|
|
|
self.loader = self.machinery.ExtensionFileLoader(ext_util.NAME,
|
|
|
|
ext_util.FILEPATH)
|
2012-05-11 15:48:41 -03:00
|
|
|
|
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')
|
|
|
|
|
2014-01-04 18:06:49 -04:00
|
|
|
def test_equality(self):
|
|
|
|
other = self.machinery.ExtensionFileLoader(ext_util.NAME,
|
|
|
|
ext_util.FILEPATH)
|
|
|
|
self.assertEqual(self.loader, other)
|
|
|
|
|
|
|
|
def test_inequality(self):
|
|
|
|
other = self.machinery.ExtensionFileLoader('_' + ext_util.NAME,
|
|
|
|
ext_util.FILEPATH)
|
|
|
|
self.assertNotEqual(self.loader, other)
|
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__,
|
2013-10-25 16:39:02 -03:00
|
|
|
self.machinery.ExtensionFileLoader)
|
2009-01-17 20:24:28 -04:00
|
|
|
|
2013-11-22 12:05:39 -04:00
|
|
|
# No extension module as __init__ available for testing.
|
|
|
|
test_package = None
|
2009-01-31 20:49:41 -04:00
|
|
|
|
2013-11-22 12:05:39 -04:00
|
|
|
# No extension module in a package available for testing.
|
|
|
|
test_lacking_parent = None
|
2009-01-31 20:49:41 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
2013-11-22 12:05:39 -04:00
|
|
|
# No easy way to trigger a failure after a successful import.
|
|
|
|
test_state_after_failure = None
|
2009-01-31 20:49:41 -04:00
|
|
|
|
|
|
|
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))
|
2013-10-25 16:39:02 -03:00
|
|
|
for suffix in self.machinery.EXTENSION_SUFFIXES:
|
2012-08-10 14:47:54 -03:00
|
|
|
path = os.path.join('some', 'path', 'pkg', '__init__' + suffix)
|
2013-10-25 16:39:02 -03:00
|
|
|
loader = self.machinery.ExtensionFileLoader('pkg', path)
|
2012-08-10 14:47:54 -03:00
|
|
|
self.assertTrue(loader.is_package('pkg'))
|
|
|
|
|
2013-10-25 16:39:02 -03:00
|
|
|
Frozen_LoaderTests, Source_LoaderTests = util.test_both(
|
|
|
|
LoaderTests, machinery=machinery)
|
2009-01-17 20:24:28 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2013-10-25 16:39:02 -03:00
|
|
|
unittest.main()
|