test loadmodule for builtin modules def setupself self verification name errno package loader self machinery builtinimporter def verifyself module common case builtin modules cannot be a package no way to force an import failure test that the same module is used in a reload using the name of a module already imported but not a builtin should still fail tests for inspectloader methods for builtinimporter def testgetcodeself there is no code object result self machinery builtinimporter getcodeutil builtins goodname self assertisnoneresult def testgetsourceself there is no source result self machinery builtinimporter getsourceutil builtins goodname self assertisnoneresult def testispackageself cannot be a package result self machinery builtinimporter ispackageutil builtins goodname self assertfalseresult unittest skipifutil builtins badname is none all modules are built in def testnotbuiltinself modules not builtin should raise importerror for methname in getcode getsource ispackage method getattrself machinery builtinimporter methname with self assertraisesimporterror as cm methodutil builtins badname frozeninspectloadertests sourceinspectloadertests util testbothinspectloadertests machinerymachinery if name main unittest main test load_module for built in modules verify that the module matches against what it should have common case built in modules cannot be a package no way to force an import failure test that the same module is used in a reload using the name of a module already imported but not a built in should still fail tests for inspectloader methods for builtinimporter there is no code object there is no source cannot be a package modules not built in should raise importerror
from test.test_importlib import abc, util

machinery = util.import_importlib('importlib.machinery')

import sys
import types
import unittest
import warnings

@unittest.skipIf(util.BUILTINS.good_name is None, 'no reasonable builtin module')
class LoaderTests(abc.LoaderTests):

    

    def setUp(self):
        self.verification = {'__name__': 'errno', '__package__': '',
                             '__loader__': self.machinery.BuiltinImporter}

    def verify(self, module):
        
        self.assertIsInstance(module, types.ModuleType)
        for attr, value in self.verification.items():
            self.assertEqual(getattr(module, attr), value)
        self.assertIn(module.__name__, sys.modules)

    def load_module(self, name):
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", DeprecationWarning)
            return self.machinery.BuiltinImporter.load_module(name)

    def test_module(self):
        
        with util.uncache(util.BUILTINS.good_name):
            module = self.load_module(util.BUILTINS.good_name)
            self.verify(module)

    
    test_package = test_lacking_parent = None

    
    test_state_after_failure = None

    def test_module_reuse(self):
        
        with util.uncache(util.BUILTINS.good_name):
            module1 = self.load_module(util.BUILTINS.good_name)
            module2 = self.load_module(util.BUILTINS.good_name)
            self.assertIs(module1, module2)

    def test_unloadable(self):
        name = 'dssdsdfff'
        assert name not in sys.builtin_module_names
        with self.assertRaises(ImportError) as cm:
            self.load_module(name)
        self.assertEqual(cm.exception.name, name)

    def test_already_imported(self):
        
        
        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
        with self.assertRaises(ImportError) as cm:
            self.load_module(module_name)
        self.assertEqual(cm.exception.name, module_name)


(Frozen_LoaderTests,
 Source_LoaderTests
 ) = util.test_both(LoaderTests, machinery=machinery)


@unittest.skipIf(util.BUILTINS.good_name is None, 'no reasonable builtin module')
class InspectLoaderTests:

    

    def test_get_code(self):
        
        result = self.machinery.BuiltinImporter.get_code(util.BUILTINS.good_name)
        self.assertIsNone(result)

    def test_get_source(self):
        
        result = self.machinery.BuiltinImporter.get_source(util.BUILTINS.good_name)
        self.assertIsNone(result)

    def test_is_package(self):
        
        result = self.machinery.BuiltinImporter.is_package(util.BUILTINS.good_name)
        self.assertFalse(result)

    @unittest.skipIf(util.BUILTINS.bad_name is None, 'all modules are built in')
    def test_not_builtin(self):
        
        for meth_name in ('get_code', 'get_source', 'is_package'):
            method = getattr(self.machinery.BuiltinImporter, meth_name)
        with self.assertRaises(ImportError) as cm:
            method(util.BUILTINS.bad_name)


(Frozen_InspectLoaderTests,
 Source_InspectLoaderTests
 ) = util.test_both(InspectLoaderTests, machinery=machinery)


if __name__ == '__main__':
    unittest.main()