1 | n/a | """Test relative imports (PEP 328).""" |
---|
2 | n/a | from .. import util |
---|
3 | n/a | import unittest |
---|
4 | n/a | import warnings |
---|
5 | n/a | |
---|
6 | n/a | |
---|
7 | n/a | class RelativeImports: |
---|
8 | n/a | |
---|
9 | n/a | """PEP 328 introduced relative imports. This allows for imports to occur |
---|
10 | n/a | from within a package without having to specify the actual package name. |
---|
11 | n/a | |
---|
12 | n/a | A simple example is to import another module within the same package |
---|
13 | n/a | [module from module]:: |
---|
14 | n/a | |
---|
15 | n/a | # From pkg.mod1 with pkg.mod2 being a module. |
---|
16 | n/a | from . import mod2 |
---|
17 | n/a | |
---|
18 | n/a | This also works for getting an attribute from a module that is specified |
---|
19 | n/a | in a relative fashion [attr from module]:: |
---|
20 | n/a | |
---|
21 | n/a | # From pkg.mod1. |
---|
22 | n/a | from .mod2 import attr |
---|
23 | n/a | |
---|
24 | n/a | But this is in no way restricted to working between modules; it works |
---|
25 | n/a | from [package to module],:: |
---|
26 | n/a | |
---|
27 | n/a | # From pkg, importing pkg.module which is a module. |
---|
28 | n/a | from . import module |
---|
29 | n/a | |
---|
30 | n/a | [module to package],:: |
---|
31 | n/a | |
---|
32 | n/a | # Pull attr from pkg, called from pkg.module which is a module. |
---|
33 | n/a | from . import attr |
---|
34 | n/a | |
---|
35 | n/a | and [package to package]:: |
---|
36 | n/a | |
---|
37 | n/a | # From pkg.subpkg1 (both pkg.subpkg[1,2] are packages). |
---|
38 | n/a | from .. import subpkg2 |
---|
39 | n/a | |
---|
40 | n/a | The number of dots used is in no way restricted [deep import]:: |
---|
41 | n/a | |
---|
42 | n/a | # Import pkg.attr from pkg.pkg1.pkg2.pkg3.pkg4.pkg5. |
---|
43 | n/a | from ...... import attr |
---|
44 | n/a | |
---|
45 | n/a | To prevent someone from accessing code that is outside of a package, one |
---|
46 | n/a | cannot reach the location containing the root package itself:: |
---|
47 | n/a | |
---|
48 | n/a | # From pkg.__init__ [too high from package] |
---|
49 | n/a | from .. import top_level |
---|
50 | n/a | |
---|
51 | n/a | # From pkg.module [too high from module] |
---|
52 | n/a | from .. import top_level |
---|
53 | n/a | |
---|
54 | n/a | Relative imports are the only type of import that allow for an empty |
---|
55 | n/a | module name for an import [empty name]. |
---|
56 | n/a | |
---|
57 | n/a | """ |
---|
58 | n/a | |
---|
59 | n/a | def relative_import_test(self, create, globals_, callback): |
---|
60 | n/a | """Abstract out boilerplace for setting up for an import test.""" |
---|
61 | n/a | uncache_names = [] |
---|
62 | n/a | for name in create: |
---|
63 | n/a | if not name.endswith('.__init__'): |
---|
64 | n/a | uncache_names.append(name) |
---|
65 | n/a | else: |
---|
66 | n/a | uncache_names.append(name[:-len('.__init__')]) |
---|
67 | n/a | with util.mock_spec(*create) as importer: |
---|
68 | n/a | with util.import_state(meta_path=[importer]): |
---|
69 | n/a | with warnings.catch_warnings(): |
---|
70 | n/a | warnings.simplefilter("ignore") |
---|
71 | n/a | for global_ in globals_: |
---|
72 | n/a | with util.uncache(*uncache_names): |
---|
73 | n/a | callback(global_) |
---|
74 | n/a | |
---|
75 | n/a | |
---|
76 | n/a | def test_module_from_module(self): |
---|
77 | n/a | # [module from module] |
---|
78 | n/a | create = 'pkg.__init__', 'pkg.mod2' |
---|
79 | n/a | globals_ = {'__package__': 'pkg'}, {'__name__': 'pkg.mod1'} |
---|
80 | n/a | def callback(global_): |
---|
81 | n/a | self.__import__('pkg') # For __import__(). |
---|
82 | n/a | module = self.__import__('', global_, fromlist=['mod2'], level=1) |
---|
83 | n/a | self.assertEqual(module.__name__, 'pkg') |
---|
84 | n/a | self.assertTrue(hasattr(module, 'mod2')) |
---|
85 | n/a | self.assertEqual(module.mod2.attr, 'pkg.mod2') |
---|
86 | n/a | self.relative_import_test(create, globals_, callback) |
---|
87 | n/a | |
---|
88 | n/a | def test_attr_from_module(self): |
---|
89 | n/a | # [attr from module] |
---|
90 | n/a | create = 'pkg.__init__', 'pkg.mod2' |
---|
91 | n/a | globals_ = {'__package__': 'pkg'}, {'__name__': 'pkg.mod1'} |
---|
92 | n/a | def callback(global_): |
---|
93 | n/a | self.__import__('pkg') # For __import__(). |
---|
94 | n/a | module = self.__import__('mod2', global_, fromlist=['attr'], |
---|
95 | n/a | level=1) |
---|
96 | n/a | self.assertEqual(module.__name__, 'pkg.mod2') |
---|
97 | n/a | self.assertEqual(module.attr, 'pkg.mod2') |
---|
98 | n/a | self.relative_import_test(create, globals_, callback) |
---|
99 | n/a | |
---|
100 | n/a | def test_package_to_module(self): |
---|
101 | n/a | # [package to module] |
---|
102 | n/a | create = 'pkg.__init__', 'pkg.module' |
---|
103 | n/a | globals_ = ({'__package__': 'pkg'}, |
---|
104 | n/a | {'__name__': 'pkg', '__path__': ['blah']}) |
---|
105 | n/a | def callback(global_): |
---|
106 | n/a | self.__import__('pkg') # For __import__(). |
---|
107 | n/a | module = self.__import__('', global_, fromlist=['module'], |
---|
108 | n/a | level=1) |
---|
109 | n/a | self.assertEqual(module.__name__, 'pkg') |
---|
110 | n/a | self.assertTrue(hasattr(module, 'module')) |
---|
111 | n/a | self.assertEqual(module.module.attr, 'pkg.module') |
---|
112 | n/a | self.relative_import_test(create, globals_, callback) |
---|
113 | n/a | |
---|
114 | n/a | def test_module_to_package(self): |
---|
115 | n/a | # [module to package] |
---|
116 | n/a | create = 'pkg.__init__', 'pkg.module' |
---|
117 | n/a | globals_ = {'__package__': 'pkg'}, {'__name__': 'pkg.module'} |
---|
118 | n/a | def callback(global_): |
---|
119 | n/a | self.__import__('pkg') # For __import__(). |
---|
120 | n/a | module = self.__import__('', global_, fromlist=['attr'], level=1) |
---|
121 | n/a | self.assertEqual(module.__name__, 'pkg') |
---|
122 | n/a | self.relative_import_test(create, globals_, callback) |
---|
123 | n/a | |
---|
124 | n/a | def test_package_to_package(self): |
---|
125 | n/a | # [package to package] |
---|
126 | n/a | create = ('pkg.__init__', 'pkg.subpkg1.__init__', |
---|
127 | n/a | 'pkg.subpkg2.__init__') |
---|
128 | n/a | globals_ = ({'__package__': 'pkg.subpkg1'}, |
---|
129 | n/a | {'__name__': 'pkg.subpkg1', '__path__': ['blah']}) |
---|
130 | n/a | def callback(global_): |
---|
131 | n/a | module = self.__import__('', global_, fromlist=['subpkg2'], |
---|
132 | n/a | level=2) |
---|
133 | n/a | self.assertEqual(module.__name__, 'pkg') |
---|
134 | n/a | self.assertTrue(hasattr(module, 'subpkg2')) |
---|
135 | n/a | self.assertEqual(module.subpkg2.attr, 'pkg.subpkg2.__init__') |
---|
136 | n/a | |
---|
137 | n/a | def test_deep_import(self): |
---|
138 | n/a | # [deep import] |
---|
139 | n/a | create = ['pkg.__init__'] |
---|
140 | n/a | for count in range(1,6): |
---|
141 | n/a | create.append('{0}.pkg{1}.__init__'.format( |
---|
142 | n/a | create[-1][:-len('.__init__')], count)) |
---|
143 | n/a | globals_ = ({'__package__': 'pkg.pkg1.pkg2.pkg3.pkg4.pkg5'}, |
---|
144 | n/a | {'__name__': 'pkg.pkg1.pkg2.pkg3.pkg4.pkg5', |
---|
145 | n/a | '__path__': ['blah']}) |
---|
146 | n/a | def callback(global_): |
---|
147 | n/a | self.__import__(globals_[0]['__package__']) |
---|
148 | n/a | module = self.__import__('', global_, fromlist=['attr'], level=6) |
---|
149 | n/a | self.assertEqual(module.__name__, 'pkg') |
---|
150 | n/a | self.relative_import_test(create, globals_, callback) |
---|
151 | n/a | |
---|
152 | n/a | def test_too_high_from_package(self): |
---|
153 | n/a | # [too high from package] |
---|
154 | n/a | create = ['top_level', 'pkg.__init__'] |
---|
155 | n/a | globals_ = ({'__package__': 'pkg'}, |
---|
156 | n/a | {'__name__': 'pkg', '__path__': ['blah']}) |
---|
157 | n/a | def callback(global_): |
---|
158 | n/a | self.__import__('pkg') |
---|
159 | n/a | with self.assertRaises(ValueError): |
---|
160 | n/a | self.__import__('', global_, fromlist=['top_level'], |
---|
161 | n/a | level=2) |
---|
162 | n/a | self.relative_import_test(create, globals_, callback) |
---|
163 | n/a | |
---|
164 | n/a | def test_too_high_from_module(self): |
---|
165 | n/a | # [too high from module] |
---|
166 | n/a | create = ['top_level', 'pkg.__init__', 'pkg.module'] |
---|
167 | n/a | globals_ = {'__package__': 'pkg'}, {'__name__': 'pkg.module'} |
---|
168 | n/a | def callback(global_): |
---|
169 | n/a | self.__import__('pkg') |
---|
170 | n/a | with self.assertRaises(ValueError): |
---|
171 | n/a | self.__import__('', global_, fromlist=['top_level'], |
---|
172 | n/a | level=2) |
---|
173 | n/a | self.relative_import_test(create, globals_, callback) |
---|
174 | n/a | |
---|
175 | n/a | def test_empty_name_w_level_0(self): |
---|
176 | n/a | # [empty name] |
---|
177 | n/a | with self.assertRaises(ValueError): |
---|
178 | n/a | self.__import__('') |
---|
179 | n/a | |
---|
180 | n/a | def test_import_from_different_package(self): |
---|
181 | n/a | # Test importing from a different package than the caller. |
---|
182 | n/a | # in pkg.subpkg1.mod |
---|
183 | n/a | # from ..subpkg2 import mod |
---|
184 | n/a | create = ['__runpy_pkg__.__init__', |
---|
185 | n/a | '__runpy_pkg__.__runpy_pkg__.__init__', |
---|
186 | n/a | '__runpy_pkg__.uncle.__init__', |
---|
187 | n/a | '__runpy_pkg__.uncle.cousin.__init__', |
---|
188 | n/a | '__runpy_pkg__.uncle.cousin.nephew'] |
---|
189 | n/a | globals_ = {'__package__': '__runpy_pkg__.__runpy_pkg__'} |
---|
190 | n/a | def callback(global_): |
---|
191 | n/a | self.__import__('__runpy_pkg__.__runpy_pkg__') |
---|
192 | n/a | module = self.__import__('uncle.cousin', globals_, {}, |
---|
193 | n/a | fromlist=['nephew'], |
---|
194 | n/a | level=2) |
---|
195 | n/a | self.assertEqual(module.__name__, '__runpy_pkg__.uncle.cousin') |
---|
196 | n/a | self.relative_import_test(create, globals_, callback) |
---|
197 | n/a | |
---|
198 | n/a | def test_import_relative_import_no_fromlist(self): |
---|
199 | n/a | # Import a relative module w/ no fromlist. |
---|
200 | n/a | create = ['crash.__init__', 'crash.mod'] |
---|
201 | n/a | globals_ = [{'__package__': 'crash', '__name__': 'crash'}] |
---|
202 | n/a | def callback(global_): |
---|
203 | n/a | self.__import__('crash') |
---|
204 | n/a | mod = self.__import__('mod', global_, {}, [], 1) |
---|
205 | n/a | self.assertEqual(mod.__name__, 'crash.mod') |
---|
206 | n/a | self.relative_import_test(create, globals_, callback) |
---|
207 | n/a | |
---|
208 | n/a | def test_relative_import_no_globals(self): |
---|
209 | n/a | # No globals for a relative import is an error. |
---|
210 | n/a | with warnings.catch_warnings(): |
---|
211 | n/a | warnings.simplefilter("ignore") |
---|
212 | n/a | with self.assertRaises(KeyError): |
---|
213 | n/a | self.__import__('sys', level=1) |
---|
214 | n/a | |
---|
215 | n/a | def test_relative_import_no_package(self): |
---|
216 | n/a | with self.assertRaises(ImportError): |
---|
217 | n/a | self.__import__('a', {'__package__': '', '__spec__': None}, |
---|
218 | n/a | level=1) |
---|
219 | n/a | |
---|
220 | n/a | def test_relative_import_no_package_exists_absolute(self): |
---|
221 | n/a | with self.assertRaises(ImportError): |
---|
222 | n/a | self.__import__('sys', {'__package__': '', '__spec__': None}, |
---|
223 | n/a | level=1) |
---|
224 | n/a | |
---|
225 | n/a | |
---|
226 | n/a | (Frozen_RelativeImports, |
---|
227 | n/a | Source_RelativeImports |
---|
228 | n/a | ) = util.test_both(RelativeImports, __import__=util.__import__) |
---|
229 | n/a | |
---|
230 | n/a | |
---|
231 | n/a | if __name__ == '__main__': |
---|
232 | n/a | unittest.main() |
---|