blob: 233d3da960be29745edd3a84d7a26e499f94e0db [file] [log] [blame]
Neal Norwitz2294c0d2003-02-12 23:02:21 +00001import imp
Guido van Rossum0ad59d42009-03-30 22:01:35 +00002import os
3import os.path
Barry Warsaw28a691b2010-04-17 00:19:56 +00004import shutil
Brett Cannon8a9583e2008-09-04 05:04:25 +00005import sys
Thomas Wouters89f507f2006-12-13 04:49:30 +00006import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00007from test import support
Neal Norwitz2294c0d2003-02-12 23:02:21 +00008
Neal Norwitz2294c0d2003-02-12 23:02:21 +00009
Thomas Wouters89f507f2006-12-13 04:49:30 +000010class LockTests(unittest.TestCase):
Tim Peters579bed72003-04-26 14:31:24 +000011
Thomas Wouters89f507f2006-12-13 04:49:30 +000012 """Very basic test of import lock functions."""
Tim Peters579bed72003-04-26 14:31:24 +000013
Thomas Wouters89f507f2006-12-13 04:49:30 +000014 def verify_lock_state(self, expected):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000015 self.assertEqual(imp.lock_held(), expected,
Thomas Wouters89f507f2006-12-13 04:49:30 +000016 "expected imp.lock_held() to be %r" % expected)
17 def testLock(self):
18 LOOPS = 50
Tim Peters579bed72003-04-26 14:31:24 +000019
Thomas Wouters89f507f2006-12-13 04:49:30 +000020 # The import lock may already be held, e.g. if the test suite is run
21 # via "import test.autotest".
22 lock_held_at_start = imp.lock_held()
23 self.verify_lock_state(lock_held_at_start)
Tim Peters579bed72003-04-26 14:31:24 +000024
Thomas Wouters89f507f2006-12-13 04:49:30 +000025 for i in range(LOOPS):
26 imp.acquire_lock()
27 self.verify_lock_state(True)
Tim Peters579bed72003-04-26 14:31:24 +000028
Thomas Wouters89f507f2006-12-13 04:49:30 +000029 for i in range(LOOPS):
Neal Norwitz2294c0d2003-02-12 23:02:21 +000030 imp.release_lock()
Thomas Wouters89f507f2006-12-13 04:49:30 +000031
32 # The original state should be restored now.
33 self.verify_lock_state(lock_held_at_start)
34
35 if not lock_held_at_start:
36 try:
37 imp.release_lock()
38 except RuntimeError:
39 pass
40 else:
41 self.fail("release_lock() without lock should raise "
42 "RuntimeError")
Neal Norwitz2294c0d2003-02-12 23:02:21 +000043
Guido van Rossumce3a72a2007-10-19 23:16:50 +000044class ImportTests(unittest.TestCase):
45
46 def test_find_module_encoding(self):
Benjamin Peterson335d2c72010-07-05 14:59:40 +000047 fd = imp.find_module("pydoc")[0]
Guido van Rossumce3a72a2007-10-19 23:16:50 +000048 self.assertEqual(fd.encoding, "iso-8859-1")
49
Guido van Rossum40d20bc2007-10-22 00:09:51 +000050 def test_issue1267(self):
51 fp, filename, info = imp.find_module("pydoc")
52 self.assertNotEqual(fp, None)
53 self.assertEqual(fp.encoding, "iso-8859-1")
54 self.assertEqual(fp.tell(), 0)
Benjamin Peterson25bfc552010-03-11 23:59:55 +000055 self.assertEqual(fp.readline(), '#!/usr/bin/env python3\n')
Guido van Rossum40d20bc2007-10-22 00:09:51 +000056 fp.close()
57
58 fp, filename, info = imp.find_module("tokenize")
59 self.assertNotEqual(fp, None)
60 self.assertEqual(fp.encoding, "utf-8")
61 self.assertEqual(fp.tell(), 0)
62 self.assertEqual(fp.readline(),
63 '"""Tokenization help for Python programs.\n')
64 fp.close()
65
Brett Cannon8a9583e2008-09-04 05:04:25 +000066 def test_issue3594(self):
67 temp_mod_name = 'test_imp_helper'
68 sys.path.insert(0, '.')
69 try:
70 with open(temp_mod_name + '.py', 'w') as file:
71 file.write("# coding: cp1252\nu = 'test.test_imp'\n")
72 file, filename, info = imp.find_module(temp_mod_name)
73 file.close()
74 self.assertEquals(file.encoding, 'cp1252')
75 finally:
76 del sys.path[0]
77 support.unlink(temp_mod_name + '.py')
78 support.unlink(temp_mod_name + '.pyc')
79 support.unlink(temp_mod_name + '.pyo')
80
Guido van Rossum0ad59d42009-03-30 22:01:35 +000081 def test_issue5604(self):
82 # Test cannot cover imp.load_compiled function.
83 # Martin von Loewis note what shared library cannot have non-ascii
84 # character because init_xxx function cannot be compiled
85 # and issue never happens for dynamic modules.
86 # But sources modified to follow generic way for processing pathes.
87
Ezio Melotti435b5312010-03-06 01:20:49 +000088 # the return encoding could be uppercase or None
89 fs_encoding = sys.getfilesystemencoding()
Ezio Melotti06dbff32010-03-05 15:17:26 +000090 fs_encoding = fs_encoding.lower() if fs_encoding else 'ascii'
Guido van Rossum0ad59d42009-03-30 22:01:35 +000091
92 # covers utf-8 and Windows ANSI code pages
93 # one non-space symbol from every page
94 # (http://en.wikipedia.org/wiki/Code_page)
95 known_locales = {
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +000096 'utf-8' : b'\xc3\xa4',
Guido van Rossum0ad59d42009-03-30 22:01:35 +000097 'cp1250' : b'\x8C',
98 'cp1251' : b'\xc0',
99 'cp1252' : b'\xc0',
100 'cp1253' : b'\xc1',
101 'cp1254' : b'\xc0',
102 'cp1255' : b'\xe0',
103 'cp1256' : b'\xe0',
104 'cp1257' : b'\xc0',
105 'cp1258' : b'\xc0',
106 }
107
Florent Xicluna21164ce2010-03-20 20:30:53 +0000108 if sys.platform == 'darwin':
109 self.assertEqual(fs_encoding, 'utf-8')
110 # Mac OS X uses the Normal Form D decomposition
111 # http://developer.apple.com/mac/library/qa/qa2001/qa1173.html
112 special_char = b'a\xcc\x88'
113 else:
114 special_char = known_locales.get(fs_encoding)
115
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000116 if not special_char:
Ezio Melotti76e0d1a2010-03-05 15:08:19 +0000117 self.skipTest("can't run this test with %s as filesystem encoding"
118 % fs_encoding)
119 decoded_char = special_char.decode(fs_encoding)
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000120 temp_mod_name = 'test_imp_helper_' + decoded_char
121 test_package_name = 'test_imp_helper_package_' + decoded_char
122 init_file_name = os.path.join(test_package_name, '__init__.py')
123 try:
Ezio Melotti41a6b042010-03-06 01:50:25 +0000124 # if the curdir is not in sys.path the test fails when run with
125 # ./python ./Lib/test/regrtest.py test_imp
126 sys.path.insert(0, os.curdir)
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000127 with open(temp_mod_name + '.py', 'w') as file:
128 file.write('a = 1\n')
129 file, filename, info = imp.find_module(temp_mod_name)
Ezio Melotti435b5312010-03-06 01:20:49 +0000130 self.assertIsNotNone(file)
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000131 self.assertTrue(filename[:-3].endswith(temp_mod_name))
Ezio Melotti435b5312010-03-06 01:20:49 +0000132 self.assertEqual(info[0], '.py')
133 self.assertEqual(info[1], 'U')
134 self.assertEqual(info[2], imp.PY_SOURCE)
Guido van Rossum0ad59d42009-03-30 22:01:35 +0000135
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000136 mod = imp.load_module(temp_mod_name, file, filename, info)
Ezio Melotti435b5312010-03-06 01:20:49 +0000137 self.assertEqual(mod.a, 1)
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000138 file.close()
Guido van Rossum0ad59d42009-03-30 22:01:35 +0000139
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000140 mod = imp.load_source(temp_mod_name, temp_mod_name + '.py')
Ezio Melotti435b5312010-03-06 01:20:49 +0000141 self.assertEqual(mod.a, 1)
Guido van Rossum0ad59d42009-03-30 22:01:35 +0000142
Barry Warsaw28a691b2010-04-17 00:19:56 +0000143 mod = imp.load_compiled(
144 temp_mod_name, imp.cache_from_source(temp_mod_name + '.py'))
Ezio Melotti435b5312010-03-06 01:20:49 +0000145 self.assertEqual(mod.a, 1)
Guido van Rossum0ad59d42009-03-30 22:01:35 +0000146
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000147 if not os.path.exists(test_package_name):
148 os.mkdir(test_package_name)
149 with open(init_file_name, 'w') as file:
150 file.write('b = 2\n')
151 package = imp.load_package(test_package_name, test_package_name)
Ezio Melotti435b5312010-03-06 01:20:49 +0000152 self.assertEqual(package.b, 2)
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000153 finally:
Ezio Melotti41a6b042010-03-06 01:50:25 +0000154 del sys.path[0]
Ezio Melotti435b5312010-03-06 01:20:49 +0000155 for ext in ('.py', '.pyc', '.pyo'):
156 support.unlink(temp_mod_name + ext)
157 support.unlink(init_file_name + ext)
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000158 support.rmtree(test_package_name)
Guido van Rossum0ad59d42009-03-30 22:01:35 +0000159
160
Nick Coghlan6ead5522009-10-18 13:19:33 +0000161class ReloadTests(unittest.TestCase):
162
163 """Very basic tests to make sure that imp.reload() operates just like
164 reload()."""
165
166 def test_source(self):
Florent Xicluna97133722010-03-20 20:31:34 +0000167 # XXX (ncoghlan): It would be nice to use test.support.CleanImport
Nick Coghlan6ead5522009-10-18 13:19:33 +0000168 # here, but that breaks because the os module registers some
169 # handlers in copy_reg on import. Since CleanImport doesn't
170 # revert that registration, the module is left in a broken
171 # state after reversion. Reinitialising the module contents
172 # and just reverting os.environ to its previous state is an OK
173 # workaround
174 with support.EnvironmentVarGuard():
175 import os
176 imp.reload(os)
177
178 def test_extension(self):
179 with support.CleanImport('time'):
180 import time
181 imp.reload(time)
182
183 def test_builtin(self):
184 with support.CleanImport('marshal'):
185 import marshal
186 imp.reload(marshal)
Christian Heimes13a7a212008-01-07 17:13:09 +0000187
Guido van Rossum40d20bc2007-10-22 00:09:51 +0000188
Barry Warsaw28a691b2010-04-17 00:19:56 +0000189class PEP3147Tests(unittest.TestCase):
190 """Tests of PEP 3147."""
191
192 tag = imp.get_tag()
193
194 def test_cache_from_source(self):
195 # Given the path to a .py file, return the path to its PEP 3147
196 # defined .pyc file (i.e. under __pycache__).
197 self.assertEqual(
198 imp.cache_from_source('/foo/bar/baz/qux.py', True),
199 '/foo/bar/baz/__pycache__/qux.{}.pyc'.format(self.tag))
200
201 def test_cache_from_source_optimized(self):
202 # Given the path to a .py file, return the path to its PEP 3147
203 # defined .pyo file (i.e. under __pycache__).
204 self.assertEqual(
205 imp.cache_from_source('/foo/bar/baz/qux.py', False),
206 '/foo/bar/baz/__pycache__/qux.{}.pyo'.format(self.tag))
207
208 def test_cache_from_source_cwd(self):
209 self.assertEqual(imp.cache_from_source('foo.py', True),
210 os.sep.join(('__pycache__',
211 'foo.{}.pyc'.format(self.tag))))
212
213 def test_cache_from_source_override(self):
214 # When debug_override is not None, it can be any true-ish or false-ish
215 # value.
216 self.assertEqual(
217 imp.cache_from_source('/foo/bar/baz.py', []),
218 '/foo/bar/__pycache__/baz.{}.pyo'.format(self.tag))
219 self.assertEqual(
220 imp.cache_from_source('/foo/bar/baz.py', [17]),
221 '/foo/bar/__pycache__/baz.{}.pyc'.format(self.tag))
222 # However if the bool-ishness can't be determined, the exception
223 # propagates.
224 class Bearish:
225 def __bool__(self): raise RuntimeError
226 self.assertRaises(
227 RuntimeError,
228 imp.cache_from_source, '/foo/bar/baz.py', Bearish())
229
230 @unittest.skipIf(os.altsep is None,
231 'test meaningful only where os.altsep is defined')
232 def test_altsep_cache_from_source(self):
233 # Windows path and PEP 3147.
234 self.assertEqual(
235 imp.cache_from_source('\\foo\\bar\\baz\\qux.py', True),
236 '\\foo\\bar\\baz\\__pycache__\\qux.{}.pyc'.format(self.tag))
237
238 @unittest.skipIf(os.altsep is None,
239 'test meaningful only where os.altsep is defined')
240 def test_altsep_and_sep_cache_from_source(self):
241 # Windows path and PEP 3147 where altsep is right of sep.
242 self.assertEqual(
243 imp.cache_from_source('\\foo\\bar/baz\\qux.py', True),
244 '\\foo\\bar/baz\\__pycache__\\qux.{}.pyc'.format(self.tag))
245
246 @unittest.skipIf(os.altsep is None,
247 'test meaningful only where os.altsep is defined')
248 def test_sep_altsep_and_sep_cache_from_source(self):
249 # Windows path and PEP 3147 where sep is right of altsep.
250 self.assertEqual(
251 imp.cache_from_source('\\foo\\bar\\baz/qux.py', True),
252 '\\foo\\bar\\baz/__pycache__/qux.{}.pyc'.format(self.tag))
253
254 def test_source_from_cache(self):
255 # Given the path to a PEP 3147 defined .pyc file, return the path to
256 # its source. This tests the good path.
257 self.assertEqual(imp.source_from_cache(
258 '/foo/bar/baz/__pycache__/qux.{}.pyc'.format(self.tag)),
259 '/foo/bar/baz/qux.py')
260
261 def test_source_from_cache_bad_path(self):
262 # When the path to a pyc file is not in PEP 3147 format, a ValueError
263 # is raised.
264 self.assertRaises(
265 ValueError, imp.source_from_cache, '/foo/bar/bazqux.pyc')
266
267 def test_source_from_cache_no_slash(self):
268 # No slashes at all in path -> ValueError
269 self.assertRaises(
270 ValueError, imp.source_from_cache, 'foo.cpython-32.pyc')
271
272 def test_source_from_cache_too_few_dots(self):
273 # Too few dots in final path component -> ValueError
274 self.assertRaises(
275 ValueError, imp.source_from_cache, '__pycache__/foo.pyc')
276
277 def test_source_from_cache_too_many_dots(self):
278 # Too many dots in final path component -> ValueError
279 self.assertRaises(
280 ValueError, imp.source_from_cache,
281 '__pycache__/foo.cpython-32.foo.pyc')
282
283 def test_source_from_cache_no__pycache__(self):
284 # Another problem with the path -> ValueError
285 self.assertRaises(
286 ValueError, imp.source_from_cache,
287 '/foo/bar/foo.cpython-32.foo.pyc')
288
289 def test_package___file__(self):
290 # Test that a package's __file__ points to the right source directory.
291 os.mkdir('pep3147')
292 sys.path.insert(0, os.curdir)
293 def cleanup():
294 if sys.path[0] == os.curdir:
295 del sys.path[0]
296 shutil.rmtree('pep3147')
297 self.addCleanup(cleanup)
298 # Touch the __init__.py file.
299 with open('pep3147/__init__.py', 'w'):
300 pass
301 m = __import__('pep3147')
302 # Ensure we load the pyc file.
303 support.forget('pep3147')
304 m = __import__('pep3147')
305 self.assertEqual(m.__file__,
306 os.sep.join(('.', 'pep3147', '__init__.py')))
307
308
Victor Stinner1a4d12d2010-08-13 13:07:29 +0000309class NullImporterTests(unittest.TestCase):
310 @unittest.skipIf(support.TESTFN_UNENCODEABLE is None,
311 "Need an undecodeable filename")
312 def test_unencodeable(self):
313 name = support.TESTFN_UNENCODEABLE
314 os.mkdir(name)
315 try:
316 self.assertRaises(ImportError, imp.NullImporter, name)
317 finally:
318 os.rmdir(name)
319
320
Neal Norwitz996acf12003-02-17 14:51:41 +0000321def test_main():
Hirokazu Yamamoto36144092008-09-09 07:33:27 +0000322 tests = [
323 ImportTests,
Barry Warsaw28a691b2010-04-17 00:19:56 +0000324 PEP3147Tests,
Nick Coghlan6ead5522009-10-18 13:19:33 +0000325 ReloadTests,
Victor Stinner1a4d12d2010-08-13 13:07:29 +0000326 NullImporterTests,
Barry Warsaw28a691b2010-04-17 00:19:56 +0000327 ]
Hirokazu Yamamoto36144092008-09-09 07:33:27 +0000328 try:
329 import _thread
330 except ImportError:
331 pass
332 else:
333 tests.append(LockTests)
334 support.run_unittest(*tests)
Neal Norwitz996acf12003-02-17 14:51:41 +0000335
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000336if __name__ == "__main__":
Neal Norwitz996acf12003-02-17 14:51:41 +0000337 test_main()