2009-01-17 20:24:28 -04:00
|
|
|
import importlib
|
2009-01-22 18:43:07 -04:00
|
|
|
from importlib import machinery
|
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
|
|
|
|
|
|
|
import sys
|
|
|
|
import types
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
|
2009-01-31 20:37:13 -04:00
|
|
|
class LoaderTests(abc.LoaderTests):
|
2009-01-17 20:24:28 -04:00
|
|
|
|
|
|
|
"""Test load_module() for built-in modules."""
|
|
|
|
|
2009-03-10 02:17:37 -03:00
|
|
|
verification = {'__name__': 'errno', '__package__': '',
|
|
|
|
'__loader__': 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
|
|
|
|
|
|
|
load_module = staticmethod(lambda name:
|
2009-01-22 18:43:07 -04:00
|
|
|
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)
|
|
|
|
|
2009-01-31 20:37:13 -04:00
|
|
|
def test_package(self):
|
|
|
|
# Built-in modules cannot be a package.
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_lacking_parent(self):
|
|
|
|
# Built-in modules cannot be a package.
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_state_after_failure(self):
|
|
|
|
# Not way to force an imoprt failure.
|
|
|
|
pass
|
|
|
|
|
|
|
|
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.
|
|
|
|
assert hasattr(importlib, '__file__') # Not a built-in.
|
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('importlib')
|
2012-04-12 22:09:01 -03:00
|
|
|
self.assertEqual(cm.exception.name, 'importlib')
|
2009-01-17 20:24:28 -04:00
|
|
|
|
|
|
|
|
2009-03-14 22:41:33 -03:00
|
|
|
class InspectLoaderTests(unittest.TestCase):
|
|
|
|
|
|
|
|
"""Tests for InspectLoader methods for BuiltinImporter."""
|
|
|
|
|
|
|
|
def test_get_code(self):
|
|
|
|
# There is no code object.
|
|
|
|
result = 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.
|
|
|
|
result = 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.
|
|
|
|
result = 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'):
|
|
|
|
method = getattr(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
|
|
|
|
|
|
|
|
|
|
|
|
2009-01-17 20:24:28 -04:00
|
|
|
def test_main():
|
|
|
|
from test.support import run_unittest
|
2009-03-14 22:41:33 -03:00
|
|
|
run_unittest(LoaderTests, InspectLoaderTests)
|
2009-01-17 20:24:28 -04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
test_main()
|