blob: d66d40edd7f75125ca129a9230b3685eee51a026 [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):
Georg Brandlab91fde2009-08-13 08:51:18 +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)
54 self.assertEqual(fp.readline(), '#!/usr/bin/env python\n')
55 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 Melottic0ddee52010-03-06 03:09:26 +000087 # the return encoding could be uppercase or None
88 fs_encoding = sys.getfilesystemencoding()
89 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 Melottic0ddee52010-03-06 03:09:26 +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 Xiclunaf38b2b42010-03-20 20:38:20 +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 Melottic0ddee52010-03-06 03:09:26 +0000115 if not special_char:
116 self.skipTest("can't run this test with %s as filesystem encoding"
117 % fs_encoding)
118 decoded_char = special_char.decode(fs_encoding)
119 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:
123 # 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)
126 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)
129 self.assertIsNotNone(file)
130 self.assertTrue(filename[:-3].endswith(temp_mod_name))
131 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 Melottic0ddee52010-03-06 03:09:26 +0000135 mod = imp.load_module(temp_mod_name, file, filename, info)
136 self.assertEqual(mod.a, 1)
137 file.close()
Guido van Rossum0ad59d42009-03-30 22:01:35 +0000138
Ezio Melottic0ddee52010-03-06 03:09:26 +0000139 mod = imp.load_source(temp_mod_name, temp_mod_name + '.py')
140 self.assertEqual(mod.a, 1)
Guido van Rossum0ad59d42009-03-30 22:01:35 +0000141
Ezio Melottic0ddee52010-03-06 03:09:26 +0000142 mod = imp.load_compiled(temp_mod_name, temp_mod_name + '.pyc')
143 self.assertEqual(mod.a, 1)
Guido van Rossum0ad59d42009-03-30 22:01:35 +0000144
Ezio Melottic0ddee52010-03-06 03:09:26 +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)
150 self.assertEqual(package.b, 2)
151 finally:
152 del sys.path[0]
153 for ext in ('.py', '.pyc', '.pyo'):
154 support.unlink(temp_mod_name + ext)
155 support.unlink(init_file_name + ext)
156 support.rmtree(test_package_name)
Guido van Rossum0ad59d42009-03-30 22:01:35 +0000157
158
Christian Heimes13a7a212008-01-07 17:13:09 +0000159 def test_reload(self):
160 import marshal
161 imp.reload(marshal)
162 import string
163 imp.reload(string)
164 ## import sys
165 ## self.assertRaises(ImportError, reload, sys)
166
Guido van Rossum40d20bc2007-10-22 00:09:51 +0000167
Neal Norwitz996acf12003-02-17 14:51:41 +0000168def test_main():
Hirokazu Yamamoto36144092008-09-09 07:33:27 +0000169 tests = [
170 ImportTests,
171 ]
172 try:
173 import _thread
174 except ImportError:
175 pass
176 else:
177 tests.append(LockTests)
178 support.run_unittest(*tests)
Neal Norwitz996acf12003-02-17 14:51:41 +0000179
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000180if __name__ == "__main__":
Neal Norwitz996acf12003-02-17 14:51:41 +0000181 test_main()