blob: 79b3e0f1a8ba8f97383de39d80d27526ac070fca [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
Brett Cannon8a9583e2008-09-04 05:04:25 +00004import sys
Thomas Wouters89f507f2006-12-13 04:49:30 +00005import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00006from test import support
Neal Norwitz2294c0d2003-02-12 23:02:21 +00007
Neal Norwitz2294c0d2003-02-12 23:02:21 +00008
Thomas Wouters89f507f2006-12-13 04:49:30 +00009class LockTests(unittest.TestCase):
Tim Peters579bed72003-04-26 14:31:24 +000010
Thomas Wouters89f507f2006-12-13 04:49:30 +000011 """Very basic test of import lock functions."""
Tim Peters579bed72003-04-26 14:31:24 +000012
Thomas Wouters89f507f2006-12-13 04:49:30 +000013 def verify_lock_state(self, expected):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000014 self.assertEqual(imp.lock_held(), expected,
Thomas Wouters89f507f2006-12-13 04:49:30 +000015 "expected imp.lock_held() to be %r" % expected)
16 def testLock(self):
17 LOOPS = 50
Tim Peters579bed72003-04-26 14:31:24 +000018
Thomas Wouters89f507f2006-12-13 04:49:30 +000019 # The import lock may already be held, e.g. if the test suite is run
20 # via "import test.autotest".
21 lock_held_at_start = imp.lock_held()
22 self.verify_lock_state(lock_held_at_start)
Tim Peters579bed72003-04-26 14:31:24 +000023
Thomas Wouters89f507f2006-12-13 04:49:30 +000024 for i in range(LOOPS):
25 imp.acquire_lock()
26 self.verify_lock_state(True)
Tim Peters579bed72003-04-26 14:31:24 +000027
Thomas Wouters89f507f2006-12-13 04:49:30 +000028 for i in range(LOOPS):
Neal Norwitz2294c0d2003-02-12 23:02:21 +000029 imp.release_lock()
Thomas Wouters89f507f2006-12-13 04:49:30 +000030
31 # The original state should be restored now.
32 self.verify_lock_state(lock_held_at_start)
33
34 if not lock_held_at_start:
35 try:
36 imp.release_lock()
37 except RuntimeError:
38 pass
39 else:
40 self.fail("release_lock() without lock should raise "
41 "RuntimeError")
Neal Norwitz2294c0d2003-02-12 23:02:21 +000042
Guido van Rossumce3a72a2007-10-19 23:16:50 +000043class ImportTests(unittest.TestCase):
44
45 def test_find_module_encoding(self):
46 fd = imp.find_module("heapq")[0]
47 self.assertEqual(fd.encoding, "iso-8859-1")
48
Guido van Rossum40d20bc2007-10-22 00:09:51 +000049 def test_issue1267(self):
50 fp, filename, info = imp.find_module("pydoc")
51 self.assertNotEqual(fp, None)
52 self.assertEqual(fp.encoding, "iso-8859-1")
53 self.assertEqual(fp.tell(), 0)
Benjamin Peterson25bfc552010-03-11 23:59:55 +000054 self.assertEqual(fp.readline(), '#!/usr/bin/env python3\n')
Guido van Rossum40d20bc2007-10-22 00:09:51 +000055 fp.close()
56
57 fp, filename, info = imp.find_module("tokenize")
58 self.assertNotEqual(fp, None)
59 self.assertEqual(fp.encoding, "utf-8")
60 self.assertEqual(fp.tell(), 0)
61 self.assertEqual(fp.readline(),
62 '"""Tokenization help for Python programs.\n')
63 fp.close()
64
Brett Cannon8a9583e2008-09-04 05:04:25 +000065 def test_issue3594(self):
66 temp_mod_name = 'test_imp_helper'
67 sys.path.insert(0, '.')
68 try:
69 with open(temp_mod_name + '.py', 'w') as file:
70 file.write("# coding: cp1252\nu = 'test.test_imp'\n")
71 file, filename, info = imp.find_module(temp_mod_name)
72 file.close()
73 self.assertEquals(file.encoding, 'cp1252')
74 finally:
75 del sys.path[0]
76 support.unlink(temp_mod_name + '.py')
77 support.unlink(temp_mod_name + '.pyc')
78 support.unlink(temp_mod_name + '.pyo')
79
Guido van Rossum0ad59d42009-03-30 22:01:35 +000080 def test_issue5604(self):
81 # Test cannot cover imp.load_compiled function.
82 # Martin von Loewis note what shared library cannot have non-ascii
83 # character because init_xxx function cannot be compiled
84 # and issue never happens for dynamic modules.
85 # But sources modified to follow generic way for processing pathes.
86
Ezio Melotti435b5312010-03-06 01:20:49 +000087 # the return encoding could be uppercase or None
88 fs_encoding = sys.getfilesystemencoding()
Ezio Melotti06dbff32010-03-05 15:17:26 +000089 fs_encoding = fs_encoding.lower() if fs_encoding else 'ascii'
Guido van Rossum0ad59d42009-03-30 22:01:35 +000090
91 # covers utf-8 and Windows ANSI code pages
92 # one non-space symbol from every page
93 # (http://en.wikipedia.org/wiki/Code_page)
94 known_locales = {
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +000095 'utf-8' : b'\xc3\xa4',
Guido van Rossum0ad59d42009-03-30 22:01:35 +000096 'cp1250' : b'\x8C',
97 'cp1251' : b'\xc0',
98 'cp1252' : b'\xc0',
99 'cp1253' : b'\xc1',
100 'cp1254' : b'\xc0',
101 'cp1255' : b'\xe0',
102 'cp1256' : b'\xe0',
103 'cp1257' : b'\xc0',
104 'cp1258' : b'\xc0',
105 }
106
Ezio Melotti76e0d1a2010-03-05 15:08:19 +0000107 special_char = known_locales.get(fs_encoding)
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000108 if not special_char:
Ezio Melotti76e0d1a2010-03-05 15:08:19 +0000109 self.skipTest("can't run this test with %s as filesystem encoding"
110 % fs_encoding)
111 decoded_char = special_char.decode(fs_encoding)
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000112 temp_mod_name = 'test_imp_helper_' + decoded_char
113 test_package_name = 'test_imp_helper_package_' + decoded_char
114 init_file_name = os.path.join(test_package_name, '__init__.py')
115 try:
Ezio Melotti41a6b042010-03-06 01:50:25 +0000116 # if the curdir is not in sys.path the test fails when run with
117 # ./python ./Lib/test/regrtest.py test_imp
118 sys.path.insert(0, os.curdir)
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000119 with open(temp_mod_name + '.py', 'w') as file:
120 file.write('a = 1\n')
121 file, filename, info = imp.find_module(temp_mod_name)
Ezio Melotti435b5312010-03-06 01:20:49 +0000122 self.assertIsNotNone(file)
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000123 self.assertTrue(filename[:-3].endswith(temp_mod_name))
Ezio Melotti435b5312010-03-06 01:20:49 +0000124 self.assertEqual(info[0], '.py')
125 self.assertEqual(info[1], 'U')
126 self.assertEqual(info[2], imp.PY_SOURCE)
Guido van Rossum0ad59d42009-03-30 22:01:35 +0000127
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000128 mod = imp.load_module(temp_mod_name, file, filename, info)
Ezio Melotti435b5312010-03-06 01:20:49 +0000129 self.assertEqual(mod.a, 1)
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000130 file.close()
Guido van Rossum0ad59d42009-03-30 22:01:35 +0000131
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000132 mod = imp.load_source(temp_mod_name, temp_mod_name + '.py')
Ezio Melotti435b5312010-03-06 01:20:49 +0000133 self.assertEqual(mod.a, 1)
Guido van Rossum0ad59d42009-03-30 22:01:35 +0000134
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000135 mod = imp.load_compiled(temp_mod_name, temp_mod_name + '.pyc')
Ezio Melotti435b5312010-03-06 01:20:49 +0000136 self.assertEqual(mod.a, 1)
Guido van Rossum0ad59d42009-03-30 22:01:35 +0000137
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000138 if not os.path.exists(test_package_name):
139 os.mkdir(test_package_name)
140 with open(init_file_name, 'w') as file:
141 file.write('b = 2\n')
142 package = imp.load_package(test_package_name, test_package_name)
Ezio Melotti435b5312010-03-06 01:20:49 +0000143 self.assertEqual(package.b, 2)
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000144 finally:
Ezio Melotti41a6b042010-03-06 01:50:25 +0000145 del sys.path[0]
Ezio Melotti435b5312010-03-06 01:20:49 +0000146 for ext in ('.py', '.pyc', '.pyo'):
147 support.unlink(temp_mod_name + ext)
148 support.unlink(init_file_name + ext)
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000149 support.rmtree(test_package_name)
Guido van Rossum0ad59d42009-03-30 22:01:35 +0000150
151
Nick Coghlan6ead5522009-10-18 13:19:33 +0000152class ReloadTests(unittest.TestCase):
153
154 """Very basic tests to make sure that imp.reload() operates just like
155 reload()."""
156
157 def test_source(self):
158 # XXX (ncoghlan): It would be nice to use test_support.CleanImport
159 # here, but that breaks because the os module registers some
160 # handlers in copy_reg on import. Since CleanImport doesn't
161 # revert that registration, the module is left in a broken
162 # state after reversion. Reinitialising the module contents
163 # and just reverting os.environ to its previous state is an OK
164 # workaround
165 with support.EnvironmentVarGuard():
166 import os
167 imp.reload(os)
168
169 def test_extension(self):
170 with support.CleanImport('time'):
171 import time
172 imp.reload(time)
173
174 def test_builtin(self):
175 with support.CleanImport('marshal'):
176 import marshal
177 imp.reload(marshal)
Christian Heimes13a7a212008-01-07 17:13:09 +0000178
Guido van Rossum40d20bc2007-10-22 00:09:51 +0000179
Neal Norwitz996acf12003-02-17 14:51:41 +0000180def test_main():
Hirokazu Yamamoto36144092008-09-09 07:33:27 +0000181 tests = [
182 ImportTests,
Nick Coghlan6ead5522009-10-18 13:19:33 +0000183 ReloadTests,
Hirokazu Yamamoto36144092008-09-09 07:33:27 +0000184 ]
185 try:
186 import _thread
187 except ImportError:
188 pass
189 else:
190 tests.append(LockTests)
191 support.run_unittest(*tests)
Neal Norwitz996acf12003-02-17 14:51:41 +0000192
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000193if __name__ == "__main__":
Neal Norwitz996acf12003-02-17 14:51:41 +0000194 test_main()