blob: 49cb4cd25ed007c221301070e06075d100999b0d [file] [log] [blame]
Neal Norwitz2294c0d2003-02-12 23:02:21 +00001import imp
Guido van Rossum0ad59d42009-03-30 22:01:35 +00002import locale
3import os
4import os.path
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):
47 fd = imp.find_module("heapq")[0]
48 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)
55 self.assertEqual(fp.readline(), '#!/usr/bin/env python\n')
56 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 Melotti9a7d5ac2010-03-05 12:43:17 +000088 # the return encoding can be uppercase
89 locale_encoding = locale.getpreferredencoding().lower()
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
107 special_char = known_locales.get(locale_encoding)
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000108 if not special_char:
109 self.skipTest("can't run this test with %s as preferred encoding"
110 % locale_encoding)
111 decoded_char = special_char.decode(locale_encoding)
112 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:
116 with open(temp_mod_name + '.py', 'w') as file:
117 file.write('a = 1\n')
118 file, filename, info = imp.find_module(temp_mod_name)
119 self.assertNotEquals(None, file)
120 self.assertTrue(filename[:-3].endswith(temp_mod_name))
121 self.assertEquals('.py', info[0])
122 self.assertEquals('U', info[1])
123 self.assertEquals(imp.PY_SOURCE, info[2])
Guido van Rossum0ad59d42009-03-30 22:01:35 +0000124
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000125 mod = imp.load_module(temp_mod_name, file, filename, info)
126 self.assertEquals(1, mod.a)
127 file.close()
Guido van Rossum0ad59d42009-03-30 22:01:35 +0000128
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000129 mod = imp.load_source(temp_mod_name, temp_mod_name + '.py')
130 self.assertEquals(1, mod.a)
Guido van Rossum0ad59d42009-03-30 22:01:35 +0000131
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000132 mod = imp.load_compiled(temp_mod_name, temp_mod_name + '.pyc')
133 self.assertEquals(1, mod.a)
Guido van Rossum0ad59d42009-03-30 22:01:35 +0000134
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000135 if not os.path.exists(test_package_name):
136 os.mkdir(test_package_name)
137 with open(init_file_name, 'w') as file:
138 file.write('b = 2\n')
139 package = imp.load_package(test_package_name, test_package_name)
140 self.assertEquals(2, package.b)
141 finally:
142 support.unlink(temp_mod_name + '.py')
143 support.unlink(temp_mod_name + '.pyc')
144 support.unlink(temp_mod_name + '.pyo')
Guido van Rossum0ad59d42009-03-30 22:01:35 +0000145
Ezio Melotti9a7d5ac2010-03-05 12:43:17 +0000146 support.unlink(init_file_name + '.py')
147 support.unlink(init_file_name + '.pyc')
148 support.unlink(init_file_name + '.pyo')
149 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()