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