blob: 96771d8ba5321e1eea384d60117cf80637755433 [file] [log] [blame]
Neal Norwitz2294c0d2003-02-12 23:02:21 +00001import imp
Brett Cannonc0499522012-05-11 14:48:41 -04002import importlib
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003import os
4import os.path
Barry Warsaw28a691b2010-04-17 00:19:56 +00005import shutil
Brett Cannon8a9583e2008-09-04 05:04:25 +00006import sys
Benjamin Petersonee8712c2008-05-20 21:35:26 +00007from test import support
Brett Cannonc0499522012-05-11 14:48:41 -04008import unittest
9import warnings
Neal Norwitz2294c0d2003-02-12 23:02:21 +000010
Thomas Wouters89f507f2006-12-13 04:49:30 +000011class LockTests(unittest.TestCase):
Tim Peters579bed72003-04-26 14:31:24 +000012
Thomas Wouters89f507f2006-12-13 04:49:30 +000013 """Very basic test of import lock functions."""
Tim Peters579bed72003-04-26 14:31:24 +000014
Thomas Wouters89f507f2006-12-13 04:49:30 +000015 def verify_lock_state(self, expected):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000016 self.assertEqual(imp.lock_held(), expected,
Thomas Wouters89f507f2006-12-13 04:49:30 +000017 "expected imp.lock_held() to be %r" % expected)
18 def testLock(self):
19 LOOPS = 50
Tim Peters579bed72003-04-26 14:31:24 +000020
Thomas Wouters89f507f2006-12-13 04:49:30 +000021 # The import lock may already be held, e.g. if the test suite is run
22 # via "import test.autotest".
23 lock_held_at_start = imp.lock_held()
24 self.verify_lock_state(lock_held_at_start)
Tim Peters579bed72003-04-26 14:31:24 +000025
Thomas Wouters89f507f2006-12-13 04:49:30 +000026 for i in range(LOOPS):
27 imp.acquire_lock()
28 self.verify_lock_state(True)
Tim Peters579bed72003-04-26 14:31:24 +000029
Thomas Wouters89f507f2006-12-13 04:49:30 +000030 for i in range(LOOPS):
Neal Norwitz2294c0d2003-02-12 23:02:21 +000031 imp.release_lock()
Thomas Wouters89f507f2006-12-13 04:49:30 +000032
33 # The original state should be restored now.
34 self.verify_lock_state(lock_held_at_start)
35
36 if not lock_held_at_start:
37 try:
38 imp.release_lock()
39 except RuntimeError:
40 pass
41 else:
42 self.fail("release_lock() without lock should raise "
43 "RuntimeError")
Neal Norwitz2294c0d2003-02-12 23:02:21 +000044
Guido van Rossumce3a72a2007-10-19 23:16:50 +000045class ImportTests(unittest.TestCase):
Alexander Belopolskye8f58322010-10-15 16:28:20 +000046 def setUp(self):
47 mod = importlib.import_module('test.encoded_modules')
48 self.test_strings = mod.test_strings
49 self.test_path = mod.__path__
50
51 def test_import_encoded_module(self):
52 for modname, encoding, teststr in self.test_strings:
53 mod = importlib.import_module('test.encoded_modules.'
54 'module_' + modname)
55 self.assertEqual(teststr, mod.test)
Guido van Rossumce3a72a2007-10-19 23:16:50 +000056
57 def test_find_module_encoding(self):
Alexander Belopolskye8f58322010-10-15 16:28:20 +000058 for mod, encoding, _ in self.test_strings:
Brett Cannon749afa92010-10-29 23:47:23 +000059 with imp.find_module('module_' + mod, self.test_path)[0] as fd:
60 self.assertEqual(fd.encoding, encoding)
Guido van Rossumce3a72a2007-10-19 23:16:50 +000061
Victor Stinnerfe7c5b52011-04-05 01:48:03 +020062 path = [os.path.dirname(__file__)]
Brett Cannondd9a5692012-04-20 12:59:59 -040063 with self.assertRaises(SyntaxError):
64 imp.find_module('badsyntax_pep3120', path)
Victor Stinnerfe7c5b52011-04-05 01:48:03 +020065
Guido van Rossum40d20bc2007-10-22 00:09:51 +000066 def test_issue1267(self):
Alexander Belopolskye8f58322010-10-15 16:28:20 +000067 for mod, encoding, _ in self.test_strings:
68 fp, filename, info = imp.find_module('module_' + mod,
69 self.test_path)
Brett Cannon749afa92010-10-29 23:47:23 +000070 with fp:
71 self.assertNotEqual(fp, None)
72 self.assertEqual(fp.encoding, encoding)
73 self.assertEqual(fp.tell(), 0)
74 self.assertEqual(fp.readline(), '# test %s encoding\n'
75 % encoding)
Guido van Rossum40d20bc2007-10-22 00:09:51 +000076
77 fp, filename, info = imp.find_module("tokenize")
Brett Cannon749afa92010-10-29 23:47:23 +000078 with fp:
79 self.assertNotEqual(fp, None)
80 self.assertEqual(fp.encoding, "utf-8")
81 self.assertEqual(fp.tell(), 0)
82 self.assertEqual(fp.readline(),
83 '"""Tokenization help for Python programs.\n')
Guido van Rossum40d20bc2007-10-22 00:09:51 +000084
Brett Cannon8a9583e2008-09-04 05:04:25 +000085 def test_issue3594(self):
86 temp_mod_name = 'test_imp_helper'
87 sys.path.insert(0, '.')
88 try:
89 with open(temp_mod_name + '.py', 'w') as file:
90 file.write("# coding: cp1252\nu = 'test.test_imp'\n")
91 file, filename, info = imp.find_module(temp_mod_name)
92 file.close()
Ezio Melottib3aedd42010-11-20 19:04:17 +000093 self.assertEqual(file.encoding, 'cp1252')
Brett Cannon8a9583e2008-09-04 05:04:25 +000094 finally:
95 del sys.path[0]
96 support.unlink(temp_mod_name + '.py')
97 support.unlink(temp_mod_name + '.pyc')
98 support.unlink(temp_mod_name + '.pyo')
99
Guido van Rossum0ad59d42009-03-30 22:01:35 +0000100 def test_issue5604(self):
101 # Test cannot cover imp.load_compiled function.
102 # Martin von Loewis note what shared library cannot have non-ascii
103 # character because init_xxx function cannot be compiled
104 # and issue never happens for dynamic modules.
105 # But sources modified to follow generic way for processing pathes.
106
Ezio Melotti435b5312010-03-06 01:20:49 +0000107 # the return encoding could be uppercase or None
108 fs_encoding = sys.getfilesystemencoding()
Guido van Rossum0ad59d42009-03-30 22:01:35 +0000109
110 # covers utf-8 and Windows ANSI code pages
111 # one non-space symbol from every page
112 # (http://en.wikipedia.org/wiki/Code_page)
113 known_locales = {
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000114 'utf-8' : b'\xc3\xa4',
Guido van Rossum0ad59d42009-03-30 22:01:35 +0000115 'cp1250' : b'\x8C',
116 'cp1251' : b'\xc0',
117 'cp1252' : b'\xc0',
118 'cp1253' : b'\xc1',
119 'cp1254' : b'\xc0',
120 'cp1255' : b'\xe0',
121 'cp1256' : b'\xe0',
122 'cp1257' : b'\xc0',
123 'cp1258' : b'\xc0',
124 }
125
Florent Xicluna21164ce2010-03-20 20:30:53 +0000126 if sys.platform == 'darwin':
127 self.assertEqual(fs_encoding, 'utf-8')
128 # Mac OS X uses the Normal Form D decomposition
129 # http://developer.apple.com/mac/library/qa/qa2001/qa1173.html
130 special_char = b'a\xcc\x88'
131 else:
132 special_char = known_locales.get(fs_encoding)
133
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000134 if not special_char:
Ezio Melotti76e0d1a2010-03-05 15:08:19 +0000135 self.skipTest("can't run this test with %s as filesystem encoding"
136 % fs_encoding)
137 decoded_char = special_char.decode(fs_encoding)
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000138 temp_mod_name = 'test_imp_helper_' + decoded_char
139 test_package_name = 'test_imp_helper_package_' + decoded_char
140 init_file_name = os.path.join(test_package_name, '__init__.py')
141 try:
Ezio Melotti41a6b042010-03-06 01:50:25 +0000142 # if the curdir is not in sys.path the test fails when run with
143 # ./python ./Lib/test/regrtest.py test_imp
144 sys.path.insert(0, os.curdir)
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000145 with open(temp_mod_name + '.py', 'w') as file:
146 file.write('a = 1\n')
147 file, filename, info = imp.find_module(temp_mod_name)
Brett Cannon749afa92010-10-29 23:47:23 +0000148 with file:
149 self.assertIsNotNone(file)
150 self.assertTrue(filename[:-3].endswith(temp_mod_name))
151 self.assertEqual(info[0], '.py')
152 self.assertEqual(info[1], 'U')
153 self.assertEqual(info[2], imp.PY_SOURCE)
Guido van Rossum0ad59d42009-03-30 22:01:35 +0000154
Brett Cannon749afa92010-10-29 23:47:23 +0000155 mod = imp.load_module(temp_mod_name, file, filename, info)
156 self.assertEqual(mod.a, 1)
Guido van Rossum0ad59d42009-03-30 22:01:35 +0000157
Brett Cannonc0499522012-05-11 14:48:41 -0400158 with warnings.catch_warnings():
159 warnings.simplefilter('ignore')
160 mod = imp.load_source(temp_mod_name, temp_mod_name + '.py')
Ezio Melotti435b5312010-03-06 01:20:49 +0000161 self.assertEqual(mod.a, 1)
Guido van Rossum0ad59d42009-03-30 22:01:35 +0000162
Brett Cannonc0499522012-05-11 14:48:41 -0400163 with warnings.catch_warnings():
164 warnings.simplefilter('ignore')
Ezio Melottie5e7a7c2013-03-16 21:49:20 +0200165 if not sys.dont_write_bytecode:
166 mod = imp.load_compiled(
167 temp_mod_name,
168 imp.cache_from_source(temp_mod_name + '.py'))
Ezio Melotti435b5312010-03-06 01:20:49 +0000169 self.assertEqual(mod.a, 1)
Guido van Rossum0ad59d42009-03-30 22:01:35 +0000170
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000171 if not os.path.exists(test_package_name):
172 os.mkdir(test_package_name)
173 with open(init_file_name, 'w') as file:
174 file.write('b = 2\n')
Brett Cannonc0499522012-05-11 14:48:41 -0400175 with warnings.catch_warnings():
176 warnings.simplefilter('ignore')
177 package = imp.load_package(test_package_name, test_package_name)
Ezio Melotti435b5312010-03-06 01:20:49 +0000178 self.assertEqual(package.b, 2)
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000179 finally:
Ezio Melotti41a6b042010-03-06 01:50:25 +0000180 del sys.path[0]
Ezio Melotti435b5312010-03-06 01:20:49 +0000181 for ext in ('.py', '.pyc', '.pyo'):
182 support.unlink(temp_mod_name + ext)
183 support.unlink(init_file_name + ext)
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000184 support.rmtree(test_package_name)
Guido van Rossum0ad59d42009-03-30 22:01:35 +0000185
Victor Stinnerc68b6aa2011-04-23 00:41:19 +0200186 def test_issue9319(self):
Antoine Pitrou11846902011-04-25 21:39:49 +0200187 path = os.path.dirname(__file__)
Victor Stinner7fdd0fe2011-04-23 01:24:11 +0200188 self.assertRaises(SyntaxError,
Antoine Pitrou11846902011-04-25 21:39:49 +0200189 imp.find_module, "badsyntax_pep3120", [path])
Victor Stinnerc68b6aa2011-04-23 00:41:19 +0200190
Nick Coghlan91b9f132012-09-01 00:13:45 +1000191 def test_load_from_source(self):
192 # Verify that the imp module can correctly load and find .py files
193 # XXX (ncoghlan): It would be nice to use support.CleanImport
194 # here, but that breaks because the os module registers some
195 # handlers in copy_reg on import. Since CleanImport doesn't
196 # revert that registration, the module is left in a broken
197 # state after reversion. Reinitialising the module contents
198 # and just reverting os.environ to its previous state is an OK
199 # workaround
200 orig_path = os.path
201 orig_getenv = os.getenv
202 with support.EnvironmentVarGuard():
203 x = imp.find_module("os")
204 self.addCleanup(x[0].close)
205 new_os = imp.load_module("os", *x)
206 self.assertIs(os, new_os)
207 self.assertIs(orig_path, new_os.path)
208 self.assertIsNot(orig_getenv, new_os.getenv)
209
210 @support.cpython_only
Brett Cannon9d0f7722013-05-03 10:37:08 -0400211 @unittest.skipIf(not hasattr(imp, 'load_dynamic'),
212 'imp.load_dynamic() required')
Nick Coghlan91b9f132012-09-01 00:13:45 +1000213 def test_issue15828_load_extensions(self):
214 # Issue 15828 picked up that the adapter between the old imp API
215 # and importlib couldn't handle C extensions
216 example = "_heapq"
217 x = imp.find_module(example)
Brett Cannon848cdfd2012-08-31 11:31:20 -0400218 file_ = x[0]
219 if file_ is not None:
220 self.addCleanup(file_.close)
Nick Coghlan91b9f132012-09-01 00:13:45 +1000221 mod = imp.load_module(example, *x)
222 self.assertEqual(mod.__name__, example)
223
Brett Cannonf0434e62012-04-20 15:22:50 -0400224 def test_load_dynamic_ImportError_path(self):
225 # Issue #1559549 added `name` and `path` attributes to ImportError
226 # in order to provide better detail. Issue #10854 implemented those
227 # attributes on import failures of extensions on Windows.
228 path = 'bogus file path'
229 name = 'extension'
230 with self.assertRaises(ImportError) as err:
231 imp.load_dynamic(name, path)
232 self.assertIn(path, err.exception.path)
233 self.assertEqual(name, err.exception.name)
234
Brett Cannon9d0f7722013-05-03 10:37:08 -0400235 @support.cpython_only
236 @unittest.skipIf(not hasattr(imp, 'load_dynamic'),
237 'imp.load_dynamic() required')
238 def test_load_module_extension_file_is_None(self):
239 # When loading an extension module and the file is None, open one
240 # on the behalf of imp.load_dynamic().
241 # Issue #15902
242 name = '_heapq'
243 found = imp.find_module(name)
Brett Cannon9d0f7722013-05-03 10:37:08 -0400244 found[0].close()
Brett Cannondea2ec42013-05-04 18:11:12 -0400245 if found[2][2] != imp.C_EXTENSION:
246 return
Brett Cannon9d0f7722013-05-03 10:37:08 -0400247 imp.load_module(name, None, *found[1:])
248
Guido van Rossum0ad59d42009-03-30 22:01:35 +0000249
Nick Coghlan6ead5522009-10-18 13:19:33 +0000250class ReloadTests(unittest.TestCase):
251
252 """Very basic tests to make sure that imp.reload() operates just like
253 reload()."""
254
255 def test_source(self):
Florent Xicluna97133722010-03-20 20:31:34 +0000256 # XXX (ncoghlan): It would be nice to use test.support.CleanImport
Nick Coghlan6ead5522009-10-18 13:19:33 +0000257 # here, but that breaks because the os module registers some
258 # handlers in copy_reg on import. Since CleanImport doesn't
259 # revert that registration, the module is left in a broken
260 # state after reversion. Reinitialising the module contents
261 # and just reverting os.environ to its previous state is an OK
262 # workaround
263 with support.EnvironmentVarGuard():
264 import os
265 imp.reload(os)
266
267 def test_extension(self):
268 with support.CleanImport('time'):
269 import time
270 imp.reload(time)
271
272 def test_builtin(self):
273 with support.CleanImport('marshal'):
274 import marshal
275 imp.reload(marshal)
Christian Heimes13a7a212008-01-07 17:13:09 +0000276
Guido van Rossum40d20bc2007-10-22 00:09:51 +0000277
Barry Warsaw28a691b2010-04-17 00:19:56 +0000278class PEP3147Tests(unittest.TestCase):
279 """Tests of PEP 3147."""
280
281 tag = imp.get_tag()
282
Brett Cannon19a2f592012-07-09 13:58:07 -0400283 @unittest.skipUnless(sys.implementation.cache_tag is not None,
284 'requires sys.implementation.cache_tag not be None')
Barry Warsaw28a691b2010-04-17 00:19:56 +0000285 def test_cache_from_source(self):
286 # Given the path to a .py file, return the path to its PEP 3147
287 # defined .pyc file (i.e. under __pycache__).
Brett Cannon410e88d2012-04-22 13:29:47 -0400288 path = os.path.join('foo', 'bar', 'baz', 'qux.py')
289 expect = os.path.join('foo', 'bar', 'baz', '__pycache__',
290 'qux.{}.pyc'.format(self.tag))
291 self.assertEqual(imp.cache_from_source(path, True), expect)
292
Brett Cannon19a2f592012-07-09 13:58:07 -0400293 def test_cache_from_source_no_cache_tag(self):
294 # Non cache tag means NotImplementedError.
295 with support.swap_attr(sys.implementation, 'cache_tag', None):
296 with self.assertRaises(NotImplementedError):
297 imp.cache_from_source('whatever.py')
298
Brett Cannon410e88d2012-04-22 13:29:47 -0400299 def test_cache_from_source_no_dot(self):
300 # Directory with a dot, filename without dot.
301 path = os.path.join('foo.bar', 'file')
302 expect = os.path.join('foo.bar', '__pycache__',
303 'file{}.pyc'.format(self.tag))
304 self.assertEqual(imp.cache_from_source(path, True), expect)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000305
306 def test_cache_from_source_optimized(self):
307 # Given the path to a .py file, return the path to its PEP 3147
308 # defined .pyo file (i.e. under __pycache__).
Brett Cannon410e88d2012-04-22 13:29:47 -0400309 path = os.path.join('foo', 'bar', 'baz', 'qux.py')
310 expect = os.path.join('foo', 'bar', 'baz', '__pycache__',
311 'qux.{}.pyo'.format(self.tag))
312 self.assertEqual(imp.cache_from_source(path, False), expect)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000313
314 def test_cache_from_source_cwd(self):
Brett Cannon410e88d2012-04-22 13:29:47 -0400315 path = 'foo.py'
316 expect = os.path.join('__pycache__', 'foo.{}.pyc'.format(self.tag))
317 self.assertEqual(imp.cache_from_source(path, True), expect)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000318
319 def test_cache_from_source_override(self):
320 # When debug_override is not None, it can be any true-ish or false-ish
321 # value.
Brett Cannon410e88d2012-04-22 13:29:47 -0400322 path = os.path.join('foo', 'bar', 'baz.py')
323 partial_expect = os.path.join('foo', 'bar', '__pycache__',
324 'baz.{}.py'.format(self.tag))
325 self.assertEqual(imp.cache_from_source(path, []), partial_expect + 'o')
326 self.assertEqual(imp.cache_from_source(path, [17]),
327 partial_expect + 'c')
Barry Warsaw28a691b2010-04-17 00:19:56 +0000328 # However if the bool-ishness can't be determined, the exception
329 # propagates.
330 class Bearish:
331 def __bool__(self): raise RuntimeError
Brett Cannon410e88d2012-04-22 13:29:47 -0400332 with self.assertRaises(RuntimeError):
333 imp.cache_from_source('/foo/bar/baz.py', Bearish())
Barry Warsaw28a691b2010-04-17 00:19:56 +0000334
Brett Cannon410e88d2012-04-22 13:29:47 -0400335 @unittest.skipUnless(os.sep == '\\' and os.altsep == '/',
Barry Warsaw28a691b2010-04-17 00:19:56 +0000336 'test meaningful only where os.altsep is defined')
337 def test_sep_altsep_and_sep_cache_from_source(self):
338 # Windows path and PEP 3147 where sep is right of altsep.
339 self.assertEqual(
340 imp.cache_from_source('\\foo\\bar\\baz/qux.py', True),
Brett Cannon410e88d2012-04-22 13:29:47 -0400341 '\\foo\\bar\\baz\\__pycache__\\qux.{}.pyc'.format(self.tag))
Barry Warsaw28a691b2010-04-17 00:19:56 +0000342
Brett Cannon19a2f592012-07-09 13:58:07 -0400343 @unittest.skipUnless(sys.implementation.cache_tag is not None,
344 'requires sys.implementation.cache_tag to not be '
345 'None')
Barry Warsaw28a691b2010-04-17 00:19:56 +0000346 def test_source_from_cache(self):
347 # Given the path to a PEP 3147 defined .pyc file, return the path to
348 # its source. This tests the good path.
Brett Cannon410e88d2012-04-22 13:29:47 -0400349 path = os.path.join('foo', 'bar', 'baz', '__pycache__',
350 'qux.{}.pyc'.format(self.tag))
351 expect = os.path.join('foo', 'bar', 'baz', 'qux.py')
352 self.assertEqual(imp.source_from_cache(path), expect)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000353
Brett Cannon19a2f592012-07-09 13:58:07 -0400354 def test_source_from_cache_no_cache_tag(self):
355 # If sys.implementation.cache_tag is None, raise NotImplementedError.
356 path = os.path.join('blah', '__pycache__', 'whatever.pyc')
357 with support.swap_attr(sys.implementation, 'cache_tag', None):
358 with self.assertRaises(NotImplementedError):
359 imp.source_from_cache(path)
360
Barry Warsaw28a691b2010-04-17 00:19:56 +0000361 def test_source_from_cache_bad_path(self):
362 # When the path to a pyc file is not in PEP 3147 format, a ValueError
363 # is raised.
364 self.assertRaises(
365 ValueError, imp.source_from_cache, '/foo/bar/bazqux.pyc')
366
367 def test_source_from_cache_no_slash(self):
368 # No slashes at all in path -> ValueError
369 self.assertRaises(
370 ValueError, imp.source_from_cache, 'foo.cpython-32.pyc')
371
372 def test_source_from_cache_too_few_dots(self):
373 # Too few dots in final path component -> ValueError
374 self.assertRaises(
375 ValueError, imp.source_from_cache, '__pycache__/foo.pyc')
376
377 def test_source_from_cache_too_many_dots(self):
378 # Too many dots in final path component -> ValueError
379 self.assertRaises(
380 ValueError, imp.source_from_cache,
381 '__pycache__/foo.cpython-32.foo.pyc')
382
383 def test_source_from_cache_no__pycache__(self):
384 # Another problem with the path -> ValueError
385 self.assertRaises(
386 ValueError, imp.source_from_cache,
387 '/foo/bar/foo.cpython-32.foo.pyc')
388
389 def test_package___file__(self):
Antoine Pitrou06e37582012-06-23 17:27:56 +0200390 try:
391 m = __import__('pep3147')
392 except ImportError:
393 pass
394 else:
395 self.fail("pep3147 module already exists: %r" % (m,))
Barry Warsaw28a691b2010-04-17 00:19:56 +0000396 # Test that a package's __file__ points to the right source directory.
397 os.mkdir('pep3147')
398 sys.path.insert(0, os.curdir)
399 def cleanup():
400 if sys.path[0] == os.curdir:
401 del sys.path[0]
402 shutil.rmtree('pep3147')
403 self.addCleanup(cleanup)
404 # Touch the __init__.py file.
Victor Stinnerbf816222011-06-30 23:25:47 +0200405 support.create_empty_file('pep3147/__init__.py')
Antoine Pitrou4f92a682012-02-26 18:09:50 +0100406 importlib.invalidate_caches()
Antoine Pitrouabe72d72012-02-22 01:11:31 +0100407 expected___file__ = os.sep.join(('.', 'pep3147', '__init__.py'))
Barry Warsaw28a691b2010-04-17 00:19:56 +0000408 m = __import__('pep3147')
Antoine Pitrou9a4d7dd2012-02-27 22:01:25 +0100409 self.assertEqual(m.__file__, expected___file__, (m.__file__, m.__path__, sys.path, sys.path_importer_cache))
Barry Warsaw28a691b2010-04-17 00:19:56 +0000410 # Ensure we load the pyc file.
Antoine Pitrou037615e2012-02-22 02:30:09 +0100411 support.unload('pep3147')
Barry Warsaw28a691b2010-04-17 00:19:56 +0000412 m = __import__('pep3147')
Antoine Pitrou037615e2012-02-22 02:30:09 +0100413 support.unload('pep3147')
Antoine Pitrou9a4d7dd2012-02-27 22:01:25 +0100414 self.assertEqual(m.__file__, expected___file__, (m.__file__, m.__path__, sys.path, sys.path_importer_cache))
Barry Warsaw28a691b2010-04-17 00:19:56 +0000415
416
Victor Stinner1a4d12d2010-08-13 13:07:29 +0000417class NullImporterTests(unittest.TestCase):
Victor Stinner09c449c2010-08-13 22:23:24 +0000418 @unittest.skipIf(support.TESTFN_UNENCODABLE is None,
Victor Stinner1a4d12d2010-08-13 13:07:29 +0000419 "Need an undecodeable filename")
420 def test_unencodeable(self):
Victor Stinner09c449c2010-08-13 22:23:24 +0000421 name = support.TESTFN_UNENCODABLE
Victor Stinner1a4d12d2010-08-13 13:07:29 +0000422 os.mkdir(name)
423 try:
424 self.assertRaises(ImportError, imp.NullImporter, name)
425 finally:
426 os.rmdir(name)
427
428
Neal Norwitz996acf12003-02-17 14:51:41 +0000429def test_main():
Hirokazu Yamamoto36144092008-09-09 07:33:27 +0000430 tests = [
431 ImportTests,
Barry Warsaw28a691b2010-04-17 00:19:56 +0000432 PEP3147Tests,
Nick Coghlan6ead5522009-10-18 13:19:33 +0000433 ReloadTests,
Victor Stinner1a4d12d2010-08-13 13:07:29 +0000434 NullImporterTests,
Barry Warsaw28a691b2010-04-17 00:19:56 +0000435 ]
Hirokazu Yamamoto36144092008-09-09 07:33:27 +0000436 try:
437 import _thread
438 except ImportError:
439 pass
440 else:
441 tests.append(LockTests)
442 support.run_unittest(*tests)
Neal Norwitz996acf12003-02-17 14:51:41 +0000443
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000444if __name__ == "__main__":
Neal Norwitz996acf12003-02-17 14:51:41 +0000445 test_main()