Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 1 | import imp |
Brett Cannon | c049952 | 2012-05-11 14:48:41 -0400 | [diff] [blame] | 2 | import importlib |
Guido van Rossum | 0ad59d4 | 2009-03-30 22:01:35 +0000 | [diff] [blame] | 3 | import os |
| 4 | import os.path |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 5 | import shutil |
Brett Cannon | 8a9583e | 2008-09-04 05:04:25 +0000 | [diff] [blame] | 6 | import sys |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 7 | from test import support |
Brett Cannon | c049952 | 2012-05-11 14:48:41 -0400 | [diff] [blame] | 8 | import unittest |
| 9 | import warnings |
Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 10 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 11 | class LockTests(unittest.TestCase): |
Tim Peters | 579bed7 | 2003-04-26 14:31:24 +0000 | [diff] [blame] | 12 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 13 | """Very basic test of import lock functions.""" |
Tim Peters | 579bed7 | 2003-04-26 14:31:24 +0000 | [diff] [blame] | 14 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 15 | def verify_lock_state(self, expected): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 16 | self.assertEqual(imp.lock_held(), expected, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 17 | "expected imp.lock_held() to be %r" % expected) |
| 18 | def testLock(self): |
| 19 | LOOPS = 50 |
Tim Peters | 579bed7 | 2003-04-26 14:31:24 +0000 | [diff] [blame] | 20 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 21 | # The import lock may already be held, e.g. if the test suite is run |
| 22 | # via "import test.autotest". |
| 23 | lock_held_at_start = imp.lock_held() |
| 24 | self.verify_lock_state(lock_held_at_start) |
Tim Peters | 579bed7 | 2003-04-26 14:31:24 +0000 | [diff] [blame] | 25 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 26 | for i in range(LOOPS): |
| 27 | imp.acquire_lock() |
| 28 | self.verify_lock_state(True) |
Tim Peters | 579bed7 | 2003-04-26 14:31:24 +0000 | [diff] [blame] | 29 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 30 | for i in range(LOOPS): |
Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 31 | imp.release_lock() |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 32 | |
| 33 | # The original state should be restored now. |
| 34 | self.verify_lock_state(lock_held_at_start) |
| 35 | |
| 36 | if not lock_held_at_start: |
| 37 | try: |
| 38 | imp.release_lock() |
| 39 | except RuntimeError: |
| 40 | pass |
| 41 | else: |
| 42 | self.fail("release_lock() without lock should raise " |
| 43 | "RuntimeError") |
Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 44 | |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 45 | class ImportTests(unittest.TestCase): |
Alexander Belopolsky | e8f5832 | 2010-10-15 16:28:20 +0000 | [diff] [blame] | 46 | def setUp(self): |
| 47 | mod = importlib.import_module('test.encoded_modules') |
| 48 | self.test_strings = mod.test_strings |
| 49 | self.test_path = mod.__path__ |
| 50 | |
| 51 | def test_import_encoded_module(self): |
| 52 | for modname, encoding, teststr in self.test_strings: |
| 53 | mod = importlib.import_module('test.encoded_modules.' |
| 54 | 'module_' + modname) |
| 55 | self.assertEqual(teststr, mod.test) |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 56 | |
| 57 | def test_find_module_encoding(self): |
Alexander Belopolsky | e8f5832 | 2010-10-15 16:28:20 +0000 | [diff] [blame] | 58 | for mod, encoding, _ in self.test_strings: |
Brett Cannon | 749afa9 | 2010-10-29 23:47:23 +0000 | [diff] [blame] | 59 | with imp.find_module('module_' + mod, self.test_path)[0] as fd: |
| 60 | self.assertEqual(fd.encoding, encoding) |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 61 | |
Victor Stinner | fe7c5b5 | 2011-04-05 01:48:03 +0200 | [diff] [blame] | 62 | path = [os.path.dirname(__file__)] |
Brett Cannon | dd9a569 | 2012-04-20 12:59:59 -0400 | [diff] [blame] | 63 | with self.assertRaises(SyntaxError): |
| 64 | imp.find_module('badsyntax_pep3120', path) |
Victor Stinner | fe7c5b5 | 2011-04-05 01:48:03 +0200 | [diff] [blame] | 65 | |
Guido van Rossum | 40d20bc | 2007-10-22 00:09:51 +0000 | [diff] [blame] | 66 | def test_issue1267(self): |
Alexander Belopolsky | e8f5832 | 2010-10-15 16:28:20 +0000 | [diff] [blame] | 67 | for mod, encoding, _ in self.test_strings: |
| 68 | fp, filename, info = imp.find_module('module_' + mod, |
| 69 | self.test_path) |
Brett Cannon | 749afa9 | 2010-10-29 23:47:23 +0000 | [diff] [blame] | 70 | with fp: |
| 71 | self.assertNotEqual(fp, None) |
| 72 | self.assertEqual(fp.encoding, encoding) |
| 73 | self.assertEqual(fp.tell(), 0) |
| 74 | self.assertEqual(fp.readline(), '# test %s encoding\n' |
| 75 | % encoding) |
Guido van Rossum | 40d20bc | 2007-10-22 00:09:51 +0000 | [diff] [blame] | 76 | |
| 77 | fp, filename, info = imp.find_module("tokenize") |
Brett Cannon | 749afa9 | 2010-10-29 23:47:23 +0000 | [diff] [blame] | 78 | with fp: |
| 79 | self.assertNotEqual(fp, None) |
| 80 | self.assertEqual(fp.encoding, "utf-8") |
| 81 | self.assertEqual(fp.tell(), 0) |
| 82 | self.assertEqual(fp.readline(), |
| 83 | '"""Tokenization help for Python programs.\n') |
Guido van Rossum | 40d20bc | 2007-10-22 00:09:51 +0000 | [diff] [blame] | 84 | |
Brett Cannon | 8a9583e | 2008-09-04 05:04:25 +0000 | [diff] [blame] | 85 | def test_issue3594(self): |
| 86 | temp_mod_name = 'test_imp_helper' |
| 87 | sys.path.insert(0, '.') |
| 88 | try: |
| 89 | with open(temp_mod_name + '.py', 'w') as file: |
| 90 | file.write("# coding: cp1252\nu = 'test.test_imp'\n") |
| 91 | file, filename, info = imp.find_module(temp_mod_name) |
| 92 | file.close() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 93 | self.assertEqual(file.encoding, 'cp1252') |
Brett Cannon | 8a9583e | 2008-09-04 05:04:25 +0000 | [diff] [blame] | 94 | finally: |
| 95 | del sys.path[0] |
| 96 | support.unlink(temp_mod_name + '.py') |
| 97 | support.unlink(temp_mod_name + '.pyc') |
| 98 | support.unlink(temp_mod_name + '.pyo') |
| 99 | |
Guido van Rossum | 0ad59d4 | 2009-03-30 22:01:35 +0000 | [diff] [blame] | 100 | def test_issue5604(self): |
| 101 | # Test cannot cover imp.load_compiled function. |
| 102 | # Martin von Loewis note what shared library cannot have non-ascii |
| 103 | # character because init_xxx function cannot be compiled |
| 104 | # and issue never happens for dynamic modules. |
| 105 | # But sources modified to follow generic way for processing pathes. |
| 106 | |
Ezio Melotti | 435b531 | 2010-03-06 01:20:49 +0000 | [diff] [blame] | 107 | # the return encoding could be uppercase or None |
| 108 | fs_encoding = sys.getfilesystemencoding() |
Guido van Rossum | 0ad59d4 | 2009-03-30 22:01:35 +0000 | [diff] [blame] | 109 | |
| 110 | # covers utf-8 and Windows ANSI code pages |
| 111 | # one non-space symbol from every page |
| 112 | # (http://en.wikipedia.org/wiki/Code_page) |
| 113 | known_locales = { |
Ezio Melotti | 9a7d5ac | 2010-03-05 12:43:17 +0000 | [diff] [blame] | 114 | 'utf-8' : b'\xc3\xa4', |
Guido van Rossum | 0ad59d4 | 2009-03-30 22:01:35 +0000 | [diff] [blame] | 115 | 'cp1250' : b'\x8C', |
| 116 | 'cp1251' : b'\xc0', |
| 117 | 'cp1252' : b'\xc0', |
| 118 | 'cp1253' : b'\xc1', |
| 119 | 'cp1254' : b'\xc0', |
| 120 | 'cp1255' : b'\xe0', |
| 121 | 'cp1256' : b'\xe0', |
| 122 | 'cp1257' : b'\xc0', |
| 123 | 'cp1258' : b'\xc0', |
| 124 | } |
| 125 | |
Florent Xicluna | 21164ce | 2010-03-20 20:30:53 +0000 | [diff] [blame] | 126 | if sys.platform == 'darwin': |
| 127 | self.assertEqual(fs_encoding, 'utf-8') |
| 128 | # Mac OS X uses the Normal Form D decomposition |
| 129 | # http://developer.apple.com/mac/library/qa/qa2001/qa1173.html |
| 130 | special_char = b'a\xcc\x88' |
| 131 | else: |
| 132 | special_char = known_locales.get(fs_encoding) |
| 133 | |
Ezio Melotti | 9a7d5ac | 2010-03-05 12:43:17 +0000 | [diff] [blame] | 134 | if not special_char: |
Ezio Melotti | 76e0d1a | 2010-03-05 15:08:19 +0000 | [diff] [blame] | 135 | self.skipTest("can't run this test with %s as filesystem encoding" |
| 136 | % fs_encoding) |
| 137 | decoded_char = special_char.decode(fs_encoding) |
Ezio Melotti | 9a7d5ac | 2010-03-05 12:43:17 +0000 | [diff] [blame] | 138 | temp_mod_name = 'test_imp_helper_' + decoded_char |
| 139 | test_package_name = 'test_imp_helper_package_' + decoded_char |
| 140 | init_file_name = os.path.join(test_package_name, '__init__.py') |
| 141 | try: |
Ezio Melotti | 41a6b04 | 2010-03-06 01:50:25 +0000 | [diff] [blame] | 142 | # if the curdir is not in sys.path the test fails when run with |
| 143 | # ./python ./Lib/test/regrtest.py test_imp |
| 144 | sys.path.insert(0, os.curdir) |
Ezio Melotti | 9a7d5ac | 2010-03-05 12:43:17 +0000 | [diff] [blame] | 145 | with open(temp_mod_name + '.py', 'w') as file: |
| 146 | file.write('a = 1\n') |
| 147 | file, filename, info = imp.find_module(temp_mod_name) |
Brett Cannon | 749afa9 | 2010-10-29 23:47:23 +0000 | [diff] [blame] | 148 | with file: |
| 149 | self.assertIsNotNone(file) |
| 150 | self.assertTrue(filename[:-3].endswith(temp_mod_name)) |
| 151 | self.assertEqual(info[0], '.py') |
| 152 | self.assertEqual(info[1], 'U') |
| 153 | self.assertEqual(info[2], imp.PY_SOURCE) |
Guido van Rossum | 0ad59d4 | 2009-03-30 22:01:35 +0000 | [diff] [blame] | 154 | |
Brett Cannon | 749afa9 | 2010-10-29 23:47:23 +0000 | [diff] [blame] | 155 | mod = imp.load_module(temp_mod_name, file, filename, info) |
| 156 | self.assertEqual(mod.a, 1) |
Guido van Rossum | 0ad59d4 | 2009-03-30 22:01:35 +0000 | [diff] [blame] | 157 | |
Brett Cannon | c049952 | 2012-05-11 14:48:41 -0400 | [diff] [blame] | 158 | with warnings.catch_warnings(): |
| 159 | warnings.simplefilter('ignore') |
| 160 | mod = imp.load_source(temp_mod_name, temp_mod_name + '.py') |
Ezio Melotti | 435b531 | 2010-03-06 01:20:49 +0000 | [diff] [blame] | 161 | self.assertEqual(mod.a, 1) |
Guido van Rossum | 0ad59d4 | 2009-03-30 22:01:35 +0000 | [diff] [blame] | 162 | |
Brett Cannon | c049952 | 2012-05-11 14:48:41 -0400 | [diff] [blame] | 163 | with warnings.catch_warnings(): |
| 164 | warnings.simplefilter('ignore') |
| 165 | mod = imp.load_compiled( |
| 166 | temp_mod_name, imp.cache_from_source(temp_mod_name + '.py')) |
Ezio Melotti | 435b531 | 2010-03-06 01:20:49 +0000 | [diff] [blame] | 167 | self.assertEqual(mod.a, 1) |
Guido van Rossum | 0ad59d4 | 2009-03-30 22:01:35 +0000 | [diff] [blame] | 168 | |
Ezio Melotti | 9a7d5ac | 2010-03-05 12:43:17 +0000 | [diff] [blame] | 169 | if not os.path.exists(test_package_name): |
| 170 | os.mkdir(test_package_name) |
| 171 | with open(init_file_name, 'w') as file: |
| 172 | file.write('b = 2\n') |
Brett Cannon | c049952 | 2012-05-11 14:48:41 -0400 | [diff] [blame] | 173 | with warnings.catch_warnings(): |
| 174 | warnings.simplefilter('ignore') |
| 175 | package = imp.load_package(test_package_name, test_package_name) |
Ezio Melotti | 435b531 | 2010-03-06 01:20:49 +0000 | [diff] [blame] | 176 | self.assertEqual(package.b, 2) |
Ezio Melotti | 9a7d5ac | 2010-03-05 12:43:17 +0000 | [diff] [blame] | 177 | finally: |
Ezio Melotti | 41a6b04 | 2010-03-06 01:50:25 +0000 | [diff] [blame] | 178 | del sys.path[0] |
Ezio Melotti | 435b531 | 2010-03-06 01:20:49 +0000 | [diff] [blame] | 179 | for ext in ('.py', '.pyc', '.pyo'): |
| 180 | support.unlink(temp_mod_name + ext) |
| 181 | support.unlink(init_file_name + ext) |
Ezio Melotti | 9a7d5ac | 2010-03-05 12:43:17 +0000 | [diff] [blame] | 182 | support.rmtree(test_package_name) |
Guido van Rossum | 0ad59d4 | 2009-03-30 22:01:35 +0000 | [diff] [blame] | 183 | |
Victor Stinner | c68b6aa | 2011-04-23 00:41:19 +0200 | [diff] [blame] | 184 | def test_issue9319(self): |
Antoine Pitrou | 1184690 | 2011-04-25 21:39:49 +0200 | [diff] [blame] | 185 | path = os.path.dirname(__file__) |
Victor Stinner | 7fdd0fe | 2011-04-23 01:24:11 +0200 | [diff] [blame] | 186 | self.assertRaises(SyntaxError, |
Antoine Pitrou | 1184690 | 2011-04-25 21:39:49 +0200 | [diff] [blame] | 187 | imp.find_module, "badsyntax_pep3120", [path]) |
Victor Stinner | c68b6aa | 2011-04-23 00:41:19 +0200 | [diff] [blame] | 188 | |
Nick Coghlan | 91b9f13 | 2012-09-01 00:13:45 +1000 | [diff] [blame] | 189 | def test_load_from_source(self): |
| 190 | # Verify that the imp module can correctly load and find .py files |
| 191 | # XXX (ncoghlan): It would be nice to use support.CleanImport |
| 192 | # here, but that breaks because the os module registers some |
| 193 | # handlers in copy_reg on import. Since CleanImport doesn't |
| 194 | # revert that registration, the module is left in a broken |
| 195 | # state after reversion. Reinitialising the module contents |
| 196 | # and just reverting os.environ to its previous state is an OK |
| 197 | # workaround |
| 198 | orig_path = os.path |
| 199 | orig_getenv = os.getenv |
| 200 | with support.EnvironmentVarGuard(): |
| 201 | x = imp.find_module("os") |
| 202 | self.addCleanup(x[0].close) |
| 203 | new_os = imp.load_module("os", *x) |
| 204 | self.assertIs(os, new_os) |
| 205 | self.assertIs(orig_path, new_os.path) |
| 206 | self.assertIsNot(orig_getenv, new_os.getenv) |
| 207 | |
| 208 | @support.cpython_only |
| 209 | def test_issue15828_load_extensions(self): |
| 210 | # Issue 15828 picked up that the adapter between the old imp API |
| 211 | # and importlib couldn't handle C extensions |
| 212 | example = "_heapq" |
| 213 | x = imp.find_module(example) |
Brett Cannon | 848cdfd | 2012-08-31 11:31:20 -0400 | [diff] [blame] | 214 | file_ = x[0] |
| 215 | if file_ is not None: |
| 216 | self.addCleanup(file_.close) |
Nick Coghlan | 91b9f13 | 2012-09-01 00:13:45 +1000 | [diff] [blame] | 217 | mod = imp.load_module(example, *x) |
| 218 | self.assertEqual(mod.__name__, example) |
| 219 | |
Brett Cannon | f0434e6 | 2012-04-20 15:22:50 -0400 | [diff] [blame] | 220 | def test_load_dynamic_ImportError_path(self): |
| 221 | # Issue #1559549 added `name` and `path` attributes to ImportError |
| 222 | # in order to provide better detail. Issue #10854 implemented those |
| 223 | # attributes on import failures of extensions on Windows. |
| 224 | path = 'bogus file path' |
| 225 | name = 'extension' |
| 226 | with self.assertRaises(ImportError) as err: |
| 227 | imp.load_dynamic(name, path) |
| 228 | self.assertIn(path, err.exception.path) |
| 229 | self.assertEqual(name, err.exception.name) |
| 230 | |
Guido van Rossum | 0ad59d4 | 2009-03-30 22:01:35 +0000 | [diff] [blame] | 231 | |
Nick Coghlan | 6ead552 | 2009-10-18 13:19:33 +0000 | [diff] [blame] | 232 | class ReloadTests(unittest.TestCase): |
| 233 | |
| 234 | """Very basic tests to make sure that imp.reload() operates just like |
| 235 | reload().""" |
| 236 | |
| 237 | def test_source(self): |
Florent Xicluna | 9713372 | 2010-03-20 20:31:34 +0000 | [diff] [blame] | 238 | # XXX (ncoghlan): It would be nice to use test.support.CleanImport |
Nick Coghlan | 6ead552 | 2009-10-18 13:19:33 +0000 | [diff] [blame] | 239 | # here, but that breaks because the os module registers some |
| 240 | # handlers in copy_reg on import. Since CleanImport doesn't |
| 241 | # revert that registration, the module is left in a broken |
| 242 | # state after reversion. Reinitialising the module contents |
| 243 | # and just reverting os.environ to its previous state is an OK |
| 244 | # workaround |
| 245 | with support.EnvironmentVarGuard(): |
| 246 | import os |
| 247 | imp.reload(os) |
| 248 | |
| 249 | def test_extension(self): |
| 250 | with support.CleanImport('time'): |
| 251 | import time |
| 252 | imp.reload(time) |
| 253 | |
| 254 | def test_builtin(self): |
| 255 | with support.CleanImport('marshal'): |
| 256 | import marshal |
| 257 | imp.reload(marshal) |
Christian Heimes | 13a7a21 | 2008-01-07 17:13:09 +0000 | [diff] [blame] | 258 | |
Guido van Rossum | 40d20bc | 2007-10-22 00:09:51 +0000 | [diff] [blame] | 259 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 260 | class PEP3147Tests(unittest.TestCase): |
| 261 | """Tests of PEP 3147.""" |
| 262 | |
| 263 | tag = imp.get_tag() |
| 264 | |
Brett Cannon | 19a2f59 | 2012-07-09 13:58:07 -0400 | [diff] [blame] | 265 | @unittest.skipUnless(sys.implementation.cache_tag is not None, |
| 266 | 'requires sys.implementation.cache_tag not be None') |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 267 | def test_cache_from_source(self): |
| 268 | # Given the path to a .py file, return the path to its PEP 3147 |
| 269 | # defined .pyc file (i.e. under __pycache__). |
Brett Cannon | 410e88d | 2012-04-22 13:29:47 -0400 | [diff] [blame] | 270 | path = os.path.join('foo', 'bar', 'baz', 'qux.py') |
| 271 | expect = os.path.join('foo', 'bar', 'baz', '__pycache__', |
| 272 | 'qux.{}.pyc'.format(self.tag)) |
| 273 | self.assertEqual(imp.cache_from_source(path, True), expect) |
| 274 | |
Brett Cannon | 19a2f59 | 2012-07-09 13:58:07 -0400 | [diff] [blame] | 275 | def test_cache_from_source_no_cache_tag(self): |
| 276 | # Non cache tag means NotImplementedError. |
| 277 | with support.swap_attr(sys.implementation, 'cache_tag', None): |
| 278 | with self.assertRaises(NotImplementedError): |
| 279 | imp.cache_from_source('whatever.py') |
| 280 | |
Brett Cannon | 410e88d | 2012-04-22 13:29:47 -0400 | [diff] [blame] | 281 | def test_cache_from_source_no_dot(self): |
| 282 | # Directory with a dot, filename without dot. |
| 283 | path = os.path.join('foo.bar', 'file') |
| 284 | expect = os.path.join('foo.bar', '__pycache__', |
| 285 | 'file{}.pyc'.format(self.tag)) |
| 286 | self.assertEqual(imp.cache_from_source(path, True), expect) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 287 | |
| 288 | def test_cache_from_source_optimized(self): |
| 289 | # Given the path to a .py file, return the path to its PEP 3147 |
| 290 | # defined .pyo file (i.e. under __pycache__). |
Brett Cannon | 410e88d | 2012-04-22 13:29:47 -0400 | [diff] [blame] | 291 | path = os.path.join('foo', 'bar', 'baz', 'qux.py') |
| 292 | expect = os.path.join('foo', 'bar', 'baz', '__pycache__', |
| 293 | 'qux.{}.pyo'.format(self.tag)) |
| 294 | self.assertEqual(imp.cache_from_source(path, False), expect) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 295 | |
| 296 | def test_cache_from_source_cwd(self): |
Brett Cannon | 410e88d | 2012-04-22 13:29:47 -0400 | [diff] [blame] | 297 | path = 'foo.py' |
| 298 | expect = os.path.join('__pycache__', 'foo.{}.pyc'.format(self.tag)) |
| 299 | self.assertEqual(imp.cache_from_source(path, True), expect) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 300 | |
| 301 | def test_cache_from_source_override(self): |
| 302 | # When debug_override is not None, it can be any true-ish or false-ish |
| 303 | # value. |
Brett Cannon | 410e88d | 2012-04-22 13:29:47 -0400 | [diff] [blame] | 304 | path = os.path.join('foo', 'bar', 'baz.py') |
| 305 | partial_expect = os.path.join('foo', 'bar', '__pycache__', |
| 306 | 'baz.{}.py'.format(self.tag)) |
| 307 | self.assertEqual(imp.cache_from_source(path, []), partial_expect + 'o') |
| 308 | self.assertEqual(imp.cache_from_source(path, [17]), |
| 309 | partial_expect + 'c') |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 310 | # However if the bool-ishness can't be determined, the exception |
| 311 | # propagates. |
| 312 | class Bearish: |
| 313 | def __bool__(self): raise RuntimeError |
Brett Cannon | 410e88d | 2012-04-22 13:29:47 -0400 | [diff] [blame] | 314 | with self.assertRaises(RuntimeError): |
| 315 | imp.cache_from_source('/foo/bar/baz.py', Bearish()) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 316 | |
Brett Cannon | 410e88d | 2012-04-22 13:29:47 -0400 | [diff] [blame] | 317 | @unittest.skipUnless(os.sep == '\\' and os.altsep == '/', |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 318 | 'test meaningful only where os.altsep is defined') |
| 319 | def test_sep_altsep_and_sep_cache_from_source(self): |
| 320 | # Windows path and PEP 3147 where sep is right of altsep. |
| 321 | self.assertEqual( |
| 322 | imp.cache_from_source('\\foo\\bar\\baz/qux.py', True), |
Brett Cannon | 410e88d | 2012-04-22 13:29:47 -0400 | [diff] [blame] | 323 | '\\foo\\bar\\baz\\__pycache__\\qux.{}.pyc'.format(self.tag)) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 324 | |
Brett Cannon | 19a2f59 | 2012-07-09 13:58:07 -0400 | [diff] [blame] | 325 | @unittest.skipUnless(sys.implementation.cache_tag is not None, |
| 326 | 'requires sys.implementation.cache_tag to not be ' |
| 327 | 'None') |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 328 | def test_source_from_cache(self): |
| 329 | # Given the path to a PEP 3147 defined .pyc file, return the path to |
| 330 | # its source. This tests the good path. |
Brett Cannon | 410e88d | 2012-04-22 13:29:47 -0400 | [diff] [blame] | 331 | path = os.path.join('foo', 'bar', 'baz', '__pycache__', |
| 332 | 'qux.{}.pyc'.format(self.tag)) |
| 333 | expect = os.path.join('foo', 'bar', 'baz', 'qux.py') |
| 334 | self.assertEqual(imp.source_from_cache(path), expect) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 335 | |
Brett Cannon | 19a2f59 | 2012-07-09 13:58:07 -0400 | [diff] [blame] | 336 | def test_source_from_cache_no_cache_tag(self): |
| 337 | # If sys.implementation.cache_tag is None, raise NotImplementedError. |
| 338 | path = os.path.join('blah', '__pycache__', 'whatever.pyc') |
| 339 | with support.swap_attr(sys.implementation, 'cache_tag', None): |
| 340 | with self.assertRaises(NotImplementedError): |
| 341 | imp.source_from_cache(path) |
| 342 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 343 | def test_source_from_cache_bad_path(self): |
| 344 | # When the path to a pyc file is not in PEP 3147 format, a ValueError |
| 345 | # is raised. |
| 346 | self.assertRaises( |
| 347 | ValueError, imp.source_from_cache, '/foo/bar/bazqux.pyc') |
| 348 | |
| 349 | def test_source_from_cache_no_slash(self): |
| 350 | # No slashes at all in path -> ValueError |
| 351 | self.assertRaises( |
| 352 | ValueError, imp.source_from_cache, 'foo.cpython-32.pyc') |
| 353 | |
| 354 | def test_source_from_cache_too_few_dots(self): |
| 355 | # Too few dots in final path component -> ValueError |
| 356 | self.assertRaises( |
| 357 | ValueError, imp.source_from_cache, '__pycache__/foo.pyc') |
| 358 | |
| 359 | def test_source_from_cache_too_many_dots(self): |
| 360 | # Too many dots in final path component -> ValueError |
| 361 | self.assertRaises( |
| 362 | ValueError, imp.source_from_cache, |
| 363 | '__pycache__/foo.cpython-32.foo.pyc') |
| 364 | |
| 365 | def test_source_from_cache_no__pycache__(self): |
| 366 | # Another problem with the path -> ValueError |
| 367 | self.assertRaises( |
| 368 | ValueError, imp.source_from_cache, |
| 369 | '/foo/bar/foo.cpython-32.foo.pyc') |
| 370 | |
| 371 | def test_package___file__(self): |
Antoine Pitrou | 06e3758 | 2012-06-23 17:27:56 +0200 | [diff] [blame] | 372 | try: |
| 373 | m = __import__('pep3147') |
| 374 | except ImportError: |
| 375 | pass |
| 376 | else: |
| 377 | self.fail("pep3147 module already exists: %r" % (m,)) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 378 | # Test that a package's __file__ points to the right source directory. |
| 379 | os.mkdir('pep3147') |
| 380 | sys.path.insert(0, os.curdir) |
| 381 | def cleanup(): |
| 382 | if sys.path[0] == os.curdir: |
| 383 | del sys.path[0] |
| 384 | shutil.rmtree('pep3147') |
| 385 | self.addCleanup(cleanup) |
| 386 | # Touch the __init__.py file. |
Victor Stinner | bf81622 | 2011-06-30 23:25:47 +0200 | [diff] [blame] | 387 | support.create_empty_file('pep3147/__init__.py') |
Antoine Pitrou | 4f92a68 | 2012-02-26 18:09:50 +0100 | [diff] [blame] | 388 | importlib.invalidate_caches() |
Antoine Pitrou | abe72d7 | 2012-02-22 01:11:31 +0100 | [diff] [blame] | 389 | expected___file__ = os.sep.join(('.', 'pep3147', '__init__.py')) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 390 | m = __import__('pep3147') |
Antoine Pitrou | 9a4d7dd | 2012-02-27 22:01:25 +0100 | [diff] [blame] | 391 | self.assertEqual(m.__file__, expected___file__, (m.__file__, m.__path__, sys.path, sys.path_importer_cache)) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 392 | # Ensure we load the pyc file. |
Antoine Pitrou | 037615e | 2012-02-22 02:30:09 +0100 | [diff] [blame] | 393 | support.unload('pep3147') |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 394 | m = __import__('pep3147') |
Antoine Pitrou | 037615e | 2012-02-22 02:30:09 +0100 | [diff] [blame] | 395 | support.unload('pep3147') |
Antoine Pitrou | 9a4d7dd | 2012-02-27 22:01:25 +0100 | [diff] [blame] | 396 | self.assertEqual(m.__file__, expected___file__, (m.__file__, m.__path__, sys.path, sys.path_importer_cache)) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 397 | |
| 398 | |
Victor Stinner | 1a4d12d | 2010-08-13 13:07:29 +0000 | [diff] [blame] | 399 | class NullImporterTests(unittest.TestCase): |
Victor Stinner | 09c449c | 2010-08-13 22:23:24 +0000 | [diff] [blame] | 400 | @unittest.skipIf(support.TESTFN_UNENCODABLE is None, |
Victor Stinner | 1a4d12d | 2010-08-13 13:07:29 +0000 | [diff] [blame] | 401 | "Need an undecodeable filename") |
| 402 | def test_unencodeable(self): |
Victor Stinner | 09c449c | 2010-08-13 22:23:24 +0000 | [diff] [blame] | 403 | name = support.TESTFN_UNENCODABLE |
Victor Stinner | 1a4d12d | 2010-08-13 13:07:29 +0000 | [diff] [blame] | 404 | os.mkdir(name) |
| 405 | try: |
| 406 | self.assertRaises(ImportError, imp.NullImporter, name) |
| 407 | finally: |
| 408 | os.rmdir(name) |
| 409 | |
| 410 | |
Neal Norwitz | 996acf1 | 2003-02-17 14:51:41 +0000 | [diff] [blame] | 411 | def test_main(): |
Hirokazu Yamamoto | 3614409 | 2008-09-09 07:33:27 +0000 | [diff] [blame] | 412 | tests = [ |
| 413 | ImportTests, |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 414 | PEP3147Tests, |
Nick Coghlan | 6ead552 | 2009-10-18 13:19:33 +0000 | [diff] [blame] | 415 | ReloadTests, |
Victor Stinner | 1a4d12d | 2010-08-13 13:07:29 +0000 | [diff] [blame] | 416 | NullImporterTests, |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 417 | ] |
Hirokazu Yamamoto | 3614409 | 2008-09-09 07:33:27 +0000 | [diff] [blame] | 418 | try: |
| 419 | import _thread |
| 420 | except ImportError: |
| 421 | pass |
| 422 | else: |
| 423 | tests.append(LockTests) |
| 424 | support.run_unittest(*tests) |
Neal Norwitz | 996acf1 | 2003-02-17 14:51:41 +0000 | [diff] [blame] | 425 | |
Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 426 | if __name__ == "__main__": |
Neal Norwitz | 996acf1 | 2003-02-17 14:51:41 +0000 | [diff] [blame] | 427 | test_main() |