blob: e995bf06302f3f1793532e76d905241c98fc97e0 [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
Florent Xicluna21164ce2010-03-20 20:30:53 +0000107 if sys.platform == 'darwin':
108 self.assertEqual(fs_encoding, 'utf-8')
109 # Mac OS X uses the Normal Form D decomposition
110 # http://developer.apple.com/mac/library/qa/qa2001/qa1173.html
111 special_char = b'a\xcc\x88'
112 else:
113 special_char = known_locales.get(fs_encoding)
114
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000115 if not special_char:
Ezio Melotti76e0d1a2010-03-05 15:08:19 +0000116 self.skipTest("can't run this test with %s as filesystem encoding"
117 % fs_encoding)
118 decoded_char = special_char.decode(fs_encoding)
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000119 temp_mod_name = 'test_imp_helper_' + decoded_char
120 test_package_name = 'test_imp_helper_package_' + decoded_char
121 init_file_name = os.path.join(test_package_name, '__init__.py')
122 try:
Ezio Melotti41a6b042010-03-06 01:50:25 +0000123 # if the curdir is not in sys.path the test fails when run with
124 # ./python ./Lib/test/regrtest.py test_imp
125 sys.path.insert(0, os.curdir)
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000126 with open(temp_mod_name + '.py', 'w') as file:
127 file.write('a = 1\n')
128 file, filename, info = imp.find_module(temp_mod_name)
Ezio Melotti435b5312010-03-06 01:20:49 +0000129 self.assertIsNotNone(file)
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000130 self.assertTrue(filename[:-3].endswith(temp_mod_name))
Ezio Melotti435b5312010-03-06 01:20:49 +0000131 self.assertEqual(info[0], '.py')
132 self.assertEqual(info[1], 'U')
133 self.assertEqual(info[2], imp.PY_SOURCE)
Guido van Rossum0ad59d42009-03-30 22:01:35 +0000134
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000135 mod = imp.load_module(temp_mod_name, file, filename, info)
Ezio Melotti435b5312010-03-06 01:20:49 +0000136 self.assertEqual(mod.a, 1)
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000137 file.close()
Guido van Rossum0ad59d42009-03-30 22:01:35 +0000138
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000139 mod = imp.load_source(temp_mod_name, temp_mod_name + '.py')
Ezio Melotti435b5312010-03-06 01:20:49 +0000140 self.assertEqual(mod.a, 1)
Guido van Rossum0ad59d42009-03-30 22:01:35 +0000141
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000142 mod = imp.load_compiled(temp_mod_name, temp_mod_name + '.pyc')
Ezio Melotti435b5312010-03-06 01:20:49 +0000143 self.assertEqual(mod.a, 1)
Guido van Rossum0ad59d42009-03-30 22:01:35 +0000144
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000145 if not os.path.exists(test_package_name):
146 os.mkdir(test_package_name)
147 with open(init_file_name, 'w') as file:
148 file.write('b = 2\n')
149 package = imp.load_package(test_package_name, test_package_name)
Ezio Melotti435b5312010-03-06 01:20:49 +0000150 self.assertEqual(package.b, 2)
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000151 finally:
Ezio Melotti41a6b042010-03-06 01:50:25 +0000152 del sys.path[0]
Ezio Melotti435b5312010-03-06 01:20:49 +0000153 for ext in ('.py', '.pyc', '.pyo'):
154 support.unlink(temp_mod_name + ext)
155 support.unlink(init_file_name + ext)
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000156 support.rmtree(test_package_name)
Guido van Rossum0ad59d42009-03-30 22:01:35 +0000157
158
Nick Coghlan6ead5522009-10-18 13:19:33 +0000159class ReloadTests(unittest.TestCase):
160
161 """Very basic tests to make sure that imp.reload() operates just like
162 reload()."""
163
164 def test_source(self):
Florent Xicluna97133722010-03-20 20:31:34 +0000165 # XXX (ncoghlan): It would be nice to use test.support.CleanImport
Nick Coghlan6ead5522009-10-18 13:19:33 +0000166 # here, but that breaks because the os module registers some
167 # handlers in copy_reg on import. Since CleanImport doesn't
168 # revert that registration, the module is left in a broken
169 # state after reversion. Reinitialising the module contents
170 # and just reverting os.environ to its previous state is an OK
171 # workaround
172 with support.EnvironmentVarGuard():
173 import os
174 imp.reload(os)
175
176 def test_extension(self):
177 with support.CleanImport('time'):
178 import time
179 imp.reload(time)
180
181 def test_builtin(self):
182 with support.CleanImport('marshal'):
183 import marshal
184 imp.reload(marshal)
Christian Heimes13a7a212008-01-07 17:13:09 +0000185
Guido van Rossum40d20bc2007-10-22 00:09:51 +0000186
Neal Norwitz996acf12003-02-17 14:51:41 +0000187def test_main():
Hirokazu Yamamoto36144092008-09-09 07:33:27 +0000188 tests = [
189 ImportTests,
Nick Coghlan6ead5522009-10-18 13:19:33 +0000190 ReloadTests,
Hirokazu Yamamoto36144092008-09-09 07:33:27 +0000191 ]
192 try:
193 import _thread
194 except ImportError:
195 pass
196 else:
197 tests.append(LockTests)
198 support.run_unittest(*tests)
Neal Norwitz996acf12003-02-17 14:51:41 +0000199
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000200if __name__ == "__main__":
Neal Norwitz996acf12003-02-17 14:51:41 +0000201 test_main()