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