2009-01-31 20:37:13 -04:00
|
|
|
from .. import abc
|
2009-02-01 00:00:05 -04:00
|
|
|
from .. import util
|
2009-03-14 22:41:33 -03:00
|
|
|
from . import util as builtin_util
|
2009-01-17 20:24:28 -04:00
|
|
|
|
2013-10-25 13:44:36 -03:00
|
|
|
frozen_machinery, source_machinery = util.import_importlib('importlib.machinery')
|
|
|
|
|
2009-01-17 20:24:28 -04:00
|
|
|
import sys
|
|
|
|
import types
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
|
2013-10-25 13:44:36 -03:00
|
|
|
class LoaderTests(abc.LoaderTests):
|
2009-01-17 20:24:28 -04:00
|
|
|
|
|
|
|
"""Test load_module() for built-in modules."""
|
|
|
|
|
2013-10-25 13:44:36 -03:00
|
|
|
def setUp(self):
|
|
|
|
self.verification = {'__name__': 'errno', '__package__': '',
|
|
|
|
'__loader__': self.machinery.BuiltinImporter}
|
2009-01-17 20:24:28 -04:00
|
|
|
|
|
|
|
def verify(self, module):
|
|
|
|
"""Verify that the module matches against what it should have."""
|
2012-06-27 16:26:26 -03:00
|
|
|
self.assertIsInstance(module, types.ModuleType)
|
2009-01-17 20:24:28 -04:00
|
|
|
for attr, value in self.verification.items():
|
|
|
|
self.assertEqual(getattr(module, attr), value)
|
2012-06-27 16:26:26 -03:00
|
|
|
self.assertIn(module.__name__, sys.modules)
|
2009-01-17 20:24:28 -04:00
|
|
|
|
2013-10-25 13:44:36 -03:00
|
|
|
def load_module(self, name):
|
|
|
|
return self.machinery.BuiltinImporter.load_module(name)
|
2009-01-17 20:24:28 -04:00
|
|
|
|
2009-01-31 20:37:13 -04:00
|
|
|
def test_module(self):
|
2009-01-17 20:24:28 -04:00
|
|
|
# Common case.
|
2009-03-14 22:41:33 -03:00
|
|
|
with util.uncache(builtin_util.NAME):
|
|
|
|
module = self.load_module(builtin_util.NAME)
|
2009-01-17 20:24:28 -04:00
|
|
|
self.verify(module)
|
|
|
|
|
2013-11-22 12:05:39 -04:00
|
|
|
# Built-in modules cannot be a package.
|
|
|
|
test_package = test_lacking_parent = None
|
2009-01-31 20:37:13 -04:00
|
|
|
|
2013-11-22 12:05:39 -04:00
|
|
|
# No way to force an import failure.
|
|
|
|
test_state_after_failure = None
|
2009-01-31 20:37:13 -04:00
|
|
|
|
|
|
|
def test_module_reuse(self):
|
|
|
|
# Test that the same module is used in a reload.
|
2009-03-14 22:41:33 -03:00
|
|
|
with util.uncache(builtin_util.NAME):
|
|
|
|
module1 = self.load_module(builtin_util.NAME)
|
|
|
|
module2 = self.load_module(builtin_util.NAME)
|
2012-06-27 16:26:26 -03:00
|
|
|
self.assertIs(module1, module2)
|
2009-01-31 20:37:13 -04:00
|
|
|
|
|
|
|
def test_unloadable(self):
|
2009-01-17 20:24:28 -04:00
|
|
|
name = 'dssdsdfff'
|
|
|
|
assert name not in sys.builtin_module_names
|
2012-04-12 22:09:01 -03:00
|
|
|
with self.assertRaises(ImportError) as cm:
|
2009-08-27 20:49:21 -03:00
|
|
|
self.load_module(name)
|
2012-04-12 22:09:01 -03:00
|
|
|
self.assertEqual(cm.exception.name, name)
|
2009-01-17 20:24:28 -04:00
|
|
|
|
|
|
|
def test_already_imported(self):
|
|
|
|
# Using the name of a module already imported but not a built-in should
|
|
|
|
# still fail.
|
2013-11-22 15:38:09 -04:00
|
|
|
module_name = 'builtin_reload_test'
|
|
|
|
assert module_name not in sys.builtin_module_names
|
|
|
|
with util.uncache(module_name):
|
|
|
|
module = types.ModuleType(module_name)
|
|
|
|
sys.modules[module_name] = module
|
2012-04-12 22:09:01 -03:00
|
|
|
with self.assertRaises(ImportError) as cm:
|
2013-11-22 15:38:09 -04:00
|
|
|
self.load_module(module_name)
|
|
|
|
self.assertEqual(cm.exception.name, module_name)
|
2013-10-25 13:44:36 -03:00
|
|
|
|
|
|
|
|
|
|
|
Frozen_LoaderTests, Source_LoaderTests = util.test_both(LoaderTests,
|
|
|
|
machinery=[frozen_machinery, source_machinery])
|
2009-01-17 20:24:28 -04:00
|
|
|
|
|
|
|
|
2013-10-25 13:44:36 -03:00
|
|
|
class InspectLoaderTests:
|
2009-03-14 22:41:33 -03:00
|
|
|
|
|
|
|
"""Tests for InspectLoader methods for BuiltinImporter."""
|
|
|
|
|
|
|
|
def test_get_code(self):
|
|
|
|
# There is no code object.
|
2013-10-25 13:44:36 -03:00
|
|
|
result = self.machinery.BuiltinImporter.get_code(builtin_util.NAME)
|
2012-06-28 07:15:01 -03:00
|
|
|
self.assertIsNone(result)
|
2009-03-14 22:41:33 -03:00
|
|
|
|
|
|
|
def test_get_source(self):
|
|
|
|
# There is no source.
|
2013-10-25 13:44:36 -03:00
|
|
|
result = self.machinery.BuiltinImporter.get_source(builtin_util.NAME)
|
2012-06-28 07:15:01 -03:00
|
|
|
self.assertIsNone(result)
|
2009-03-14 22:41:33 -03:00
|
|
|
|
|
|
|
def test_is_package(self):
|
|
|
|
# Cannot be a package.
|
2013-10-25 13:44:36 -03:00
|
|
|
result = self.machinery.BuiltinImporter.is_package(builtin_util.NAME)
|
2009-06-30 20:06:06 -03:00
|
|
|
self.assertTrue(not result)
|
2009-03-14 22:41:33 -03:00
|
|
|
|
|
|
|
def test_not_builtin(self):
|
|
|
|
# Modules not built-in should raise ImportError.
|
|
|
|
for meth_name in ('get_code', 'get_source', 'is_package'):
|
2013-10-25 13:44:36 -03:00
|
|
|
method = getattr(self.machinery.BuiltinImporter, meth_name)
|
2012-04-12 22:09:01 -03:00
|
|
|
with self.assertRaises(ImportError) as cm:
|
2009-08-27 20:49:21 -03:00
|
|
|
method(builtin_util.BAD_NAME)
|
2012-04-12 22:09:01 -03:00
|
|
|
self.assertRaises(builtin_util.BAD_NAME)
|
2009-03-14 22:41:33 -03:00
|
|
|
|
2013-10-25 13:44:36 -03:00
|
|
|
Frozen_InspectLoaderTests, Source_InspectLoaderTests = util.test_both(
|
|
|
|
InspectLoaderTests,
|
|
|
|
machinery=[frozen_machinery, source_machinery])
|
2009-01-17 20:24:28 -04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2013-10-25 13:44:36 -03:00
|
|
|
unittest.main()
|