ยปCore Development>Code coverage>Lib/test/test_importlib/import_/test_packages.py

Python code coverage for Lib/test/test_importlib/import_/test_packages.py

#countcontent
1n/afrom .. import util
2n/aimport sys
3n/aimport unittest
4n/afrom test import support
5n/a
6n/a
7n/aclass ParentModuleTests:
8n/a
9n/a """Importing a submodule should import the parent modules."""
10n/a
11n/a def test_import_parent(self):
12n/a with util.mock_spec('pkg.__init__', 'pkg.module') as mock:
13n/a with util.import_state(meta_path=[mock]):
14n/a module = self.__import__('pkg.module')
15n/a self.assertIn('pkg', sys.modules)
16n/a
17n/a def test_bad_parent(self):
18n/a with util.mock_spec('pkg.module') as mock:
19n/a with util.import_state(meta_path=[mock]):
20n/a with self.assertRaises(ImportError) as cm:
21n/a self.__import__('pkg.module')
22n/a self.assertEqual(cm.exception.name, 'pkg')
23n/a
24n/a def test_raising_parent_after_importing_child(self):
25n/a def __init__():
26n/a import pkg.module
27n/a 1/0
28n/a mock = util.mock_spec('pkg.__init__', 'pkg.module',
29n/a module_code={'pkg': __init__})
30n/a with mock:
31n/a with util.import_state(meta_path=[mock]):
32n/a with self.assertRaises(ZeroDivisionError):
33n/a self.__import__('pkg')
34n/a self.assertNotIn('pkg', sys.modules)
35n/a self.assertIn('pkg.module', sys.modules)
36n/a with self.assertRaises(ZeroDivisionError):
37n/a self.__import__('pkg.module')
38n/a self.assertNotIn('pkg', sys.modules)
39n/a self.assertIn('pkg.module', sys.modules)
40n/a
41n/a def test_raising_parent_after_relative_importing_child(self):
42n/a def __init__():
43n/a from . import module
44n/a 1/0
45n/a mock = util.mock_spec('pkg.__init__', 'pkg.module',
46n/a module_code={'pkg': __init__})
47n/a with mock:
48n/a with util.import_state(meta_path=[mock]):
49n/a with self.assertRaises((ZeroDivisionError, ImportError)):
50n/a # This raises ImportError on the "from . import module"
51n/a # line, not sure why.
52n/a self.__import__('pkg')
53n/a self.assertNotIn('pkg', sys.modules)
54n/a with self.assertRaises((ZeroDivisionError, ImportError)):
55n/a self.__import__('pkg.module')
56n/a self.assertNotIn('pkg', sys.modules)
57n/a # XXX False
58n/a #self.assertIn('pkg.module', sys.modules)
59n/a
60n/a def test_raising_parent_after_double_relative_importing_child(self):
61n/a def __init__():
62n/a from ..subpkg import module
63n/a 1/0
64n/a mock = util.mock_spec('pkg.__init__', 'pkg.subpkg.__init__',
65n/a 'pkg.subpkg.module',
66n/a module_code={'pkg.subpkg': __init__})
67n/a with mock:
68n/a with util.import_state(meta_path=[mock]):
69n/a with self.assertRaises((ZeroDivisionError, ImportError)):
70n/a # This raises ImportError on the "from ..subpkg import module"
71n/a # line, not sure why.
72n/a self.__import__('pkg.subpkg')
73n/a self.assertNotIn('pkg.subpkg', sys.modules)
74n/a with self.assertRaises((ZeroDivisionError, ImportError)):
75n/a self.__import__('pkg.subpkg.module')
76n/a self.assertNotIn('pkg.subpkg', sys.modules)
77n/a # XXX False
78n/a #self.assertIn('pkg.subpkg.module', sys.modules)
79n/a
80n/a def test_module_not_package(self):
81n/a # Try to import a submodule from a non-package should raise ImportError.
82n/a assert not hasattr(sys, '__path__')
83n/a with self.assertRaises(ImportError) as cm:
84n/a self.__import__('sys.no_submodules_here')
85n/a self.assertEqual(cm.exception.name, 'sys.no_submodules_here')
86n/a
87n/a def test_module_not_package_but_side_effects(self):
88n/a # If a module injects something into sys.modules as a side-effect, then
89n/a # pick up on that fact.
90n/a name = 'mod'
91n/a subname = name + '.b'
92n/a def module_injection():
93n/a sys.modules[subname] = 'total bunk'
94n/a mock_spec = util.mock_spec('mod',
95n/a module_code={'mod': module_injection})
96n/a with mock_spec as mock:
97n/a with util.import_state(meta_path=[mock]):
98n/a try:
99n/a submodule = self.__import__(subname)
100n/a finally:
101n/a support.unload(subname)
102n/a
103n/a
104n/a(Frozen_ParentTests,
105n/a Source_ParentTests
106n/a ) = util.test_both(ParentModuleTests, __import__=util.__import__)
107n/a
108n/a
109n/aif __name__ == '__main__':
110n/a unittest.main()