importing a submodule should import the parent modules def testimportparentself with util mockspec pkg init pkg module as mock with util importstatemetapathmock module self import pkg module self assertin pkg sys modules def testbadparentself with util mockspec pkg module as mock with util importstatemetapathmock with self assertraisesimporterror as cm self import pkg module self assertequalcm exception name pkg def testraisingparentafterimportingchildself def init import pkg module 10 mock util mockspec pkg init pkg module modulecode pkg init with mock with util importstatemetapathmock with self assertraiseszerodivisionerror self import pkg self assertnotin pkg sys modules self assertin pkg module sys modules with self assertraiseszerodivisionerror self import pkg module self assertnotin pkg sys modules self assertin pkg module sys modules def testraisingparentafterrelativeimportingchildself def init from import module 10 mock util mockspec pkg init pkg module modulecode pkg init with mock with util importstatemetapathmock with self assertraiseszerodivisionerror importerror this raises importerror on the from import module line not sure why self import pkg self assertnotin pkg sys modules with self assertraiseszerodivisionerror importerror self import pkg module self assertnotin pkg sys modules xxx false self assertin pkg module sys modules def testraisingparentafterdoublerelativeimportingchildself def init from subpkg import module 10 mock util mockspec pkg init pkg subpkg init pkg subpkg module modulecode pkg subpkg init with mock with util importstatemetapathmock with self assertraiseszerodivisionerror importerror this raises importerror on the from subpkg import module line not sure why self import pkg subpkg self assertnotin pkg subpkg sys modules with self assertraiseszerodivisionerror importerror self import pkg subpkg module self assertnotin pkg subpkg sys modules xxx false self assertin pkg subpkg module sys modules def testmodulenotpackageself try to import a submodule from a nonpackage should raise importerror assert not hasattrsys path with self assertraisesimporterror as cm self import sys nosubmoduleshere self assertequalcm exception name sys nosubmoduleshere def testmodulenotpackagebutsideeffectsself if a module injects something into sys modules as a sideeffect then pick up on that fact name mod subname name b def moduleinjection sys modulessubname total bunk mockspec util mockspec mod modulecode mod moduleinjection with mockspec as mock with util importstatemetapathmock try submodule self importsubname finally importhelper unloadsubname frozenparenttests sourceparenttests util testbothparentmoduletests importutil import if name main unittest main importing a submodule should import the parent modules this raises importerror on the from import module line not sure why xxx false self assertin pkg module sys modules this raises importerror on the from subpkg import module line not sure why xxx false self assertin pkg subpkg module sys modules try to import a submodule from a non package should raise importerror if a module injects something into sys modules as a side effect then pick up on that fact
from test.test_importlib import util
import sys
import unittest
from test.support import import_helper


class ParentModuleTests:

    

    def test_import_parent(self):
        with util.mock_spec('pkg.__init__', 'pkg.module') as mock:
            with util.import_state(meta_path=[mock]):
                module = self.__import__('pkg.module')
                self.assertIn('pkg', sys.modules)

    def test_bad_parent(self):
        with util.mock_spec('pkg.module') as mock:
            with util.import_state(meta_path=[mock]):
                with self.assertRaises(ImportError) as cm:
                    self.__import__('pkg.module')
                self.assertEqual(cm.exception.name, 'pkg')

    def test_raising_parent_after_importing_child(self):
        def __init__():
            import pkg.module
            1/0
        mock = util.mock_spec('pkg.__init__', 'pkg.module',
                                 module_code={'pkg': __init__})
        with mock:
            with util.import_state(meta_path=[mock]):
                with self.assertRaises(ZeroDivisionError):
                    self.__import__('pkg')
                self.assertNotIn('pkg', sys.modules)
                self.assertIn('pkg.module', sys.modules)
                with self.assertRaises(ZeroDivisionError):
                    self.__import__('pkg.module')
                self.assertNotIn('pkg', sys.modules)
                self.assertIn('pkg.module', sys.modules)

    def test_raising_parent_after_relative_importing_child(self):
        def __init__():
            from . import module
            1/0
        mock = util.mock_spec('pkg.__init__', 'pkg.module',
                                 module_code={'pkg': __init__})
        with mock:
            with util.import_state(meta_path=[mock]):
                with self.assertRaises((ZeroDivisionError, ImportError)):
                    
                    
                    self.__import__('pkg')
                self.assertNotIn('pkg', sys.modules)
                with self.assertRaises((ZeroDivisionError, ImportError)):
                    self.__import__('pkg.module')
                self.assertNotIn('pkg', sys.modules)
                
                

    def test_raising_parent_after_double_relative_importing_child(self):
        def __init__():
            from ..subpkg import module
            1/0
        mock = util.mock_spec('pkg.__init__', 'pkg.subpkg.__init__',
                                 'pkg.subpkg.module',
                                 module_code={'pkg.subpkg': __init__})
        with mock:
            with util.import_state(meta_path=[mock]):
                with self.assertRaises((ZeroDivisionError, ImportError)):
                    
                    
                    self.__import__('pkg.subpkg')
                self.assertNotIn('pkg.subpkg', sys.modules)
                with self.assertRaises((ZeroDivisionError, ImportError)):
                    self.__import__('pkg.subpkg.module')
                self.assertNotIn('pkg.subpkg', sys.modules)
                
                

    def test_module_not_package(self):
        
        assert not hasattr(sys, '__path__')
        with self.assertRaises(ImportError) as cm:
            self.__import__('sys.no_submodules_here')
        self.assertEqual(cm.exception.name, 'sys.no_submodules_here')

    def test_module_not_package_but_side_effects(self):
        
        
        name = 'mod'
        subname = name + '.b'
        def module_injection():
            sys.modules[subname] = 'total bunk'
        mock_spec = util.mock_spec('mod',
                                         module_code={'mod': module_injection})
        with mock_spec as mock:
            with util.import_state(meta_path=[mock]):
                try:
                    submodule = self.__import__(subname)
                finally:
                    import_helper.unload(subname)


(Frozen_ParentTests,
 Source_ParentTests
 ) = util.test_both(ParentModuleTests, __import__=util.__import__)


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