Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 1 | import imp |
Guido van Rossum | 0ad59d4 | 2009-03-30 22:01:35 +0000 | [diff] [blame] | 2 | import os |
| 3 | import os.path |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 4 | import shutil |
Brett Cannon | 8a9583e | 2008-09-04 05:04:25 +0000 | [diff] [blame] | 5 | import sys |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 7 | from test import support |
Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 8 | |
Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 9 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 10 | class LockTests(unittest.TestCase): |
Tim Peters | 579bed7 | 2003-04-26 14:31:24 +0000 | [diff] [blame] | 11 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 12 | """Very basic test of import lock functions.""" |
Tim Peters | 579bed7 | 2003-04-26 14:31:24 +0000 | [diff] [blame] | 13 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 14 | def verify_lock_state(self, expected): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 15 | self.assertEqual(imp.lock_held(), expected, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 16 | "expected imp.lock_held() to be %r" % expected) |
| 17 | def testLock(self): |
| 18 | LOOPS = 50 |
Tim Peters | 579bed7 | 2003-04-26 14:31:24 +0000 | [diff] [blame] | 19 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 20 | # 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 Peters | 579bed7 | 2003-04-26 14:31:24 +0000 | [diff] [blame] | 24 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 25 | for i in range(LOOPS): |
| 26 | imp.acquire_lock() |
| 27 | self.verify_lock_state(True) |
Tim Peters | 579bed7 | 2003-04-26 14:31:24 +0000 | [diff] [blame] | 28 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 29 | for i in range(LOOPS): |
Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 30 | imp.release_lock() |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 31 | |
| 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 Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 43 | |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 44 | class ImportTests(unittest.TestCase): |
| 45 | |
| 46 | def test_find_module_encoding(self): |
Benjamin Peterson | 335d2c7 | 2010-07-05 14:59:40 +0000 | [diff] [blame^] | 47 | fd = imp.find_module("pydoc")[0] |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 48 | self.assertEqual(fd.encoding, "iso-8859-1") |
| 49 | |
Guido van Rossum | 40d20bc | 2007-10-22 00:09:51 +0000 | [diff] [blame] | 50 | 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 Peterson | 25bfc55 | 2010-03-11 23:59:55 +0000 | [diff] [blame] | 55 | self.assertEqual(fp.readline(), '#!/usr/bin/env python3\n') |
Guido van Rossum | 40d20bc | 2007-10-22 00:09:51 +0000 | [diff] [blame] | 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 Cannon | 8a9583e | 2008-09-04 05:04:25 +0000 | [diff] [blame] | 66 | 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 Rossum | 0ad59d4 | 2009-03-30 22:01:35 +0000 | [diff] [blame] | 81 | 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 Melotti | 435b531 | 2010-03-06 01:20:49 +0000 | [diff] [blame] | 88 | # the return encoding could be uppercase or None |
| 89 | fs_encoding = sys.getfilesystemencoding() |
Ezio Melotti | 06dbff3 | 2010-03-05 15:17:26 +0000 | [diff] [blame] | 90 | fs_encoding = fs_encoding.lower() if fs_encoding else 'ascii' |
Guido van Rossum | 0ad59d4 | 2009-03-30 22:01:35 +0000 | [diff] [blame] | 91 | |
| 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 Melotti | 9a7d5ac | 2010-03-05 12:43:17 +0000 | [diff] [blame] | 96 | 'utf-8' : b'\xc3\xa4', |
Guido van Rossum | 0ad59d4 | 2009-03-30 22:01:35 +0000 | [diff] [blame] | 97 | '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 Xicluna | 21164ce | 2010-03-20 20:30:53 +0000 | [diff] [blame] | 108 | 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 Melotti | 9a7d5ac | 2010-03-05 12:43:17 +0000 | [diff] [blame] | 116 | if not special_char: |
Ezio Melotti | 76e0d1a | 2010-03-05 15:08:19 +0000 | [diff] [blame] | 117 | self.skipTest("can't run this test with %s as filesystem encoding" |
| 118 | % fs_encoding) |
| 119 | decoded_char = special_char.decode(fs_encoding) |
Ezio Melotti | 9a7d5ac | 2010-03-05 12:43:17 +0000 | [diff] [blame] | 120 | 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 Melotti | 41a6b04 | 2010-03-06 01:50:25 +0000 | [diff] [blame] | 124 | # 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 Melotti | 9a7d5ac | 2010-03-05 12:43:17 +0000 | [diff] [blame] | 127 | 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 Melotti | 435b531 | 2010-03-06 01:20:49 +0000 | [diff] [blame] | 130 | self.assertIsNotNone(file) |
Ezio Melotti | 9a7d5ac | 2010-03-05 12:43:17 +0000 | [diff] [blame] | 131 | self.assertTrue(filename[:-3].endswith(temp_mod_name)) |
Ezio Melotti | 435b531 | 2010-03-06 01:20:49 +0000 | [diff] [blame] | 132 | self.assertEqual(info[0], '.py') |
| 133 | self.assertEqual(info[1], 'U') |
| 134 | self.assertEqual(info[2], imp.PY_SOURCE) |
Guido van Rossum | 0ad59d4 | 2009-03-30 22:01:35 +0000 | [diff] [blame] | 135 | |
Ezio Melotti | 9a7d5ac | 2010-03-05 12:43:17 +0000 | [diff] [blame] | 136 | mod = imp.load_module(temp_mod_name, file, filename, info) |
Ezio Melotti | 435b531 | 2010-03-06 01:20:49 +0000 | [diff] [blame] | 137 | self.assertEqual(mod.a, 1) |
Ezio Melotti | 9a7d5ac | 2010-03-05 12:43:17 +0000 | [diff] [blame] | 138 | file.close() |
Guido van Rossum | 0ad59d4 | 2009-03-30 22:01:35 +0000 | [diff] [blame] | 139 | |
Ezio Melotti | 9a7d5ac | 2010-03-05 12:43:17 +0000 | [diff] [blame] | 140 | mod = imp.load_source(temp_mod_name, temp_mod_name + '.py') |
Ezio Melotti | 435b531 | 2010-03-06 01:20:49 +0000 | [diff] [blame] | 141 | self.assertEqual(mod.a, 1) |
Guido van Rossum | 0ad59d4 | 2009-03-30 22:01:35 +0000 | [diff] [blame] | 142 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 143 | mod = imp.load_compiled( |
| 144 | temp_mod_name, imp.cache_from_source(temp_mod_name + '.py')) |
Ezio Melotti | 435b531 | 2010-03-06 01:20:49 +0000 | [diff] [blame] | 145 | self.assertEqual(mod.a, 1) |
Guido van Rossum | 0ad59d4 | 2009-03-30 22:01:35 +0000 | [diff] [blame] | 146 | |
Ezio Melotti | 9a7d5ac | 2010-03-05 12:43:17 +0000 | [diff] [blame] | 147 | 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 Melotti | 435b531 | 2010-03-06 01:20:49 +0000 | [diff] [blame] | 152 | self.assertEqual(package.b, 2) |
Ezio Melotti | 9a7d5ac | 2010-03-05 12:43:17 +0000 | [diff] [blame] | 153 | finally: |
Ezio Melotti | 41a6b04 | 2010-03-06 01:50:25 +0000 | [diff] [blame] | 154 | del sys.path[0] |
Ezio Melotti | 435b531 | 2010-03-06 01:20:49 +0000 | [diff] [blame] | 155 | for ext in ('.py', '.pyc', '.pyo'): |
| 156 | support.unlink(temp_mod_name + ext) |
| 157 | support.unlink(init_file_name + ext) |
Ezio Melotti | 9a7d5ac | 2010-03-05 12:43:17 +0000 | [diff] [blame] | 158 | support.rmtree(test_package_name) |
Guido van Rossum | 0ad59d4 | 2009-03-30 22:01:35 +0000 | [diff] [blame] | 159 | |
| 160 | |
Nick Coghlan | 6ead552 | 2009-10-18 13:19:33 +0000 | [diff] [blame] | 161 | class 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 Xicluna | 9713372 | 2010-03-20 20:31:34 +0000 | [diff] [blame] | 167 | # XXX (ncoghlan): It would be nice to use test.support.CleanImport |
Nick Coghlan | 6ead552 | 2009-10-18 13:19:33 +0000 | [diff] [blame] | 168 | # 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 Heimes | 13a7a21 | 2008-01-07 17:13:09 +0000 | [diff] [blame] | 187 | |
Guido van Rossum | 40d20bc | 2007-10-22 00:09:51 +0000 | [diff] [blame] | 188 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 189 | class 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 | |
Neal Norwitz | 996acf1 | 2003-02-17 14:51:41 +0000 | [diff] [blame] | 309 | def test_main(): |
Hirokazu Yamamoto | 3614409 | 2008-09-09 07:33:27 +0000 | [diff] [blame] | 310 | tests = [ |
| 311 | ImportTests, |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 312 | PEP3147Tests, |
Nick Coghlan | 6ead552 | 2009-10-18 13:19:33 +0000 | [diff] [blame] | 313 | ReloadTests, |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 314 | ] |
Hirokazu Yamamoto | 3614409 | 2008-09-09 07:33:27 +0000 | [diff] [blame] | 315 | try: |
| 316 | import _thread |
| 317 | except ImportError: |
| 318 | pass |
| 319 | else: |
| 320 | tests.append(LockTests) |
| 321 | support.run_unittest(*tests) |
Neal Norwitz | 996acf1 | 2003-02-17 14:51:41 +0000 | [diff] [blame] | 322 | |
Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 323 | if __name__ == "__main__": |
Neal Norwitz | 996acf1 | 2003-02-17 14:51:41 +0000 | [diff] [blame] | 324 | test_main() |