ยปCore Development>Code coverage>Lib/importlib/test/import_/test___package__.py

Python code coverage for Lib/importlib/test/import_/test___package__.py

#countcontent
1n/a"""PEP 366 ("Main module explicit relative imports") specifies the
2n/asemantics for the __package__ attribute on modules. This attribute is
3n/aused, when available, to detect which package a module belongs to (instead
4n/aof using the typical __path__/__name__ test).
5n/a
6n/a"""
7n/aimport unittest
8n/afrom .. import util
9n/afrom . import util as import_util
10n/a
11n/a
12n/aclass Using__package__(unittest.TestCase):
13n/a
14n/a """Use of __package__ supercedes the use of __name__/__path__ to calculate
15n/a what package a module belongs to. The basic algorithm is [__package__]::
16n/a
17n/a def resolve_name(name, package, level):
18n/a level -= 1
19n/a base = package.rsplit('.', level)[0]
20n/a return '{0}.{1}'.format(base, name)
21n/a
22n/a But since there is no guarantee that __package__ has been set (or not been
23n/a set to None [None]), there has to be a way to calculate the attribute's value
24n/a [__name__]::
25n/a
26n/a def calc_package(caller_name, has___path__):
27n/a if has__path__:
28n/a return caller_name
29n/a else:
30n/a return caller_name.rsplit('.', 1)[0]
31n/a
32n/a Then the normal algorithm for relative name imports can proceed as if
33n/a __package__ had been set.
34n/a
35n/a """
36n/a
37n/a def test_using___package__(self):
38n/a # [__package__]
39n/a with util.mock_modules('pkg.__init__', 'pkg.fake') as importer:
40n/a with util.import_state(meta_path=[importer]):
41n/a import_util.import_('pkg.fake')
42n/a module = import_util.import_('',
43n/a globals={'__package__': 'pkg.fake'},
44n/a fromlist=['attr'], level=2)
45n/a self.assertEqual(module.__name__, 'pkg')
46n/a
47n/a def test_using___name__(self, package_as_None=False):
48n/a # [__name__]
49n/a globals_ = {'__name__': 'pkg.fake', '__path__': []}
50n/a if package_as_None:
51n/a globals_['__package__'] = None
52n/a with util.mock_modules('pkg.__init__', 'pkg.fake') as importer:
53n/a with util.import_state(meta_path=[importer]):
54n/a import_util.import_('pkg.fake')
55n/a module = import_util.import_('', globals= globals_,
56n/a fromlist=['attr'], level=2)
57n/a self.assertEqual(module.__name__, 'pkg')
58n/a
59n/a def test_None_as___package__(self):
60n/a # [None]
61n/a self.test_using___name__(package_as_None=True)
62n/a
63n/a def test_bad__package__(self):
64n/a globals = {'__package__': '<not real>'}
65n/a with self.assertRaises(SystemError):
66n/a import_util.import_('', globals, {}, ['relimport'], 1)
67n/a
68n/a def test_bunk__package__(self):
69n/a globals = {'__package__': 42}
70n/a with self.assertRaises(TypeError):
71n/a import_util.import_('', globals, {}, ['relimport'], 1)
72n/a
73n/a
74n/a@import_util.importlib_only
75n/aclass Setting__package__(unittest.TestCase):
76n/a
77n/a """Because __package__ is a new feature, it is not always set by a loader.
78n/a Import will set it as needed to help with the transition to relying on
79n/a __package__.
80n/a
81n/a For a top-level module, __package__ is set to None [top-level]. For a
82n/a package __name__ is used for __package__ [package]. For submodules the
83n/a value is __name__.rsplit('.', 1)[0] [submodule].
84n/a
85n/a """
86n/a
87n/a # [top-level]
88n/a def test_top_level(self):
89n/a with util.mock_modules('top_level') as mock:
90n/a with util.import_state(meta_path=[mock]):
91n/a del mock['top_level'].__package__
92n/a module = import_util.import_('top_level')
93n/a self.assertEqual(module.__package__, '')
94n/a
95n/a # [package]
96n/a def test_package(self):
97n/a with util.mock_modules('pkg.__init__') as mock:
98n/a with util.import_state(meta_path=[mock]):
99n/a del mock['pkg'].__package__
100n/a module = import_util.import_('pkg')
101n/a self.assertEqual(module.__package__, 'pkg')
102n/a
103n/a # [submodule]
104n/a def test_submodule(self):
105n/a with util.mock_modules('pkg.__init__', 'pkg.mod') as mock:
106n/a with util.import_state(meta_path=[mock]):
107n/a del mock['pkg.mod'].__package__
108n/a pkg = import_util.import_('pkg.mod')
109n/a module = getattr(pkg, 'mod')
110n/a self.assertEqual(module.__package__, 'pkg')
111n/a
112n/a
113n/adef test_main():
114n/a from test.support import run_unittest
115n/a run_unittest(Using__package__, Setting__package__)
116n/a
117n/a
118n/aif __name__ == '__main__':
119n/a test_main()