1 | n/a | """PEP 366 ("Main module explicit relative imports") specifies the |
---|
2 | n/a | semantics for the __package__ attribute on modules. This attribute is |
---|
3 | n/a | used, when available, to detect which package a module belongs to (instead |
---|
4 | n/a | of using the typical __path__/__name__ test). |
---|
5 | n/a | |
---|
6 | n/a | """ |
---|
7 | n/a | import unittest |
---|
8 | n/a | import warnings |
---|
9 | n/a | from .. import util |
---|
10 | n/a | |
---|
11 | n/a | |
---|
12 | n/a | class Using__package__: |
---|
13 | n/a | |
---|
14 | n/a | """Use of __package__ supercedes the use of __name__/__path__ to calculate |
---|
15 | n/a | what package a module belongs to. The basic algorithm is [__package__]:: |
---|
16 | n/a | |
---|
17 | n/a | def resolve_name(name, package, level): |
---|
18 | n/a | level -= 1 |
---|
19 | n/a | base = package.rsplit('.', level)[0] |
---|
20 | n/a | return '{0}.{1}'.format(base, name) |
---|
21 | n/a | |
---|
22 | n/a | But since there is no guarantee that __package__ has been set (or not been |
---|
23 | n/a | set to None [None]), there has to be a way to calculate the attribute's value |
---|
24 | n/a | [__name__]:: |
---|
25 | n/a | |
---|
26 | n/a | def calc_package(caller_name, has___path__): |
---|
27 | n/a | if has__path__: |
---|
28 | n/a | return caller_name |
---|
29 | n/a | else: |
---|
30 | n/a | return caller_name.rsplit('.', 1)[0] |
---|
31 | n/a | |
---|
32 | n/a | Then the normal algorithm for relative name imports can proceed as if |
---|
33 | n/a | __package__ had been set. |
---|
34 | n/a | |
---|
35 | n/a | """ |
---|
36 | n/a | |
---|
37 | n/a | def import_module(self, globals_): |
---|
38 | n/a | with self.mock_modules('pkg.__init__', 'pkg.fake') as importer: |
---|
39 | n/a | with util.import_state(meta_path=[importer]): |
---|
40 | n/a | self.__import__('pkg.fake') |
---|
41 | n/a | module = self.__import__('', |
---|
42 | n/a | globals=globals_, |
---|
43 | n/a | fromlist=['attr'], level=2) |
---|
44 | n/a | return module |
---|
45 | n/a | |
---|
46 | n/a | def test_using___package__(self): |
---|
47 | n/a | # [__package__] |
---|
48 | n/a | module = self.import_module({'__package__': 'pkg.fake'}) |
---|
49 | n/a | self.assertEqual(module.__name__, 'pkg') |
---|
50 | n/a | |
---|
51 | n/a | def test_using___name__(self): |
---|
52 | n/a | # [__name__] |
---|
53 | n/a | with warnings.catch_warnings(): |
---|
54 | n/a | warnings.simplefilter("ignore") |
---|
55 | n/a | module = self.import_module({'__name__': 'pkg.fake', |
---|
56 | n/a | '__path__': []}) |
---|
57 | n/a | self.assertEqual(module.__name__, 'pkg') |
---|
58 | n/a | |
---|
59 | n/a | def test_warn_when_using___name__(self): |
---|
60 | n/a | with self.assertWarns(ImportWarning): |
---|
61 | n/a | self.import_module({'__name__': 'pkg.fake', '__path__': []}) |
---|
62 | n/a | |
---|
63 | n/a | def test_None_as___package__(self): |
---|
64 | n/a | # [None] |
---|
65 | n/a | with warnings.catch_warnings(): |
---|
66 | n/a | warnings.simplefilter("ignore") |
---|
67 | n/a | module = self.import_module({ |
---|
68 | n/a | '__name__': 'pkg.fake', '__path__': [], '__package__': None }) |
---|
69 | n/a | self.assertEqual(module.__name__, 'pkg') |
---|
70 | n/a | |
---|
71 | n/a | def test_spec_fallback(self): |
---|
72 | n/a | # If __package__ isn't defined, fall back on __spec__.parent. |
---|
73 | n/a | module = self.import_module({'__spec__': FakeSpec('pkg.fake')}) |
---|
74 | n/a | self.assertEqual(module.__name__, 'pkg') |
---|
75 | n/a | |
---|
76 | n/a | def test_warn_when_package_and_spec_disagree(self): |
---|
77 | n/a | # Raise an ImportWarning if __package__ != __spec__.parent. |
---|
78 | n/a | with self.assertWarns(ImportWarning): |
---|
79 | n/a | self.import_module({'__package__': 'pkg.fake', |
---|
80 | n/a | '__spec__': FakeSpec('pkg.fakefake')}) |
---|
81 | n/a | |
---|
82 | n/a | def test_bad__package__(self): |
---|
83 | n/a | globals = {'__package__': '<not real>'} |
---|
84 | n/a | with self.assertRaises(SystemError): |
---|
85 | n/a | self.__import__('', globals, {}, ['relimport'], 1) |
---|
86 | n/a | |
---|
87 | n/a | def test_bunk__package__(self): |
---|
88 | n/a | globals = {'__package__': 42} |
---|
89 | n/a | with self.assertRaises(TypeError): |
---|
90 | n/a | self.__import__('', globals, {}, ['relimport'], 1) |
---|
91 | n/a | |
---|
92 | n/a | |
---|
93 | n/a | class FakeSpec: |
---|
94 | n/a | def __init__(self, parent): |
---|
95 | n/a | self.parent = parent |
---|
96 | n/a | |
---|
97 | n/a | |
---|
98 | n/a | class Using__package__PEP302(Using__package__): |
---|
99 | n/a | mock_modules = util.mock_modules |
---|
100 | n/a | |
---|
101 | n/a | |
---|
102 | n/a | (Frozen_UsingPackagePEP302, |
---|
103 | n/a | Source_UsingPackagePEP302 |
---|
104 | n/a | ) = util.test_both(Using__package__PEP302, __import__=util.__import__) |
---|
105 | n/a | |
---|
106 | n/a | |
---|
107 | n/a | class Using__package__PEP451(Using__package__): |
---|
108 | n/a | mock_modules = util.mock_spec |
---|
109 | n/a | |
---|
110 | n/a | |
---|
111 | n/a | (Frozen_UsingPackagePEP451, |
---|
112 | n/a | Source_UsingPackagePEP451 |
---|
113 | n/a | ) = util.test_both(Using__package__PEP451, __import__=util.__import__) |
---|
114 | n/a | |
---|
115 | n/a | |
---|
116 | n/a | class Setting__package__: |
---|
117 | n/a | |
---|
118 | n/a | """Because __package__ is a new feature, it is not always set by a loader. |
---|
119 | n/a | Import will set it as needed to help with the transition to relying on |
---|
120 | n/a | __package__. |
---|
121 | n/a | |
---|
122 | n/a | For a top-level module, __package__ is set to None [top-level]. For a |
---|
123 | n/a | package __name__ is used for __package__ [package]. For submodules the |
---|
124 | n/a | value is __name__.rsplit('.', 1)[0] [submodule]. |
---|
125 | n/a | |
---|
126 | n/a | """ |
---|
127 | n/a | |
---|
128 | n/a | __import__ = util.__import__['Source'] |
---|
129 | n/a | |
---|
130 | n/a | # [top-level] |
---|
131 | n/a | def test_top_level(self): |
---|
132 | n/a | with self.mock_modules('top_level') as mock: |
---|
133 | n/a | with util.import_state(meta_path=[mock]): |
---|
134 | n/a | del mock['top_level'].__package__ |
---|
135 | n/a | module = self.__import__('top_level') |
---|
136 | n/a | self.assertEqual(module.__package__, '') |
---|
137 | n/a | |
---|
138 | n/a | # [package] |
---|
139 | n/a | def test_package(self): |
---|
140 | n/a | with self.mock_modules('pkg.__init__') as mock: |
---|
141 | n/a | with util.import_state(meta_path=[mock]): |
---|
142 | n/a | del mock['pkg'].__package__ |
---|
143 | n/a | module = self.__import__('pkg') |
---|
144 | n/a | self.assertEqual(module.__package__, 'pkg') |
---|
145 | n/a | |
---|
146 | n/a | # [submodule] |
---|
147 | n/a | def test_submodule(self): |
---|
148 | n/a | with self.mock_modules('pkg.__init__', 'pkg.mod') as mock: |
---|
149 | n/a | with util.import_state(meta_path=[mock]): |
---|
150 | n/a | del mock['pkg.mod'].__package__ |
---|
151 | n/a | pkg = self.__import__('pkg.mod') |
---|
152 | n/a | module = getattr(pkg, 'mod') |
---|
153 | n/a | self.assertEqual(module.__package__, 'pkg') |
---|
154 | n/a | |
---|
155 | n/a | class Setting__package__PEP302(Setting__package__, unittest.TestCase): |
---|
156 | n/a | mock_modules = util.mock_modules |
---|
157 | n/a | |
---|
158 | n/a | class Setting__package__PEP451(Setting__package__, unittest.TestCase): |
---|
159 | n/a | mock_modules = util.mock_spec |
---|
160 | n/a | |
---|
161 | n/a | |
---|
162 | n/a | if __name__ == '__main__': |
---|
163 | n/a | unittest.main() |
---|