1 | n/a | try: |
---|
2 | n/a | import _thread |
---|
3 | n/a | except ImportError: |
---|
4 | n/a | _thread = None |
---|
5 | n/a | import importlib |
---|
6 | n/a | import importlib.util |
---|
7 | n/a | import os |
---|
8 | n/a | import os.path |
---|
9 | n/a | import sys |
---|
10 | n/a | from test import support |
---|
11 | n/a | import unittest |
---|
12 | n/a | import warnings |
---|
13 | n/a | with warnings.catch_warnings(): |
---|
14 | n/a | warnings.simplefilter('ignore', DeprecationWarning) |
---|
15 | n/a | import imp |
---|
16 | n/a | |
---|
17 | n/a | |
---|
18 | n/a | def requires_load_dynamic(meth): |
---|
19 | n/a | """Decorator to skip a test if not running under CPython or lacking |
---|
20 | n/a | imp.load_dynamic().""" |
---|
21 | n/a | meth = support.cpython_only(meth) |
---|
22 | n/a | return unittest.skipIf(not hasattr(imp, 'load_dynamic'), |
---|
23 | n/a | 'imp.load_dynamic() required')(meth) |
---|
24 | n/a | |
---|
25 | n/a | |
---|
26 | n/a | @unittest.skipIf(_thread is None, '_thread module is required') |
---|
27 | n/a | class LockTests(unittest.TestCase): |
---|
28 | n/a | |
---|
29 | n/a | """Very basic test of import lock functions.""" |
---|
30 | n/a | |
---|
31 | n/a | def verify_lock_state(self, expected): |
---|
32 | n/a | self.assertEqual(imp.lock_held(), expected, |
---|
33 | n/a | "expected imp.lock_held() to be %r" % expected) |
---|
34 | n/a | def testLock(self): |
---|
35 | n/a | LOOPS = 50 |
---|
36 | n/a | |
---|
37 | n/a | # The import lock may already be held, e.g. if the test suite is run |
---|
38 | n/a | # via "import test.autotest". |
---|
39 | n/a | lock_held_at_start = imp.lock_held() |
---|
40 | n/a | self.verify_lock_state(lock_held_at_start) |
---|
41 | n/a | |
---|
42 | n/a | for i in range(LOOPS): |
---|
43 | n/a | imp.acquire_lock() |
---|
44 | n/a | self.verify_lock_state(True) |
---|
45 | n/a | |
---|
46 | n/a | for i in range(LOOPS): |
---|
47 | n/a | imp.release_lock() |
---|
48 | n/a | |
---|
49 | n/a | # The original state should be restored now. |
---|
50 | n/a | self.verify_lock_state(lock_held_at_start) |
---|
51 | n/a | |
---|
52 | n/a | if not lock_held_at_start: |
---|
53 | n/a | try: |
---|
54 | n/a | imp.release_lock() |
---|
55 | n/a | except RuntimeError: |
---|
56 | n/a | pass |
---|
57 | n/a | else: |
---|
58 | n/a | self.fail("release_lock() without lock should raise " |
---|
59 | n/a | "RuntimeError") |
---|
60 | n/a | |
---|
61 | n/a | class ImportTests(unittest.TestCase): |
---|
62 | n/a | def setUp(self): |
---|
63 | n/a | mod = importlib.import_module('test.encoded_modules') |
---|
64 | n/a | self.test_strings = mod.test_strings |
---|
65 | n/a | self.test_path = mod.__path__ |
---|
66 | n/a | |
---|
67 | n/a | def test_import_encoded_module(self): |
---|
68 | n/a | for modname, encoding, teststr in self.test_strings: |
---|
69 | n/a | mod = importlib.import_module('test.encoded_modules.' |
---|
70 | n/a | 'module_' + modname) |
---|
71 | n/a | self.assertEqual(teststr, mod.test) |
---|
72 | n/a | |
---|
73 | n/a | def test_find_module_encoding(self): |
---|
74 | n/a | for mod, encoding, _ in self.test_strings: |
---|
75 | n/a | with imp.find_module('module_' + mod, self.test_path)[0] as fd: |
---|
76 | n/a | self.assertEqual(fd.encoding, encoding) |
---|
77 | n/a | |
---|
78 | n/a | path = [os.path.dirname(__file__)] |
---|
79 | n/a | with self.assertRaises(SyntaxError): |
---|
80 | n/a | imp.find_module('badsyntax_pep3120', path) |
---|
81 | n/a | |
---|
82 | n/a | def test_issue1267(self): |
---|
83 | n/a | for mod, encoding, _ in self.test_strings: |
---|
84 | n/a | fp, filename, info = imp.find_module('module_' + mod, |
---|
85 | n/a | self.test_path) |
---|
86 | n/a | with fp: |
---|
87 | n/a | self.assertNotEqual(fp, None) |
---|
88 | n/a | self.assertEqual(fp.encoding, encoding) |
---|
89 | n/a | self.assertEqual(fp.tell(), 0) |
---|
90 | n/a | self.assertEqual(fp.readline(), '# test %s encoding\n' |
---|
91 | n/a | % encoding) |
---|
92 | n/a | |
---|
93 | n/a | fp, filename, info = imp.find_module("tokenize") |
---|
94 | n/a | with fp: |
---|
95 | n/a | self.assertNotEqual(fp, None) |
---|
96 | n/a | self.assertEqual(fp.encoding, "utf-8") |
---|
97 | n/a | self.assertEqual(fp.tell(), 0) |
---|
98 | n/a | self.assertEqual(fp.readline(), |
---|
99 | n/a | '"""Tokenization help for Python programs.\n') |
---|
100 | n/a | |
---|
101 | n/a | def test_issue3594(self): |
---|
102 | n/a | temp_mod_name = 'test_imp_helper' |
---|
103 | n/a | sys.path.insert(0, '.') |
---|
104 | n/a | try: |
---|
105 | n/a | with open(temp_mod_name + '.py', 'w') as file: |
---|
106 | n/a | file.write("# coding: cp1252\nu = 'test.test_imp'\n") |
---|
107 | n/a | file, filename, info = imp.find_module(temp_mod_name) |
---|
108 | n/a | file.close() |
---|
109 | n/a | self.assertEqual(file.encoding, 'cp1252') |
---|
110 | n/a | finally: |
---|
111 | n/a | del sys.path[0] |
---|
112 | n/a | support.unlink(temp_mod_name + '.py') |
---|
113 | n/a | support.unlink(temp_mod_name + '.pyc') |
---|
114 | n/a | |
---|
115 | n/a | def test_issue5604(self): |
---|
116 | n/a | # Test cannot cover imp.load_compiled function. |
---|
117 | n/a | # Martin von Loewis note what shared library cannot have non-ascii |
---|
118 | n/a | # character because init_xxx function cannot be compiled |
---|
119 | n/a | # and issue never happens for dynamic modules. |
---|
120 | n/a | # But sources modified to follow generic way for processing pathes. |
---|
121 | n/a | |
---|
122 | n/a | # the return encoding could be uppercase or None |
---|
123 | n/a | fs_encoding = sys.getfilesystemencoding() |
---|
124 | n/a | |
---|
125 | n/a | # covers utf-8 and Windows ANSI code pages |
---|
126 | n/a | # one non-space symbol from every page |
---|
127 | n/a | # (http://en.wikipedia.org/wiki/Code_page) |
---|
128 | n/a | known_locales = { |
---|
129 | n/a | 'utf-8' : b'\xc3\xa4', |
---|
130 | n/a | 'cp1250' : b'\x8C', |
---|
131 | n/a | 'cp1251' : b'\xc0', |
---|
132 | n/a | 'cp1252' : b'\xc0', |
---|
133 | n/a | 'cp1253' : b'\xc1', |
---|
134 | n/a | 'cp1254' : b'\xc0', |
---|
135 | n/a | 'cp1255' : b'\xe0', |
---|
136 | n/a | 'cp1256' : b'\xe0', |
---|
137 | n/a | 'cp1257' : b'\xc0', |
---|
138 | n/a | 'cp1258' : b'\xc0', |
---|
139 | n/a | } |
---|
140 | n/a | |
---|
141 | n/a | if sys.platform == 'darwin': |
---|
142 | n/a | self.assertEqual(fs_encoding, 'utf-8') |
---|
143 | n/a | # Mac OS X uses the Normal Form D decomposition |
---|
144 | n/a | # http://developer.apple.com/mac/library/qa/qa2001/qa1173.html |
---|
145 | n/a | special_char = b'a\xcc\x88' |
---|
146 | n/a | else: |
---|
147 | n/a | special_char = known_locales.get(fs_encoding) |
---|
148 | n/a | |
---|
149 | n/a | if not special_char: |
---|
150 | n/a | self.skipTest("can't run this test with %s as filesystem encoding" |
---|
151 | n/a | % fs_encoding) |
---|
152 | n/a | decoded_char = special_char.decode(fs_encoding) |
---|
153 | n/a | temp_mod_name = 'test_imp_helper_' + decoded_char |
---|
154 | n/a | test_package_name = 'test_imp_helper_package_' + decoded_char |
---|
155 | n/a | init_file_name = os.path.join(test_package_name, '__init__.py') |
---|
156 | n/a | try: |
---|
157 | n/a | # if the curdir is not in sys.path the test fails when run with |
---|
158 | n/a | # ./python ./Lib/test/regrtest.py test_imp |
---|
159 | n/a | sys.path.insert(0, os.curdir) |
---|
160 | n/a | with open(temp_mod_name + '.py', 'w') as file: |
---|
161 | n/a | file.write('a = 1\n') |
---|
162 | n/a | file, filename, info = imp.find_module(temp_mod_name) |
---|
163 | n/a | with file: |
---|
164 | n/a | self.assertIsNotNone(file) |
---|
165 | n/a | self.assertTrue(filename[:-3].endswith(temp_mod_name)) |
---|
166 | n/a | self.assertEqual(info[0], '.py') |
---|
167 | n/a | self.assertEqual(info[1], 'r') |
---|
168 | n/a | self.assertEqual(info[2], imp.PY_SOURCE) |
---|
169 | n/a | |
---|
170 | n/a | mod = imp.load_module(temp_mod_name, file, filename, info) |
---|
171 | n/a | self.assertEqual(mod.a, 1) |
---|
172 | n/a | |
---|
173 | n/a | with warnings.catch_warnings(): |
---|
174 | n/a | warnings.simplefilter('ignore') |
---|
175 | n/a | mod = imp.load_source(temp_mod_name, temp_mod_name + '.py') |
---|
176 | n/a | self.assertEqual(mod.a, 1) |
---|
177 | n/a | |
---|
178 | n/a | with warnings.catch_warnings(): |
---|
179 | n/a | warnings.simplefilter('ignore') |
---|
180 | n/a | if not sys.dont_write_bytecode: |
---|
181 | n/a | mod = imp.load_compiled( |
---|
182 | n/a | temp_mod_name, |
---|
183 | n/a | imp.cache_from_source(temp_mod_name + '.py')) |
---|
184 | n/a | self.assertEqual(mod.a, 1) |
---|
185 | n/a | |
---|
186 | n/a | if not os.path.exists(test_package_name): |
---|
187 | n/a | os.mkdir(test_package_name) |
---|
188 | n/a | with open(init_file_name, 'w') as file: |
---|
189 | n/a | file.write('b = 2\n') |
---|
190 | n/a | with warnings.catch_warnings(): |
---|
191 | n/a | warnings.simplefilter('ignore') |
---|
192 | n/a | package = imp.load_package(test_package_name, test_package_name) |
---|
193 | n/a | self.assertEqual(package.b, 2) |
---|
194 | n/a | finally: |
---|
195 | n/a | del sys.path[0] |
---|
196 | n/a | for ext in ('.py', '.pyc'): |
---|
197 | n/a | support.unlink(temp_mod_name + ext) |
---|
198 | n/a | support.unlink(init_file_name + ext) |
---|
199 | n/a | support.rmtree(test_package_name) |
---|
200 | n/a | support.rmtree('__pycache__') |
---|
201 | n/a | |
---|
202 | n/a | def test_issue9319(self): |
---|
203 | n/a | path = os.path.dirname(__file__) |
---|
204 | n/a | self.assertRaises(SyntaxError, |
---|
205 | n/a | imp.find_module, "badsyntax_pep3120", [path]) |
---|
206 | n/a | |
---|
207 | n/a | def test_load_from_source(self): |
---|
208 | n/a | # Verify that the imp module can correctly load and find .py files |
---|
209 | n/a | # XXX (ncoghlan): It would be nice to use support.CleanImport |
---|
210 | n/a | # here, but that breaks because the os module registers some |
---|
211 | n/a | # handlers in copy_reg on import. Since CleanImport doesn't |
---|
212 | n/a | # revert that registration, the module is left in a broken |
---|
213 | n/a | # state after reversion. Reinitialising the module contents |
---|
214 | n/a | # and just reverting os.environ to its previous state is an OK |
---|
215 | n/a | # workaround |
---|
216 | n/a | orig_path = os.path |
---|
217 | n/a | orig_getenv = os.getenv |
---|
218 | n/a | with support.EnvironmentVarGuard(): |
---|
219 | n/a | x = imp.find_module("os") |
---|
220 | n/a | self.addCleanup(x[0].close) |
---|
221 | n/a | new_os = imp.load_module("os", *x) |
---|
222 | n/a | self.assertIs(os, new_os) |
---|
223 | n/a | self.assertIs(orig_path, new_os.path) |
---|
224 | n/a | self.assertIsNot(orig_getenv, new_os.getenv) |
---|
225 | n/a | |
---|
226 | n/a | @requires_load_dynamic |
---|
227 | n/a | def test_issue15828_load_extensions(self): |
---|
228 | n/a | # Issue 15828 picked up that the adapter between the old imp API |
---|
229 | n/a | # and importlib couldn't handle C extensions |
---|
230 | n/a | example = "_heapq" |
---|
231 | n/a | x = imp.find_module(example) |
---|
232 | n/a | file_ = x[0] |
---|
233 | n/a | if file_ is not None: |
---|
234 | n/a | self.addCleanup(file_.close) |
---|
235 | n/a | mod = imp.load_module(example, *x) |
---|
236 | n/a | self.assertEqual(mod.__name__, example) |
---|
237 | n/a | |
---|
238 | n/a | @requires_load_dynamic |
---|
239 | n/a | def test_issue16421_multiple_modules_in_one_dll(self): |
---|
240 | n/a | # Issue 16421: loading several modules from the same compiled file fails |
---|
241 | n/a | m = '_testimportmultiple' |
---|
242 | n/a | fileobj, pathname, description = imp.find_module(m) |
---|
243 | n/a | fileobj.close() |
---|
244 | n/a | mod0 = imp.load_dynamic(m, pathname) |
---|
245 | n/a | mod1 = imp.load_dynamic('_testimportmultiple_foo', pathname) |
---|
246 | n/a | mod2 = imp.load_dynamic('_testimportmultiple_bar', pathname) |
---|
247 | n/a | self.assertEqual(mod0.__name__, m) |
---|
248 | n/a | self.assertEqual(mod1.__name__, '_testimportmultiple_foo') |
---|
249 | n/a | self.assertEqual(mod2.__name__, '_testimportmultiple_bar') |
---|
250 | n/a | with self.assertRaises(ImportError): |
---|
251 | n/a | imp.load_dynamic('nonexistent', pathname) |
---|
252 | n/a | |
---|
253 | n/a | @requires_load_dynamic |
---|
254 | n/a | def test_load_dynamic_ImportError_path(self): |
---|
255 | n/a | # Issue #1559549 added `name` and `path` attributes to ImportError |
---|
256 | n/a | # in order to provide better detail. Issue #10854 implemented those |
---|
257 | n/a | # attributes on import failures of extensions on Windows. |
---|
258 | n/a | path = 'bogus file path' |
---|
259 | n/a | name = 'extension' |
---|
260 | n/a | with self.assertRaises(ImportError) as err: |
---|
261 | n/a | imp.load_dynamic(name, path) |
---|
262 | n/a | self.assertIn(path, err.exception.path) |
---|
263 | n/a | self.assertEqual(name, err.exception.name) |
---|
264 | n/a | |
---|
265 | n/a | @requires_load_dynamic |
---|
266 | n/a | def test_load_module_extension_file_is_None(self): |
---|
267 | n/a | # When loading an extension module and the file is None, open one |
---|
268 | n/a | # on the behalf of imp.load_dynamic(). |
---|
269 | n/a | # Issue #15902 |
---|
270 | n/a | name = '_testimportmultiple' |
---|
271 | n/a | found = imp.find_module(name) |
---|
272 | n/a | if found[0] is not None: |
---|
273 | n/a | found[0].close() |
---|
274 | n/a | if found[2][2] != imp.C_EXTENSION: |
---|
275 | n/a | self.skipTest("found module doesn't appear to be a C extension") |
---|
276 | n/a | imp.load_module(name, None, *found[1:]) |
---|
277 | n/a | |
---|
278 | n/a | @requires_load_dynamic |
---|
279 | n/a | def test_issue24748_load_module_skips_sys_modules_check(self): |
---|
280 | n/a | name = 'test.imp_dummy' |
---|
281 | n/a | try: |
---|
282 | n/a | del sys.modules[name] |
---|
283 | n/a | except KeyError: |
---|
284 | n/a | pass |
---|
285 | n/a | try: |
---|
286 | n/a | module = importlib.import_module(name) |
---|
287 | n/a | spec = importlib.util.find_spec('_testmultiphase') |
---|
288 | n/a | module = imp.load_dynamic(name, spec.origin) |
---|
289 | n/a | self.assertEqual(module.__name__, name) |
---|
290 | n/a | self.assertEqual(module.__spec__.name, name) |
---|
291 | n/a | self.assertEqual(module.__spec__.origin, spec.origin) |
---|
292 | n/a | self.assertRaises(AttributeError, getattr, module, 'dummy_name') |
---|
293 | n/a | self.assertEqual(module.int_const, 1969) |
---|
294 | n/a | self.assertIs(sys.modules[name], module) |
---|
295 | n/a | finally: |
---|
296 | n/a | try: |
---|
297 | n/a | del sys.modules[name] |
---|
298 | n/a | except KeyError: |
---|
299 | n/a | pass |
---|
300 | n/a | |
---|
301 | n/a | @unittest.skipIf(sys.dont_write_bytecode, |
---|
302 | n/a | "test meaningful only when writing bytecode") |
---|
303 | n/a | def test_bug7732(self): |
---|
304 | n/a | with support.temp_cwd(): |
---|
305 | n/a | source = support.TESTFN + '.py' |
---|
306 | n/a | os.mkdir(source) |
---|
307 | n/a | self.assertRaisesRegex(ImportError, '^No module', |
---|
308 | n/a | imp.find_module, support.TESTFN, ["."]) |
---|
309 | n/a | |
---|
310 | n/a | def test_multiple_calls_to_get_data(self): |
---|
311 | n/a | # Issue #18755: make sure multiple calls to get_data() can succeed. |
---|
312 | n/a | loader = imp._LoadSourceCompatibility('imp', imp.__file__, |
---|
313 | n/a | open(imp.__file__)) |
---|
314 | n/a | loader.get_data(imp.__file__) # File should be closed |
---|
315 | n/a | loader.get_data(imp.__file__) # Will need to create a newly opened file |
---|
316 | n/a | |
---|
317 | n/a | |
---|
318 | n/a | class ReloadTests(unittest.TestCase): |
---|
319 | n/a | |
---|
320 | n/a | """Very basic tests to make sure that imp.reload() operates just like |
---|
321 | n/a | reload().""" |
---|
322 | n/a | |
---|
323 | n/a | def test_source(self): |
---|
324 | n/a | # XXX (ncoghlan): It would be nice to use test.support.CleanImport |
---|
325 | n/a | # here, but that breaks because the os module registers some |
---|
326 | n/a | # handlers in copy_reg on import. Since CleanImport doesn't |
---|
327 | n/a | # revert that registration, the module is left in a broken |
---|
328 | n/a | # state after reversion. Reinitialising the module contents |
---|
329 | n/a | # and just reverting os.environ to its previous state is an OK |
---|
330 | n/a | # workaround |
---|
331 | n/a | with support.EnvironmentVarGuard(): |
---|
332 | n/a | import os |
---|
333 | n/a | imp.reload(os) |
---|
334 | n/a | |
---|
335 | n/a | def test_extension(self): |
---|
336 | n/a | with support.CleanImport('time'): |
---|
337 | n/a | import time |
---|
338 | n/a | imp.reload(time) |
---|
339 | n/a | |
---|
340 | n/a | def test_builtin(self): |
---|
341 | n/a | with support.CleanImport('marshal'): |
---|
342 | n/a | import marshal |
---|
343 | n/a | imp.reload(marshal) |
---|
344 | n/a | |
---|
345 | n/a | def test_with_deleted_parent(self): |
---|
346 | n/a | # see #18681 |
---|
347 | n/a | from html import parser |
---|
348 | n/a | html = sys.modules.pop('html') |
---|
349 | n/a | def cleanup(): |
---|
350 | n/a | sys.modules['html'] = html |
---|
351 | n/a | self.addCleanup(cleanup) |
---|
352 | n/a | with self.assertRaisesRegex(ImportError, 'html'): |
---|
353 | n/a | imp.reload(parser) |
---|
354 | n/a | |
---|
355 | n/a | |
---|
356 | n/a | class PEP3147Tests(unittest.TestCase): |
---|
357 | n/a | """Tests of PEP 3147.""" |
---|
358 | n/a | |
---|
359 | n/a | tag = imp.get_tag() |
---|
360 | n/a | |
---|
361 | n/a | @unittest.skipUnless(sys.implementation.cache_tag is not None, |
---|
362 | n/a | 'requires sys.implementation.cache_tag not be None') |
---|
363 | n/a | def test_cache_from_source(self): |
---|
364 | n/a | # Given the path to a .py file, return the path to its PEP 3147 |
---|
365 | n/a | # defined .pyc file (i.e. under __pycache__). |
---|
366 | n/a | path = os.path.join('foo', 'bar', 'baz', 'qux.py') |
---|
367 | n/a | expect = os.path.join('foo', 'bar', 'baz', '__pycache__', |
---|
368 | n/a | 'qux.{}.pyc'.format(self.tag)) |
---|
369 | n/a | self.assertEqual(imp.cache_from_source(path, True), expect) |
---|
370 | n/a | |
---|
371 | n/a | @unittest.skipUnless(sys.implementation.cache_tag is not None, |
---|
372 | n/a | 'requires sys.implementation.cache_tag to not be ' |
---|
373 | n/a | 'None') |
---|
374 | n/a | def test_source_from_cache(self): |
---|
375 | n/a | # Given the path to a PEP 3147 defined .pyc file, return the path to |
---|
376 | n/a | # its source. This tests the good path. |
---|
377 | n/a | path = os.path.join('foo', 'bar', 'baz', '__pycache__', |
---|
378 | n/a | 'qux.{}.pyc'.format(self.tag)) |
---|
379 | n/a | expect = os.path.join('foo', 'bar', 'baz', 'qux.py') |
---|
380 | n/a | self.assertEqual(imp.source_from_cache(path), expect) |
---|
381 | n/a | |
---|
382 | n/a | |
---|
383 | n/a | class NullImporterTests(unittest.TestCase): |
---|
384 | n/a | @unittest.skipIf(support.TESTFN_UNENCODABLE is None, |
---|
385 | n/a | "Need an undecodeable filename") |
---|
386 | n/a | def test_unencodeable(self): |
---|
387 | n/a | name = support.TESTFN_UNENCODABLE |
---|
388 | n/a | os.mkdir(name) |
---|
389 | n/a | try: |
---|
390 | n/a | self.assertRaises(ImportError, imp.NullImporter, name) |
---|
391 | n/a | finally: |
---|
392 | n/a | os.rmdir(name) |
---|
393 | n/a | |
---|
394 | n/a | |
---|
395 | n/a | if __name__ == "__main__": |
---|
396 | n/a | unittest.main() |
---|