| 1 | n/a | # We import importlib *ASAP* in order to test #15386 |
|---|
| 2 | n/a | import importlib |
|---|
| 3 | n/a | import importlib.util |
|---|
| 4 | n/a | from importlib._bootstrap import _get_sourcefile |
|---|
| 5 | n/a | import builtins |
|---|
| 6 | n/a | from test.test_importlib.import_ import util as importlib_util |
|---|
| 7 | n/a | import marshal |
|---|
| 8 | n/a | import os |
|---|
| 9 | n/a | import platform |
|---|
| 10 | n/a | import py_compile |
|---|
| 11 | n/a | import random |
|---|
| 12 | n/a | import stat |
|---|
| 13 | n/a | import sys |
|---|
| 14 | n/a | import unittest |
|---|
| 15 | n/a | import unittest.mock as mock |
|---|
| 16 | n/a | import textwrap |
|---|
| 17 | n/a | import errno |
|---|
| 18 | n/a | import shutil |
|---|
| 19 | n/a | import contextlib |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | import test.support |
|---|
| 22 | n/a | from test.support import ( |
|---|
| 23 | n/a | EnvironmentVarGuard, TESTFN, check_warnings, forget, is_jython, |
|---|
| 24 | n/a | make_legacy_pyc, rmtree, run_unittest, swap_attr, swap_item, temp_umask, |
|---|
| 25 | n/a | unlink, unload, create_empty_file, cpython_only) |
|---|
| 26 | n/a | from test import script_helper |
|---|
| 27 | n/a | |
|---|
| 28 | n/a | |
|---|
| 29 | n/a | skip_if_dont_write_bytecode = unittest.skipIf( |
|---|
| 30 | n/a | sys.dont_write_bytecode, |
|---|
| 31 | n/a | "test meaningful only when writing bytecode") |
|---|
| 32 | n/a | |
|---|
| 33 | n/a | def remove_files(name): |
|---|
| 34 | n/a | for f in (name + ".py", |
|---|
| 35 | n/a | name + ".pyc", |
|---|
| 36 | n/a | name + ".pyo", |
|---|
| 37 | n/a | name + ".pyw", |
|---|
| 38 | n/a | name + "$py.class"): |
|---|
| 39 | n/a | unlink(f) |
|---|
| 40 | n/a | rmtree('__pycache__') |
|---|
| 41 | n/a | |
|---|
| 42 | n/a | |
|---|
| 43 | n/a | @contextlib.contextmanager |
|---|
| 44 | n/a | def _ready_to_import(name=None, source=""): |
|---|
| 45 | n/a | # sets up a temporary directory and removes it |
|---|
| 46 | n/a | # creates the module file |
|---|
| 47 | n/a | # temporarily clears the module from sys.modules (if any) |
|---|
| 48 | n/a | # reverts or removes the module when cleaning up |
|---|
| 49 | n/a | name = name or "spam" |
|---|
| 50 | n/a | with script_helper.temp_dir() as tempdir: |
|---|
| 51 | n/a | path = script_helper.make_script(tempdir, name, source) |
|---|
| 52 | n/a | old_module = sys.modules.pop(name, None) |
|---|
| 53 | n/a | try: |
|---|
| 54 | n/a | sys.path.insert(0, tempdir) |
|---|
| 55 | n/a | yield name, path |
|---|
| 56 | n/a | sys.path.remove(tempdir) |
|---|
| 57 | n/a | finally: |
|---|
| 58 | n/a | if old_module is not None: |
|---|
| 59 | n/a | sys.modules[name] = old_module |
|---|
| 60 | n/a | elif name in sys.modules: |
|---|
| 61 | n/a | del sys.modules[name] |
|---|
| 62 | n/a | |
|---|
| 63 | n/a | |
|---|
| 64 | n/a | class ImportTests(unittest.TestCase): |
|---|
| 65 | n/a | |
|---|
| 66 | n/a | def setUp(self): |
|---|
| 67 | n/a | remove_files(TESTFN) |
|---|
| 68 | n/a | importlib.invalidate_caches() |
|---|
| 69 | n/a | |
|---|
| 70 | n/a | def tearDown(self): |
|---|
| 71 | n/a | unload(TESTFN) |
|---|
| 72 | n/a | |
|---|
| 73 | n/a | def test_case_sensitivity(self): |
|---|
| 74 | n/a | # Brief digression to test that import is case-sensitive: if we got |
|---|
| 75 | n/a | # this far, we know for sure that "random" exists. |
|---|
| 76 | n/a | with self.assertRaises(ImportError): |
|---|
| 77 | n/a | import RAnDoM |
|---|
| 78 | n/a | |
|---|
| 79 | n/a | def test_double_const(self): |
|---|
| 80 | n/a | # Another brief digression to test the accuracy of manifest float |
|---|
| 81 | n/a | # constants. |
|---|
| 82 | n/a | from test import double_const # don't blink -- that *was* the test |
|---|
| 83 | n/a | |
|---|
| 84 | n/a | def test_import(self): |
|---|
| 85 | n/a | def test_with_extension(ext): |
|---|
| 86 | n/a | # The extension is normally ".py", perhaps ".pyw". |
|---|
| 87 | n/a | source = TESTFN + ext |
|---|
| 88 | n/a | pyo = TESTFN + ".pyo" |
|---|
| 89 | n/a | if is_jython: |
|---|
| 90 | n/a | pyc = TESTFN + "$py.class" |
|---|
| 91 | n/a | else: |
|---|
| 92 | n/a | pyc = TESTFN + ".pyc" |
|---|
| 93 | n/a | |
|---|
| 94 | n/a | with open(source, "w") as f: |
|---|
| 95 | n/a | print("# This tests Python's ability to import a", |
|---|
| 96 | n/a | ext, "file.", file=f) |
|---|
| 97 | n/a | a = random.randrange(1000) |
|---|
| 98 | n/a | b = random.randrange(1000) |
|---|
| 99 | n/a | print("a =", a, file=f) |
|---|
| 100 | n/a | print("b =", b, file=f) |
|---|
| 101 | n/a | |
|---|
| 102 | n/a | if TESTFN in sys.modules: |
|---|
| 103 | n/a | del sys.modules[TESTFN] |
|---|
| 104 | n/a | importlib.invalidate_caches() |
|---|
| 105 | n/a | try: |
|---|
| 106 | n/a | try: |
|---|
| 107 | n/a | mod = __import__(TESTFN) |
|---|
| 108 | n/a | except ImportError as err: |
|---|
| 109 | n/a | self.fail("import from %s failed: %s" % (ext, err)) |
|---|
| 110 | n/a | |
|---|
| 111 | n/a | self.assertEqual(mod.a, a, |
|---|
| 112 | n/a | "module loaded (%s) but contents invalid" % mod) |
|---|
| 113 | n/a | self.assertEqual(mod.b, b, |
|---|
| 114 | n/a | "module loaded (%s) but contents invalid" % mod) |
|---|
| 115 | n/a | finally: |
|---|
| 116 | n/a | forget(TESTFN) |
|---|
| 117 | n/a | unlink(source) |
|---|
| 118 | n/a | unlink(pyc) |
|---|
| 119 | n/a | unlink(pyo) |
|---|
| 120 | n/a | |
|---|
| 121 | n/a | sys.path.insert(0, os.curdir) |
|---|
| 122 | n/a | try: |
|---|
| 123 | n/a | test_with_extension(".py") |
|---|
| 124 | n/a | if sys.platform.startswith("win"): |
|---|
| 125 | n/a | for ext in [".PY", ".Py", ".pY", ".pyw", ".PYW", ".pYw"]: |
|---|
| 126 | n/a | test_with_extension(ext) |
|---|
| 127 | n/a | finally: |
|---|
| 128 | n/a | del sys.path[0] |
|---|
| 129 | n/a | |
|---|
| 130 | n/a | def test_module_with_large_stack(self, module='longlist'): |
|---|
| 131 | n/a | # Regression test for http://bugs.python.org/issue561858. |
|---|
| 132 | n/a | filename = module + '.py' |
|---|
| 133 | n/a | |
|---|
| 134 | n/a | # Create a file with a list of 65000 elements. |
|---|
| 135 | n/a | with open(filename, 'w') as f: |
|---|
| 136 | n/a | f.write('d = [\n') |
|---|
| 137 | n/a | for i in range(65000): |
|---|
| 138 | n/a | f.write('"",\n') |
|---|
| 139 | n/a | f.write(']') |
|---|
| 140 | n/a | |
|---|
| 141 | n/a | try: |
|---|
| 142 | n/a | # Compile & remove .py file; we only need .pyc (or .pyo). |
|---|
| 143 | n/a | # Bytecode must be relocated from the PEP 3147 bytecode-only location. |
|---|
| 144 | n/a | py_compile.compile(filename) |
|---|
| 145 | n/a | finally: |
|---|
| 146 | n/a | unlink(filename) |
|---|
| 147 | n/a | |
|---|
| 148 | n/a | # Need to be able to load from current dir. |
|---|
| 149 | n/a | sys.path.append('') |
|---|
| 150 | n/a | importlib.invalidate_caches() |
|---|
| 151 | n/a | |
|---|
| 152 | n/a | try: |
|---|
| 153 | n/a | make_legacy_pyc(filename) |
|---|
| 154 | n/a | # This used to crash. |
|---|
| 155 | n/a | exec('import ' + module) |
|---|
| 156 | n/a | finally: |
|---|
| 157 | n/a | # Cleanup. |
|---|
| 158 | n/a | del sys.path[-1] |
|---|
| 159 | n/a | unlink(filename + 'c') |
|---|
| 160 | n/a | unlink(filename + 'o') |
|---|
| 161 | n/a | |
|---|
| 162 | n/a | def test_failing_import_sticks(self): |
|---|
| 163 | n/a | source = TESTFN + ".py" |
|---|
| 164 | n/a | with open(source, "w") as f: |
|---|
| 165 | n/a | print("a = 1/0", file=f) |
|---|
| 166 | n/a | |
|---|
| 167 | n/a | # New in 2.4, we shouldn't be able to import that no matter how often |
|---|
| 168 | n/a | # we try. |
|---|
| 169 | n/a | sys.path.insert(0, os.curdir) |
|---|
| 170 | n/a | importlib.invalidate_caches() |
|---|
| 171 | n/a | if TESTFN in sys.modules: |
|---|
| 172 | n/a | del sys.modules[TESTFN] |
|---|
| 173 | n/a | try: |
|---|
| 174 | n/a | for i in [1, 2, 3]: |
|---|
| 175 | n/a | self.assertRaises(ZeroDivisionError, __import__, TESTFN) |
|---|
| 176 | n/a | self.assertNotIn(TESTFN, sys.modules, |
|---|
| 177 | n/a | "damaged module in sys.modules on %i try" % i) |
|---|
| 178 | n/a | finally: |
|---|
| 179 | n/a | del sys.path[0] |
|---|
| 180 | n/a | remove_files(TESTFN) |
|---|
| 181 | n/a | |
|---|
| 182 | n/a | def test_import_name_binding(self): |
|---|
| 183 | n/a | # import x.y.z binds x in the current namespace |
|---|
| 184 | n/a | import test as x |
|---|
| 185 | n/a | import test.support |
|---|
| 186 | n/a | self.assertTrue(x is test, x.__name__) |
|---|
| 187 | n/a | self.assertTrue(hasattr(test.support, "__file__")) |
|---|
| 188 | n/a | |
|---|
| 189 | n/a | # import x.y.z as w binds z as w |
|---|
| 190 | n/a | import test.support as y |
|---|
| 191 | n/a | self.assertTrue(y is test.support, y.__name__) |
|---|
| 192 | n/a | |
|---|
| 193 | n/a | def test_failing_reload(self): |
|---|
| 194 | n/a | # A failing reload should leave the module object in sys.modules. |
|---|
| 195 | n/a | source = TESTFN + os.extsep + "py" |
|---|
| 196 | n/a | with open(source, "w") as f: |
|---|
| 197 | n/a | f.write("a = 1\nb=2\n") |
|---|
| 198 | n/a | |
|---|
| 199 | n/a | sys.path.insert(0, os.curdir) |
|---|
| 200 | n/a | try: |
|---|
| 201 | n/a | mod = __import__(TESTFN) |
|---|
| 202 | n/a | self.assertIn(TESTFN, sys.modules) |
|---|
| 203 | n/a | self.assertEqual(mod.a, 1, "module has wrong attribute values") |
|---|
| 204 | n/a | self.assertEqual(mod.b, 2, "module has wrong attribute values") |
|---|
| 205 | n/a | |
|---|
| 206 | n/a | # On WinXP, just replacing the .py file wasn't enough to |
|---|
| 207 | n/a | # convince reload() to reparse it. Maybe the timestamp didn't |
|---|
| 208 | n/a | # move enough. We force it to get reparsed by removing the |
|---|
| 209 | n/a | # compiled file too. |
|---|
| 210 | n/a | remove_files(TESTFN) |
|---|
| 211 | n/a | |
|---|
| 212 | n/a | # Now damage the module. |
|---|
| 213 | n/a | with open(source, "w") as f: |
|---|
| 214 | n/a | f.write("a = 10\nb=20//0\n") |
|---|
| 215 | n/a | |
|---|
| 216 | n/a | self.assertRaises(ZeroDivisionError, importlib.reload, mod) |
|---|
| 217 | n/a | # But we still expect the module to be in sys.modules. |
|---|
| 218 | n/a | mod = sys.modules.get(TESTFN) |
|---|
| 219 | n/a | self.assertIsNot(mod, None, "expected module to be in sys.modules") |
|---|
| 220 | n/a | |
|---|
| 221 | n/a | # We should have replaced a w/ 10, but the old b value should |
|---|
| 222 | n/a | # stick. |
|---|
| 223 | n/a | self.assertEqual(mod.a, 10, "module has wrong attribute values") |
|---|
| 224 | n/a | self.assertEqual(mod.b, 2, "module has wrong attribute values") |
|---|
| 225 | n/a | |
|---|
| 226 | n/a | finally: |
|---|
| 227 | n/a | del sys.path[0] |
|---|
| 228 | n/a | remove_files(TESTFN) |
|---|
| 229 | n/a | unload(TESTFN) |
|---|
| 230 | n/a | |
|---|
| 231 | n/a | @skip_if_dont_write_bytecode |
|---|
| 232 | n/a | def test_file_to_source(self): |
|---|
| 233 | n/a | # check if __file__ points to the source file where available |
|---|
| 234 | n/a | source = TESTFN + ".py" |
|---|
| 235 | n/a | with open(source, "w") as f: |
|---|
| 236 | n/a | f.write("test = None\n") |
|---|
| 237 | n/a | |
|---|
| 238 | n/a | sys.path.insert(0, os.curdir) |
|---|
| 239 | n/a | try: |
|---|
| 240 | n/a | mod = __import__(TESTFN) |
|---|
| 241 | n/a | self.assertTrue(mod.__file__.endswith('.py')) |
|---|
| 242 | n/a | os.remove(source) |
|---|
| 243 | n/a | del sys.modules[TESTFN] |
|---|
| 244 | n/a | make_legacy_pyc(source) |
|---|
| 245 | n/a | importlib.invalidate_caches() |
|---|
| 246 | n/a | mod = __import__(TESTFN) |
|---|
| 247 | n/a | base, ext = os.path.splitext(mod.__file__) |
|---|
| 248 | n/a | self.assertIn(ext, ('.pyc', '.pyo')) |
|---|
| 249 | n/a | finally: |
|---|
| 250 | n/a | del sys.path[0] |
|---|
| 251 | n/a | remove_files(TESTFN) |
|---|
| 252 | n/a | if TESTFN in sys.modules: |
|---|
| 253 | n/a | del sys.modules[TESTFN] |
|---|
| 254 | n/a | |
|---|
| 255 | n/a | def test_import_name_binding(self): |
|---|
| 256 | n/a | # import x.y.z binds x in the current namespace. |
|---|
| 257 | n/a | import test as x |
|---|
| 258 | n/a | import test.support |
|---|
| 259 | n/a | self.assertIs(x, test, x.__name__) |
|---|
| 260 | n/a | self.assertTrue(hasattr(test.support, "__file__")) |
|---|
| 261 | n/a | |
|---|
| 262 | n/a | # import x.y.z as w binds z as w. |
|---|
| 263 | n/a | import test.support as y |
|---|
| 264 | n/a | self.assertIs(y, test.support, y.__name__) |
|---|
| 265 | n/a | |
|---|
| 266 | n/a | def test_import_by_filename(self): |
|---|
| 267 | n/a | path = os.path.abspath(TESTFN) |
|---|
| 268 | n/a | encoding = sys.getfilesystemencoding() |
|---|
| 269 | n/a | try: |
|---|
| 270 | n/a | path.encode(encoding) |
|---|
| 271 | n/a | except UnicodeEncodeError: |
|---|
| 272 | n/a | self.skipTest('path is not encodable to {}'.format(encoding)) |
|---|
| 273 | n/a | with self.assertRaises(ImportError) as c: |
|---|
| 274 | n/a | __import__(path) |
|---|
| 275 | n/a | |
|---|
| 276 | n/a | def test_import_in_del_does_not_crash(self): |
|---|
| 277 | n/a | # Issue 4236 |
|---|
| 278 | n/a | testfn = script_helper.make_script('', TESTFN, textwrap.dedent("""\ |
|---|
| 279 | n/a | import sys |
|---|
| 280 | n/a | class C: |
|---|
| 281 | n/a | def __del__(self): |
|---|
| 282 | n/a | import importlib |
|---|
| 283 | n/a | sys.argv.insert(0, C()) |
|---|
| 284 | n/a | """)) |
|---|
| 285 | n/a | script_helper.assert_python_ok(testfn) |
|---|
| 286 | n/a | |
|---|
| 287 | n/a | def test_timestamp_overflow(self): |
|---|
| 288 | n/a | # A modification timestamp larger than 2**32 should not be a problem |
|---|
| 289 | n/a | # when importing a module (issue #11235). |
|---|
| 290 | n/a | sys.path.insert(0, os.curdir) |
|---|
| 291 | n/a | try: |
|---|
| 292 | n/a | source = TESTFN + ".py" |
|---|
| 293 | n/a | compiled = importlib.util.cache_from_source(source) |
|---|
| 294 | n/a | with open(source, 'w') as f: |
|---|
| 295 | n/a | pass |
|---|
| 296 | n/a | try: |
|---|
| 297 | n/a | os.utime(source, (2 ** 33 - 5, 2 ** 33 - 5)) |
|---|
| 298 | n/a | except OverflowError: |
|---|
| 299 | n/a | self.skipTest("cannot set modification time to large integer") |
|---|
| 300 | n/a | except OSError as e: |
|---|
| 301 | n/a | if e.errno != getattr(errno, 'EOVERFLOW', None): |
|---|
| 302 | n/a | raise |
|---|
| 303 | n/a | self.skipTest("cannot set modification time to large integer ({})".format(e)) |
|---|
| 304 | n/a | __import__(TESTFN) |
|---|
| 305 | n/a | # The pyc file was created. |
|---|
| 306 | n/a | os.stat(compiled) |
|---|
| 307 | n/a | finally: |
|---|
| 308 | n/a | del sys.path[0] |
|---|
| 309 | n/a | remove_files(TESTFN) |
|---|
| 310 | n/a | |
|---|
| 311 | n/a | def test_bogus_fromlist(self): |
|---|
| 312 | n/a | try: |
|---|
| 313 | n/a | __import__('http', fromlist=['blah']) |
|---|
| 314 | n/a | except ImportError: |
|---|
| 315 | n/a | self.fail("fromlist must allow bogus names") |
|---|
| 316 | n/a | |
|---|
| 317 | n/a | @cpython_only |
|---|
| 318 | n/a | def test_delete_builtins_import(self): |
|---|
| 319 | n/a | args = ["-c", "del __builtins__.__import__; import os"] |
|---|
| 320 | n/a | popen = script_helper.spawn_python(*args) |
|---|
| 321 | n/a | stdout, stderr = popen.communicate() |
|---|
| 322 | n/a | self.assertIn(b"ImportError", stdout) |
|---|
| 323 | n/a | |
|---|
| 324 | n/a | def test_from_import_message_for_nonexistent_module(self): |
|---|
| 325 | n/a | with self.assertRaisesRegexp(ImportError, "^No module named 'bogus'"): |
|---|
| 326 | n/a | from bogus import foo |
|---|
| 327 | n/a | |
|---|
| 328 | n/a | def test_from_import_message_for_existing_module(self): |
|---|
| 329 | n/a | with self.assertRaisesRegexp(ImportError, "^cannot import name 'bogus'"): |
|---|
| 330 | n/a | from re import bogus |
|---|
| 331 | n/a | |
|---|
| 332 | n/a | |
|---|
| 333 | n/a | @skip_if_dont_write_bytecode |
|---|
| 334 | n/a | class FilePermissionTests(unittest.TestCase): |
|---|
| 335 | n/a | # tests for file mode on cached .pyc/.pyo files |
|---|
| 336 | n/a | |
|---|
| 337 | n/a | @unittest.skipUnless(os.name == 'posix', |
|---|
| 338 | n/a | "test meaningful only on posix systems") |
|---|
| 339 | n/a | def test_creation_mode(self): |
|---|
| 340 | n/a | mask = 0o022 |
|---|
| 341 | n/a | with temp_umask(mask), _ready_to_import() as (name, path): |
|---|
| 342 | n/a | cached_path = importlib.util.cache_from_source(path) |
|---|
| 343 | n/a | module = __import__(name) |
|---|
| 344 | n/a | if not os.path.exists(cached_path): |
|---|
| 345 | n/a | self.fail("__import__ did not result in creation of " |
|---|
| 346 | n/a | "either a .pyc or .pyo file") |
|---|
| 347 | n/a | stat_info = os.stat(cached_path) |
|---|
| 348 | n/a | |
|---|
| 349 | n/a | # Check that the umask is respected, and the executable bits |
|---|
| 350 | n/a | # aren't set. |
|---|
| 351 | n/a | self.assertEqual(oct(stat.S_IMODE(stat_info.st_mode)), |
|---|
| 352 | n/a | oct(0o666 & ~mask)) |
|---|
| 353 | n/a | |
|---|
| 354 | n/a | @unittest.skipUnless(os.name == 'posix', |
|---|
| 355 | n/a | "test meaningful only on posix systems") |
|---|
| 356 | n/a | def test_cached_mode_issue_2051(self): |
|---|
| 357 | n/a | # permissions of .pyc should match those of .py, regardless of mask |
|---|
| 358 | n/a | mode = 0o600 |
|---|
| 359 | n/a | with temp_umask(0o022), _ready_to_import() as (name, path): |
|---|
| 360 | n/a | cached_path = importlib.util.cache_from_source(path) |
|---|
| 361 | n/a | os.chmod(path, mode) |
|---|
| 362 | n/a | __import__(name) |
|---|
| 363 | n/a | if not os.path.exists(cached_path): |
|---|
| 364 | n/a | self.fail("__import__ did not result in creation of " |
|---|
| 365 | n/a | "either a .pyc or .pyo file") |
|---|
| 366 | n/a | stat_info = os.stat(cached_path) |
|---|
| 367 | n/a | |
|---|
| 368 | n/a | self.assertEqual(oct(stat.S_IMODE(stat_info.st_mode)), oct(mode)) |
|---|
| 369 | n/a | |
|---|
| 370 | n/a | @unittest.skipUnless(os.name == 'posix', |
|---|
| 371 | n/a | "test meaningful only on posix systems") |
|---|
| 372 | n/a | def test_cached_readonly(self): |
|---|
| 373 | n/a | mode = 0o400 |
|---|
| 374 | n/a | with temp_umask(0o022), _ready_to_import() as (name, path): |
|---|
| 375 | n/a | cached_path = importlib.util.cache_from_source(path) |
|---|
| 376 | n/a | os.chmod(path, mode) |
|---|
| 377 | n/a | __import__(name) |
|---|
| 378 | n/a | if not os.path.exists(cached_path): |
|---|
| 379 | n/a | self.fail("__import__ did not result in creation of " |
|---|
| 380 | n/a | "either a .pyc or .pyo file") |
|---|
| 381 | n/a | stat_info = os.stat(cached_path) |
|---|
| 382 | n/a | |
|---|
| 383 | n/a | expected = mode | 0o200 # Account for fix for issue #6074 |
|---|
| 384 | n/a | self.assertEqual(oct(stat.S_IMODE(stat_info.st_mode)), oct(expected)) |
|---|
| 385 | n/a | |
|---|
| 386 | n/a | def test_pyc_always_writable(self): |
|---|
| 387 | n/a | # Initially read-only .pyc files on Windows used to cause problems |
|---|
| 388 | n/a | # with later updates, see issue #6074 for details |
|---|
| 389 | n/a | with _ready_to_import() as (name, path): |
|---|
| 390 | n/a | # Write a Python file, make it read-only and import it |
|---|
| 391 | n/a | with open(path, 'w') as f: |
|---|
| 392 | n/a | f.write("x = 'original'\n") |
|---|
| 393 | n/a | # Tweak the mtime of the source to ensure pyc gets updated later |
|---|
| 394 | n/a | s = os.stat(path) |
|---|
| 395 | n/a | os.utime(path, (s.st_atime, s.st_mtime-100000000)) |
|---|
| 396 | n/a | os.chmod(path, 0o400) |
|---|
| 397 | n/a | m = __import__(name) |
|---|
| 398 | n/a | self.assertEqual(m.x, 'original') |
|---|
| 399 | n/a | # Change the file and then reimport it |
|---|
| 400 | n/a | os.chmod(path, 0o600) |
|---|
| 401 | n/a | with open(path, 'w') as f: |
|---|
| 402 | n/a | f.write("x = 'rewritten'\n") |
|---|
| 403 | n/a | unload(name) |
|---|
| 404 | n/a | importlib.invalidate_caches() |
|---|
| 405 | n/a | m = __import__(name) |
|---|
| 406 | n/a | self.assertEqual(m.x, 'rewritten') |
|---|
| 407 | n/a | # Now delete the source file and check the pyc was rewritten |
|---|
| 408 | n/a | unlink(path) |
|---|
| 409 | n/a | unload(name) |
|---|
| 410 | n/a | importlib.invalidate_caches() |
|---|
| 411 | n/a | if __debug__: |
|---|
| 412 | n/a | bytecode_only = path + "c" |
|---|
| 413 | n/a | else: |
|---|
| 414 | n/a | bytecode_only = path + "o" |
|---|
| 415 | n/a | os.rename(importlib.util.cache_from_source(path), bytecode_only) |
|---|
| 416 | n/a | m = __import__(name) |
|---|
| 417 | n/a | self.assertEqual(m.x, 'rewritten') |
|---|
| 418 | n/a | |
|---|
| 419 | n/a | |
|---|
| 420 | n/a | class PycRewritingTests(unittest.TestCase): |
|---|
| 421 | n/a | # Test that the `co_filename` attribute on code objects always points |
|---|
| 422 | n/a | # to the right file, even when various things happen (e.g. both the .py |
|---|
| 423 | n/a | # and the .pyc file are renamed). |
|---|
| 424 | n/a | |
|---|
| 425 | n/a | module_name = "unlikely_module_name" |
|---|
| 426 | n/a | module_source = """ |
|---|
| 427 | n/a | import sys |
|---|
| 428 | n/a | code_filename = sys._getframe().f_code.co_filename |
|---|
| 429 | n/a | module_filename = __file__ |
|---|
| 430 | n/a | constant = 1 |
|---|
| 431 | n/a | def func(): |
|---|
| 432 | n/a | pass |
|---|
| 433 | n/a | func_filename = func.__code__.co_filename |
|---|
| 434 | n/a | """ |
|---|
| 435 | n/a | dir_name = os.path.abspath(TESTFN) |
|---|
| 436 | n/a | file_name = os.path.join(dir_name, module_name) + os.extsep + "py" |
|---|
| 437 | n/a | compiled_name = importlib.util.cache_from_source(file_name) |
|---|
| 438 | n/a | |
|---|
| 439 | n/a | def setUp(self): |
|---|
| 440 | n/a | self.sys_path = sys.path[:] |
|---|
| 441 | n/a | self.orig_module = sys.modules.pop(self.module_name, None) |
|---|
| 442 | n/a | os.mkdir(self.dir_name) |
|---|
| 443 | n/a | with open(self.file_name, "w") as f: |
|---|
| 444 | n/a | f.write(self.module_source) |
|---|
| 445 | n/a | sys.path.insert(0, self.dir_name) |
|---|
| 446 | n/a | importlib.invalidate_caches() |
|---|
| 447 | n/a | |
|---|
| 448 | n/a | def tearDown(self): |
|---|
| 449 | n/a | sys.path[:] = self.sys_path |
|---|
| 450 | n/a | if self.orig_module is not None: |
|---|
| 451 | n/a | sys.modules[self.module_name] = self.orig_module |
|---|
| 452 | n/a | else: |
|---|
| 453 | n/a | unload(self.module_name) |
|---|
| 454 | n/a | unlink(self.file_name) |
|---|
| 455 | n/a | unlink(self.compiled_name) |
|---|
| 456 | n/a | rmtree(self.dir_name) |
|---|
| 457 | n/a | |
|---|
| 458 | n/a | def import_module(self): |
|---|
| 459 | n/a | ns = globals() |
|---|
| 460 | n/a | __import__(self.module_name, ns, ns) |
|---|
| 461 | n/a | return sys.modules[self.module_name] |
|---|
| 462 | n/a | |
|---|
| 463 | n/a | def test_basics(self): |
|---|
| 464 | n/a | mod = self.import_module() |
|---|
| 465 | n/a | self.assertEqual(mod.module_filename, self.file_name) |
|---|
| 466 | n/a | self.assertEqual(mod.code_filename, self.file_name) |
|---|
| 467 | n/a | self.assertEqual(mod.func_filename, self.file_name) |
|---|
| 468 | n/a | del sys.modules[self.module_name] |
|---|
| 469 | n/a | mod = self.import_module() |
|---|
| 470 | n/a | self.assertEqual(mod.module_filename, self.file_name) |
|---|
| 471 | n/a | self.assertEqual(mod.code_filename, self.file_name) |
|---|
| 472 | n/a | self.assertEqual(mod.func_filename, self.file_name) |
|---|
| 473 | n/a | |
|---|
| 474 | n/a | def test_incorrect_code_name(self): |
|---|
| 475 | n/a | py_compile.compile(self.file_name, dfile="another_module.py") |
|---|
| 476 | n/a | mod = self.import_module() |
|---|
| 477 | n/a | self.assertEqual(mod.module_filename, self.file_name) |
|---|
| 478 | n/a | self.assertEqual(mod.code_filename, self.file_name) |
|---|
| 479 | n/a | self.assertEqual(mod.func_filename, self.file_name) |
|---|
| 480 | n/a | |
|---|
| 481 | n/a | def test_module_without_source(self): |
|---|
| 482 | n/a | target = "another_module.py" |
|---|
| 483 | n/a | py_compile.compile(self.file_name, dfile=target) |
|---|
| 484 | n/a | os.remove(self.file_name) |
|---|
| 485 | n/a | pyc_file = make_legacy_pyc(self.file_name) |
|---|
| 486 | n/a | importlib.invalidate_caches() |
|---|
| 487 | n/a | mod = self.import_module() |
|---|
| 488 | n/a | self.assertEqual(mod.module_filename, pyc_file) |
|---|
| 489 | n/a | self.assertEqual(mod.code_filename, target) |
|---|
| 490 | n/a | self.assertEqual(mod.func_filename, target) |
|---|
| 491 | n/a | |
|---|
| 492 | n/a | def test_foreign_code(self): |
|---|
| 493 | n/a | py_compile.compile(self.file_name) |
|---|
| 494 | n/a | with open(self.compiled_name, "rb") as f: |
|---|
| 495 | n/a | header = f.read(12) |
|---|
| 496 | n/a | code = marshal.load(f) |
|---|
| 497 | n/a | constants = list(code.co_consts) |
|---|
| 498 | n/a | foreign_code = importlib.import_module.__code__ |
|---|
| 499 | n/a | pos = constants.index(1) |
|---|
| 500 | n/a | constants[pos] = foreign_code |
|---|
| 501 | n/a | code = type(code)(code.co_argcount, code.co_kwonlyargcount, |
|---|
| 502 | n/a | code.co_nlocals, code.co_stacksize, |
|---|
| 503 | n/a | code.co_flags, code.co_code, tuple(constants), |
|---|
| 504 | n/a | code.co_names, code.co_varnames, code.co_filename, |
|---|
| 505 | n/a | code.co_name, code.co_firstlineno, code.co_lnotab, |
|---|
| 506 | n/a | code.co_freevars, code.co_cellvars) |
|---|
| 507 | n/a | with open(self.compiled_name, "wb") as f: |
|---|
| 508 | n/a | f.write(header) |
|---|
| 509 | n/a | marshal.dump(code, f) |
|---|
| 510 | n/a | mod = self.import_module() |
|---|
| 511 | n/a | self.assertEqual(mod.constant.co_filename, foreign_code.co_filename) |
|---|
| 512 | n/a | |
|---|
| 513 | n/a | |
|---|
| 514 | n/a | class PathsTests(unittest.TestCase): |
|---|
| 515 | n/a | SAMPLES = ('test', 'test\u00e4\u00f6\u00fc\u00df', 'test\u00e9\u00e8', |
|---|
| 516 | n/a | 'test\u00b0\u00b3\u00b2') |
|---|
| 517 | n/a | path = TESTFN |
|---|
| 518 | n/a | |
|---|
| 519 | n/a | def setUp(self): |
|---|
| 520 | n/a | os.mkdir(self.path) |
|---|
| 521 | n/a | self.syspath = sys.path[:] |
|---|
| 522 | n/a | |
|---|
| 523 | n/a | def tearDown(self): |
|---|
| 524 | n/a | rmtree(self.path) |
|---|
| 525 | n/a | sys.path[:] = self.syspath |
|---|
| 526 | n/a | |
|---|
| 527 | n/a | # Regression test for http://bugs.python.org/issue1293. |
|---|
| 528 | n/a | def test_trailing_slash(self): |
|---|
| 529 | n/a | with open(os.path.join(self.path, 'test_trailing_slash.py'), 'w') as f: |
|---|
| 530 | n/a | f.write("testdata = 'test_trailing_slash'") |
|---|
| 531 | n/a | sys.path.append(self.path+'/') |
|---|
| 532 | n/a | mod = __import__("test_trailing_slash") |
|---|
| 533 | n/a | self.assertEqual(mod.testdata, 'test_trailing_slash') |
|---|
| 534 | n/a | unload("test_trailing_slash") |
|---|
| 535 | n/a | |
|---|
| 536 | n/a | # Regression test for http://bugs.python.org/issue3677. |
|---|
| 537 | n/a | @unittest.skipUnless(sys.platform == 'win32', 'Windows-specific') |
|---|
| 538 | n/a | def test_UNC_path(self): |
|---|
| 539 | n/a | with open(os.path.join(self.path, 'test_unc_path.py'), 'w') as f: |
|---|
| 540 | n/a | f.write("testdata = 'test_unc_path'") |
|---|
| 541 | n/a | importlib.invalidate_caches() |
|---|
| 542 | n/a | # Create the UNC path, like \\myhost\c$\foo\bar. |
|---|
| 543 | n/a | path = os.path.abspath(self.path) |
|---|
| 544 | n/a | import socket |
|---|
| 545 | n/a | hn = socket.gethostname() |
|---|
| 546 | n/a | drive = path[0] |
|---|
| 547 | n/a | unc = "\\\\%s\\%s$"%(hn, drive) |
|---|
| 548 | n/a | unc += path[2:] |
|---|
| 549 | n/a | try: |
|---|
| 550 | n/a | os.listdir(unc) |
|---|
| 551 | n/a | except OSError as e: |
|---|
| 552 | n/a | if e.errno in (errno.EPERM, errno.EACCES): |
|---|
| 553 | n/a | # See issue #15338 |
|---|
| 554 | n/a | self.skipTest("cannot access administrative share %r" % (unc,)) |
|---|
| 555 | n/a | raise |
|---|
| 556 | n/a | sys.path.insert(0, unc) |
|---|
| 557 | n/a | try: |
|---|
| 558 | n/a | mod = __import__("test_unc_path") |
|---|
| 559 | n/a | except ImportError as e: |
|---|
| 560 | n/a | self.fail("could not import 'test_unc_path' from %r: %r" |
|---|
| 561 | n/a | % (unc, e)) |
|---|
| 562 | n/a | self.assertEqual(mod.testdata, 'test_unc_path') |
|---|
| 563 | n/a | self.assertTrue(mod.__file__.startswith(unc), mod.__file__) |
|---|
| 564 | n/a | unload("test_unc_path") |
|---|
| 565 | n/a | |
|---|
| 566 | n/a | |
|---|
| 567 | n/a | class RelativeImportTests(unittest.TestCase): |
|---|
| 568 | n/a | |
|---|
| 569 | n/a | def tearDown(self): |
|---|
| 570 | n/a | unload("test.relimport") |
|---|
| 571 | n/a | setUp = tearDown |
|---|
| 572 | n/a | |
|---|
| 573 | n/a | def test_relimport_star(self): |
|---|
| 574 | n/a | # This will import * from .test_import. |
|---|
| 575 | n/a | from . import relimport |
|---|
| 576 | n/a | self.assertTrue(hasattr(relimport, "RelativeImportTests")) |
|---|
| 577 | n/a | |
|---|
| 578 | n/a | def test_issue3221(self): |
|---|
| 579 | n/a | # Note for mergers: the 'absolute' tests from the 2.x branch |
|---|
| 580 | n/a | # are missing in Py3k because implicit relative imports are |
|---|
| 581 | n/a | # a thing of the past |
|---|
| 582 | n/a | # |
|---|
| 583 | n/a | # Regression test for http://bugs.python.org/issue3221. |
|---|
| 584 | n/a | def check_relative(): |
|---|
| 585 | n/a | exec("from . import relimport", ns) |
|---|
| 586 | n/a | |
|---|
| 587 | n/a | # Check relative import OK with __package__ and __name__ correct |
|---|
| 588 | n/a | ns = dict(__package__='test', __name__='test.notarealmodule') |
|---|
| 589 | n/a | check_relative() |
|---|
| 590 | n/a | |
|---|
| 591 | n/a | # Check relative import OK with only __name__ wrong |
|---|
| 592 | n/a | ns = dict(__package__='test', __name__='notarealpkg.notarealmodule') |
|---|
| 593 | n/a | check_relative() |
|---|
| 594 | n/a | |
|---|
| 595 | n/a | # Check relative import fails with only __package__ wrong |
|---|
| 596 | n/a | ns = dict(__package__='foo', __name__='test.notarealmodule') |
|---|
| 597 | n/a | self.assertRaises(SystemError, check_relative) |
|---|
| 598 | n/a | |
|---|
| 599 | n/a | # Check relative import fails with __package__ and __name__ wrong |
|---|
| 600 | n/a | ns = dict(__package__='foo', __name__='notarealpkg.notarealmodule') |
|---|
| 601 | n/a | self.assertRaises(SystemError, check_relative) |
|---|
| 602 | n/a | |
|---|
| 603 | n/a | # Check relative import fails with package set to a non-string |
|---|
| 604 | n/a | ns = dict(__package__=object()) |
|---|
| 605 | n/a | self.assertRaises(TypeError, check_relative) |
|---|
| 606 | n/a | |
|---|
| 607 | n/a | def test_absolute_import_without_future(self): |
|---|
| 608 | n/a | # If explicit relative import syntax is used, then do not try |
|---|
| 609 | n/a | # to perform an absolute import in the face of failure. |
|---|
| 610 | n/a | # Issue #7902. |
|---|
| 611 | n/a | with self.assertRaises(ImportError): |
|---|
| 612 | n/a | from .os import sep |
|---|
| 613 | n/a | self.fail("explicit relative import triggered an " |
|---|
| 614 | n/a | "implicit absolute import") |
|---|
| 615 | n/a | |
|---|
| 616 | n/a | |
|---|
| 617 | n/a | class OverridingImportBuiltinTests(unittest.TestCase): |
|---|
| 618 | n/a | def test_override_builtin(self): |
|---|
| 619 | n/a | # Test that overriding builtins.__import__ can bypass sys.modules. |
|---|
| 620 | n/a | import os |
|---|
| 621 | n/a | |
|---|
| 622 | n/a | def foo(): |
|---|
| 623 | n/a | import os |
|---|
| 624 | n/a | return os |
|---|
| 625 | n/a | self.assertEqual(foo(), os) # Quick sanity check. |
|---|
| 626 | n/a | |
|---|
| 627 | n/a | with swap_attr(builtins, "__import__", lambda *x: 5): |
|---|
| 628 | n/a | self.assertEqual(foo(), 5) |
|---|
| 629 | n/a | |
|---|
| 630 | n/a | # Test what happens when we shadow __import__ in globals(); this |
|---|
| 631 | n/a | # currently does not impact the import process, but if this changes, |
|---|
| 632 | n/a | # other code will need to change, so keep this test as a tripwire. |
|---|
| 633 | n/a | with swap_item(globals(), "__import__", lambda *x: 5): |
|---|
| 634 | n/a | self.assertEqual(foo(), os) |
|---|
| 635 | n/a | |
|---|
| 636 | n/a | |
|---|
| 637 | n/a | class PycacheTests(unittest.TestCase): |
|---|
| 638 | n/a | # Test the various PEP 3147 related behaviors. |
|---|
| 639 | n/a | |
|---|
| 640 | n/a | tag = sys.implementation.cache_tag |
|---|
| 641 | n/a | |
|---|
| 642 | n/a | def _clean(self): |
|---|
| 643 | n/a | forget(TESTFN) |
|---|
| 644 | n/a | rmtree('__pycache__') |
|---|
| 645 | n/a | unlink(self.source) |
|---|
| 646 | n/a | |
|---|
| 647 | n/a | def setUp(self): |
|---|
| 648 | n/a | self.source = TESTFN + '.py' |
|---|
| 649 | n/a | self._clean() |
|---|
| 650 | n/a | with open(self.source, 'w') as fp: |
|---|
| 651 | n/a | print('# This is a test file written by test_import.py', file=fp) |
|---|
| 652 | n/a | sys.path.insert(0, os.curdir) |
|---|
| 653 | n/a | importlib.invalidate_caches() |
|---|
| 654 | n/a | |
|---|
| 655 | n/a | def tearDown(self): |
|---|
| 656 | n/a | assert sys.path[0] == os.curdir, 'Unexpected sys.path[0]' |
|---|
| 657 | n/a | del sys.path[0] |
|---|
| 658 | n/a | self._clean() |
|---|
| 659 | n/a | |
|---|
| 660 | n/a | @skip_if_dont_write_bytecode |
|---|
| 661 | n/a | def test_import_pyc_path(self): |
|---|
| 662 | n/a | self.assertFalse(os.path.exists('__pycache__')) |
|---|
| 663 | n/a | __import__(TESTFN) |
|---|
| 664 | n/a | self.assertTrue(os.path.exists('__pycache__')) |
|---|
| 665 | n/a | self.assertTrue(os.path.exists(os.path.join( |
|---|
| 666 | n/a | '__pycache__', '{}.{}.py{}'.format( |
|---|
| 667 | n/a | TESTFN, self.tag, 'c' if __debug__ else 'o')))) |
|---|
| 668 | n/a | |
|---|
| 669 | n/a | @unittest.skipUnless(os.name == 'posix', |
|---|
| 670 | n/a | "test meaningful only on posix systems") |
|---|
| 671 | n/a | @unittest.skipIf(hasattr(os, 'geteuid') and os.geteuid() == 0, |
|---|
| 672 | n/a | "due to varying filesystem permission semantics (issue #11956)") |
|---|
| 673 | n/a | @skip_if_dont_write_bytecode |
|---|
| 674 | n/a | def test_unwritable_directory(self): |
|---|
| 675 | n/a | # When the umask causes the new __pycache__ directory to be |
|---|
| 676 | n/a | # unwritable, the import still succeeds but no .pyc file is written. |
|---|
| 677 | n/a | with temp_umask(0o222): |
|---|
| 678 | n/a | __import__(TESTFN) |
|---|
| 679 | n/a | self.assertTrue(os.path.exists('__pycache__')) |
|---|
| 680 | n/a | self.assertFalse(os.path.exists(os.path.join( |
|---|
| 681 | n/a | '__pycache__', '{}.{}.pyc'.format(TESTFN, self.tag)))) |
|---|
| 682 | n/a | |
|---|
| 683 | n/a | @skip_if_dont_write_bytecode |
|---|
| 684 | n/a | def test_missing_source(self): |
|---|
| 685 | n/a | # With PEP 3147 cache layout, removing the source but leaving the pyc |
|---|
| 686 | n/a | # file does not satisfy the import. |
|---|
| 687 | n/a | __import__(TESTFN) |
|---|
| 688 | n/a | pyc_file = importlib.util.cache_from_source(self.source) |
|---|
| 689 | n/a | self.assertTrue(os.path.exists(pyc_file)) |
|---|
| 690 | n/a | os.remove(self.source) |
|---|
| 691 | n/a | forget(TESTFN) |
|---|
| 692 | n/a | self.assertRaises(ImportError, __import__, TESTFN) |
|---|
| 693 | n/a | |
|---|
| 694 | n/a | @skip_if_dont_write_bytecode |
|---|
| 695 | n/a | def test_missing_source_legacy(self): |
|---|
| 696 | n/a | # Like test_missing_source() except that for backward compatibility, |
|---|
| 697 | n/a | # when the pyc file lives where the py file would have been (and named |
|---|
| 698 | n/a | # without the tag), it is importable. The __file__ of the imported |
|---|
| 699 | n/a | # module is the pyc location. |
|---|
| 700 | n/a | __import__(TESTFN) |
|---|
| 701 | n/a | # pyc_file gets removed in _clean() via tearDown(). |
|---|
| 702 | n/a | pyc_file = make_legacy_pyc(self.source) |
|---|
| 703 | n/a | os.remove(self.source) |
|---|
| 704 | n/a | unload(TESTFN) |
|---|
| 705 | n/a | importlib.invalidate_caches() |
|---|
| 706 | n/a | m = __import__(TESTFN) |
|---|
| 707 | n/a | self.assertEqual(m.__file__, |
|---|
| 708 | n/a | os.path.join(os.curdir, os.path.relpath(pyc_file))) |
|---|
| 709 | n/a | |
|---|
| 710 | n/a | def test___cached__(self): |
|---|
| 711 | n/a | # Modules now also have an __cached__ that points to the pyc file. |
|---|
| 712 | n/a | m = __import__(TESTFN) |
|---|
| 713 | n/a | pyc_file = importlib.util.cache_from_source(TESTFN + '.py') |
|---|
| 714 | n/a | self.assertEqual(m.__cached__, os.path.join(os.curdir, pyc_file)) |
|---|
| 715 | n/a | |
|---|
| 716 | n/a | @skip_if_dont_write_bytecode |
|---|
| 717 | n/a | def test___cached___legacy_pyc(self): |
|---|
| 718 | n/a | # Like test___cached__() except that for backward compatibility, |
|---|
| 719 | n/a | # when the pyc file lives where the py file would have been (and named |
|---|
| 720 | n/a | # without the tag), it is importable. The __cached__ of the imported |
|---|
| 721 | n/a | # module is the pyc location. |
|---|
| 722 | n/a | __import__(TESTFN) |
|---|
| 723 | n/a | # pyc_file gets removed in _clean() via tearDown(). |
|---|
| 724 | n/a | pyc_file = make_legacy_pyc(self.source) |
|---|
| 725 | n/a | os.remove(self.source) |
|---|
| 726 | n/a | unload(TESTFN) |
|---|
| 727 | n/a | importlib.invalidate_caches() |
|---|
| 728 | n/a | m = __import__(TESTFN) |
|---|
| 729 | n/a | self.assertEqual(m.__cached__, |
|---|
| 730 | n/a | os.path.join(os.curdir, os.path.relpath(pyc_file))) |
|---|
| 731 | n/a | |
|---|
| 732 | n/a | @skip_if_dont_write_bytecode |
|---|
| 733 | n/a | def test_package___cached__(self): |
|---|
| 734 | n/a | # Like test___cached__ but for packages. |
|---|
| 735 | n/a | def cleanup(): |
|---|
| 736 | n/a | rmtree('pep3147') |
|---|
| 737 | n/a | unload('pep3147.foo') |
|---|
| 738 | n/a | unload('pep3147') |
|---|
| 739 | n/a | os.mkdir('pep3147') |
|---|
| 740 | n/a | self.addCleanup(cleanup) |
|---|
| 741 | n/a | # Touch the __init__.py |
|---|
| 742 | n/a | with open(os.path.join('pep3147', '__init__.py'), 'w'): |
|---|
| 743 | n/a | pass |
|---|
| 744 | n/a | with open(os.path.join('pep3147', 'foo.py'), 'w'): |
|---|
| 745 | n/a | pass |
|---|
| 746 | n/a | importlib.invalidate_caches() |
|---|
| 747 | n/a | m = __import__('pep3147.foo') |
|---|
| 748 | n/a | init_pyc = importlib.util.cache_from_source( |
|---|
| 749 | n/a | os.path.join('pep3147', '__init__.py')) |
|---|
| 750 | n/a | self.assertEqual(m.__cached__, os.path.join(os.curdir, init_pyc)) |
|---|
| 751 | n/a | foo_pyc = importlib.util.cache_from_source(os.path.join('pep3147', 'foo.py')) |
|---|
| 752 | n/a | self.assertEqual(sys.modules['pep3147.foo'].__cached__, |
|---|
| 753 | n/a | os.path.join(os.curdir, foo_pyc)) |
|---|
| 754 | n/a | |
|---|
| 755 | n/a | def test_package___cached___from_pyc(self): |
|---|
| 756 | n/a | # Like test___cached__ but ensuring __cached__ when imported from a |
|---|
| 757 | n/a | # PEP 3147 pyc file. |
|---|
| 758 | n/a | def cleanup(): |
|---|
| 759 | n/a | rmtree('pep3147') |
|---|
| 760 | n/a | unload('pep3147.foo') |
|---|
| 761 | n/a | unload('pep3147') |
|---|
| 762 | n/a | os.mkdir('pep3147') |
|---|
| 763 | n/a | self.addCleanup(cleanup) |
|---|
| 764 | n/a | # Touch the __init__.py |
|---|
| 765 | n/a | with open(os.path.join('pep3147', '__init__.py'), 'w'): |
|---|
| 766 | n/a | pass |
|---|
| 767 | n/a | with open(os.path.join('pep3147', 'foo.py'), 'w'): |
|---|
| 768 | n/a | pass |
|---|
| 769 | n/a | importlib.invalidate_caches() |
|---|
| 770 | n/a | m = __import__('pep3147.foo') |
|---|
| 771 | n/a | unload('pep3147.foo') |
|---|
| 772 | n/a | unload('pep3147') |
|---|
| 773 | n/a | importlib.invalidate_caches() |
|---|
| 774 | n/a | m = __import__('pep3147.foo') |
|---|
| 775 | n/a | init_pyc = importlib.util.cache_from_source( |
|---|
| 776 | n/a | os.path.join('pep3147', '__init__.py')) |
|---|
| 777 | n/a | self.assertEqual(m.__cached__, os.path.join(os.curdir, init_pyc)) |
|---|
| 778 | n/a | foo_pyc = importlib.util.cache_from_source(os.path.join('pep3147', 'foo.py')) |
|---|
| 779 | n/a | self.assertEqual(sys.modules['pep3147.foo'].__cached__, |
|---|
| 780 | n/a | os.path.join(os.curdir, foo_pyc)) |
|---|
| 781 | n/a | |
|---|
| 782 | n/a | def test_recompute_pyc_same_second(self): |
|---|
| 783 | n/a | # Even when the source file doesn't change timestamp, a change in |
|---|
| 784 | n/a | # source size is enough to trigger recomputation of the pyc file. |
|---|
| 785 | n/a | __import__(TESTFN) |
|---|
| 786 | n/a | unload(TESTFN) |
|---|
| 787 | n/a | with open(self.source, 'a') as fp: |
|---|
| 788 | n/a | print("x = 5", file=fp) |
|---|
| 789 | n/a | m = __import__(TESTFN) |
|---|
| 790 | n/a | self.assertEqual(m.x, 5) |
|---|
| 791 | n/a | |
|---|
| 792 | n/a | |
|---|
| 793 | n/a | class TestSymbolicallyLinkedPackage(unittest.TestCase): |
|---|
| 794 | n/a | package_name = 'sample' |
|---|
| 795 | n/a | tagged = package_name + '-tagged' |
|---|
| 796 | n/a | |
|---|
| 797 | n/a | def setUp(self): |
|---|
| 798 | n/a | test.support.rmtree(self.tagged) |
|---|
| 799 | n/a | test.support.rmtree(self.package_name) |
|---|
| 800 | n/a | self.orig_sys_path = sys.path[:] |
|---|
| 801 | n/a | |
|---|
| 802 | n/a | # create a sample package; imagine you have a package with a tag and |
|---|
| 803 | n/a | # you want to symbolically link it from its untagged name. |
|---|
| 804 | n/a | os.mkdir(self.tagged) |
|---|
| 805 | n/a | self.addCleanup(test.support.rmtree, self.tagged) |
|---|
| 806 | n/a | init_file = os.path.join(self.tagged, '__init__.py') |
|---|
| 807 | n/a | test.support.create_empty_file(init_file) |
|---|
| 808 | n/a | assert os.path.exists(init_file) |
|---|
| 809 | n/a | |
|---|
| 810 | n/a | # now create a symlink to the tagged package |
|---|
| 811 | n/a | # sample -> sample-tagged |
|---|
| 812 | n/a | os.symlink(self.tagged, self.package_name, target_is_directory=True) |
|---|
| 813 | n/a | self.addCleanup(test.support.unlink, self.package_name) |
|---|
| 814 | n/a | importlib.invalidate_caches() |
|---|
| 815 | n/a | |
|---|
| 816 | n/a | self.assertEqual(os.path.isdir(self.package_name), True) |
|---|
| 817 | n/a | |
|---|
| 818 | n/a | assert os.path.isfile(os.path.join(self.package_name, '__init__.py')) |
|---|
| 819 | n/a | |
|---|
| 820 | n/a | def tearDown(self): |
|---|
| 821 | n/a | sys.path[:] = self.orig_sys_path |
|---|
| 822 | n/a | |
|---|
| 823 | n/a | # regression test for issue6727 |
|---|
| 824 | n/a | @unittest.skipUnless( |
|---|
| 825 | n/a | not hasattr(sys, 'getwindowsversion') |
|---|
| 826 | n/a | or sys.getwindowsversion() >= (6, 0), |
|---|
| 827 | n/a | "Windows Vista or later required") |
|---|
| 828 | n/a | @test.support.skip_unless_symlink |
|---|
| 829 | n/a | def test_symlinked_dir_importable(self): |
|---|
| 830 | n/a | # make sure sample can only be imported from the current directory. |
|---|
| 831 | n/a | sys.path[:] = ['.'] |
|---|
| 832 | n/a | assert os.path.exists(self.package_name) |
|---|
| 833 | n/a | assert os.path.exists(os.path.join(self.package_name, '__init__.py')) |
|---|
| 834 | n/a | |
|---|
| 835 | n/a | # Try to import the package |
|---|
| 836 | n/a | importlib.import_module(self.package_name) |
|---|
| 837 | n/a | |
|---|
| 838 | n/a | |
|---|
| 839 | n/a | @cpython_only |
|---|
| 840 | n/a | class ImportlibBootstrapTests(unittest.TestCase): |
|---|
| 841 | n/a | # These tests check that importlib is bootstrapped. |
|---|
| 842 | n/a | |
|---|
| 843 | n/a | def test_frozen_importlib(self): |
|---|
| 844 | n/a | mod = sys.modules['_frozen_importlib'] |
|---|
| 845 | n/a | self.assertTrue(mod) |
|---|
| 846 | n/a | |
|---|
| 847 | n/a | def test_frozen_importlib_is_bootstrap(self): |
|---|
| 848 | n/a | from importlib import _bootstrap |
|---|
| 849 | n/a | mod = sys.modules['_frozen_importlib'] |
|---|
| 850 | n/a | self.assertIs(mod, _bootstrap) |
|---|
| 851 | n/a | self.assertEqual(mod.__name__, 'importlib._bootstrap') |
|---|
| 852 | n/a | self.assertEqual(mod.__package__, 'importlib') |
|---|
| 853 | n/a | self.assertTrue(mod.__file__.endswith('_bootstrap.py'), mod.__file__) |
|---|
| 854 | n/a | |
|---|
| 855 | n/a | def test_there_can_be_only_one(self): |
|---|
| 856 | n/a | # Issue #15386 revealed a tricky loophole in the bootstrapping |
|---|
| 857 | n/a | # This test is technically redundant, since the bug caused importing |
|---|
| 858 | n/a | # this test module to crash completely, but it helps prove the point |
|---|
| 859 | n/a | from importlib import machinery |
|---|
| 860 | n/a | mod = sys.modules['_frozen_importlib'] |
|---|
| 861 | n/a | self.assertIs(machinery.FileFinder, mod.FileFinder) |
|---|
| 862 | n/a | |
|---|
| 863 | n/a | |
|---|
| 864 | n/a | @cpython_only |
|---|
| 865 | n/a | class GetSourcefileTests(unittest.TestCase): |
|---|
| 866 | n/a | |
|---|
| 867 | n/a | """Test importlib._bootstrap._get_sourcefile() as used by the C API. |
|---|
| 868 | n/a | |
|---|
| 869 | n/a | Because of the peculiarities of the need of this function, the tests are |
|---|
| 870 | n/a | knowingly whitebox tests. |
|---|
| 871 | n/a | |
|---|
| 872 | n/a | """ |
|---|
| 873 | n/a | |
|---|
| 874 | n/a | def test_get_sourcefile(self): |
|---|
| 875 | n/a | # Given a valid bytecode path, return the path to the corresponding |
|---|
| 876 | n/a | # source file if it exists. |
|---|
| 877 | n/a | with mock.patch('importlib._bootstrap._path_isfile') as _path_isfile: |
|---|
| 878 | n/a | _path_isfile.return_value = True; |
|---|
| 879 | n/a | path = TESTFN + '.pyc' |
|---|
| 880 | n/a | expect = TESTFN + '.py' |
|---|
| 881 | n/a | self.assertEqual(_get_sourcefile(path), expect) |
|---|
| 882 | n/a | |
|---|
| 883 | n/a | def test_get_sourcefile_no_source(self): |
|---|
| 884 | n/a | # Given a valid bytecode path without a corresponding source path, |
|---|
| 885 | n/a | # return the original bytecode path. |
|---|
| 886 | n/a | with mock.patch('importlib._bootstrap._path_isfile') as _path_isfile: |
|---|
| 887 | n/a | _path_isfile.return_value = False; |
|---|
| 888 | n/a | path = TESTFN + '.pyc' |
|---|
| 889 | n/a | self.assertEqual(_get_sourcefile(path), path) |
|---|
| 890 | n/a | |
|---|
| 891 | n/a | def test_get_sourcefile_bad_ext(self): |
|---|
| 892 | n/a | # Given a path with an invalid bytecode extension, return the |
|---|
| 893 | n/a | # bytecode path passed as the argument. |
|---|
| 894 | n/a | path = TESTFN + '.bad_ext' |
|---|
| 895 | n/a | self.assertEqual(_get_sourcefile(path), path) |
|---|
| 896 | n/a | |
|---|
| 897 | n/a | |
|---|
| 898 | n/a | class ImportTracebackTests(unittest.TestCase): |
|---|
| 899 | n/a | |
|---|
| 900 | n/a | def setUp(self): |
|---|
| 901 | n/a | os.mkdir(TESTFN) |
|---|
| 902 | n/a | self.old_path = sys.path[:] |
|---|
| 903 | n/a | sys.path.insert(0, TESTFN) |
|---|
| 904 | n/a | |
|---|
| 905 | n/a | def tearDown(self): |
|---|
| 906 | n/a | sys.path[:] = self.old_path |
|---|
| 907 | n/a | rmtree(TESTFN) |
|---|
| 908 | n/a | |
|---|
| 909 | n/a | def create_module(self, mod, contents, ext=".py"): |
|---|
| 910 | n/a | fname = os.path.join(TESTFN, mod + ext) |
|---|
| 911 | n/a | with open(fname, "w") as f: |
|---|
| 912 | n/a | f.write(contents) |
|---|
| 913 | n/a | self.addCleanup(unload, mod) |
|---|
| 914 | n/a | importlib.invalidate_caches() |
|---|
| 915 | n/a | return fname |
|---|
| 916 | n/a | |
|---|
| 917 | n/a | def assert_traceback(self, tb, files): |
|---|
| 918 | n/a | deduped_files = [] |
|---|
| 919 | n/a | while tb: |
|---|
| 920 | n/a | code = tb.tb_frame.f_code |
|---|
| 921 | n/a | fn = code.co_filename |
|---|
| 922 | n/a | if not deduped_files or fn != deduped_files[-1]: |
|---|
| 923 | n/a | deduped_files.append(fn) |
|---|
| 924 | n/a | tb = tb.tb_next |
|---|
| 925 | n/a | self.assertEqual(len(deduped_files), len(files), deduped_files) |
|---|
| 926 | n/a | for fn, pat in zip(deduped_files, files): |
|---|
| 927 | n/a | self.assertIn(pat, fn) |
|---|
| 928 | n/a | |
|---|
| 929 | n/a | def test_nonexistent_module(self): |
|---|
| 930 | n/a | try: |
|---|
| 931 | n/a | # assertRaises() clears __traceback__ |
|---|
| 932 | n/a | import nonexistent_xyzzy |
|---|
| 933 | n/a | except ImportError as e: |
|---|
| 934 | n/a | tb = e.__traceback__ |
|---|
| 935 | n/a | else: |
|---|
| 936 | n/a | self.fail("ImportError should have been raised") |
|---|
| 937 | n/a | self.assert_traceback(tb, [__file__]) |
|---|
| 938 | n/a | |
|---|
| 939 | n/a | def test_nonexistent_module_nested(self): |
|---|
| 940 | n/a | self.create_module("foo", "import nonexistent_xyzzy") |
|---|
| 941 | n/a | try: |
|---|
| 942 | n/a | import foo |
|---|
| 943 | n/a | except ImportError as e: |
|---|
| 944 | n/a | tb = e.__traceback__ |
|---|
| 945 | n/a | else: |
|---|
| 946 | n/a | self.fail("ImportError should have been raised") |
|---|
| 947 | n/a | self.assert_traceback(tb, [__file__, 'foo.py']) |
|---|
| 948 | n/a | |
|---|
| 949 | n/a | def test_exec_failure(self): |
|---|
| 950 | n/a | self.create_module("foo", "1/0") |
|---|
| 951 | n/a | try: |
|---|
| 952 | n/a | import foo |
|---|
| 953 | n/a | except ZeroDivisionError as e: |
|---|
| 954 | n/a | tb = e.__traceback__ |
|---|
| 955 | n/a | else: |
|---|
| 956 | n/a | self.fail("ZeroDivisionError should have been raised") |
|---|
| 957 | n/a | self.assert_traceback(tb, [__file__, 'foo.py']) |
|---|
| 958 | n/a | |
|---|
| 959 | n/a | def test_exec_failure_nested(self): |
|---|
| 960 | n/a | self.create_module("foo", "import bar") |
|---|
| 961 | n/a | self.create_module("bar", "1/0") |
|---|
| 962 | n/a | try: |
|---|
| 963 | n/a | import foo |
|---|
| 964 | n/a | except ZeroDivisionError as e: |
|---|
| 965 | n/a | tb = e.__traceback__ |
|---|
| 966 | n/a | else: |
|---|
| 967 | n/a | self.fail("ZeroDivisionError should have been raised") |
|---|
| 968 | n/a | self.assert_traceback(tb, [__file__, 'foo.py', 'bar.py']) |
|---|
| 969 | n/a | |
|---|
| 970 | n/a | # A few more examples from issue #15425 |
|---|
| 971 | n/a | def test_syntax_error(self): |
|---|
| 972 | n/a | self.create_module("foo", "invalid syntax is invalid") |
|---|
| 973 | n/a | try: |
|---|
| 974 | n/a | import foo |
|---|
| 975 | n/a | except SyntaxError as e: |
|---|
| 976 | n/a | tb = e.__traceback__ |
|---|
| 977 | n/a | else: |
|---|
| 978 | n/a | self.fail("SyntaxError should have been raised") |
|---|
| 979 | n/a | self.assert_traceback(tb, [__file__]) |
|---|
| 980 | n/a | |
|---|
| 981 | n/a | def _setup_broken_package(self, parent, child): |
|---|
| 982 | n/a | pkg_name = "_parent_foo" |
|---|
| 983 | n/a | self.addCleanup(unload, pkg_name) |
|---|
| 984 | n/a | pkg_path = os.path.join(TESTFN, pkg_name) |
|---|
| 985 | n/a | os.mkdir(pkg_path) |
|---|
| 986 | n/a | # Touch the __init__.py |
|---|
| 987 | n/a | init_path = os.path.join(pkg_path, '__init__.py') |
|---|
| 988 | n/a | with open(init_path, 'w') as f: |
|---|
| 989 | n/a | f.write(parent) |
|---|
| 990 | n/a | bar_path = os.path.join(pkg_path, 'bar.py') |
|---|
| 991 | n/a | with open(bar_path, 'w') as f: |
|---|
| 992 | n/a | f.write(child) |
|---|
| 993 | n/a | importlib.invalidate_caches() |
|---|
| 994 | n/a | return init_path, bar_path |
|---|
| 995 | n/a | |
|---|
| 996 | n/a | def test_broken_submodule(self): |
|---|
| 997 | n/a | init_path, bar_path = self._setup_broken_package("", "1/0") |
|---|
| 998 | n/a | try: |
|---|
| 999 | n/a | import _parent_foo.bar |
|---|
| 1000 | n/a | except ZeroDivisionError as e: |
|---|
| 1001 | n/a | tb = e.__traceback__ |
|---|
| 1002 | n/a | else: |
|---|
| 1003 | n/a | self.fail("ZeroDivisionError should have been raised") |
|---|
| 1004 | n/a | self.assert_traceback(tb, [__file__, bar_path]) |
|---|
| 1005 | n/a | |
|---|
| 1006 | n/a | def test_broken_from(self): |
|---|
| 1007 | n/a | init_path, bar_path = self._setup_broken_package("", "1/0") |
|---|
| 1008 | n/a | try: |
|---|
| 1009 | n/a | from _parent_foo import bar |
|---|
| 1010 | n/a | except ZeroDivisionError as e: |
|---|
| 1011 | n/a | tb = e.__traceback__ |
|---|
| 1012 | n/a | else: |
|---|
| 1013 | n/a | self.fail("ImportError should have been raised") |
|---|
| 1014 | n/a | self.assert_traceback(tb, [__file__, bar_path]) |
|---|
| 1015 | n/a | |
|---|
| 1016 | n/a | def test_broken_parent(self): |
|---|
| 1017 | n/a | init_path, bar_path = self._setup_broken_package("1/0", "") |
|---|
| 1018 | n/a | try: |
|---|
| 1019 | n/a | import _parent_foo.bar |
|---|
| 1020 | n/a | except ZeroDivisionError as e: |
|---|
| 1021 | n/a | tb = e.__traceback__ |
|---|
| 1022 | n/a | else: |
|---|
| 1023 | n/a | self.fail("ZeroDivisionError should have been raised") |
|---|
| 1024 | n/a | self.assert_traceback(tb, [__file__, init_path]) |
|---|
| 1025 | n/a | |
|---|
| 1026 | n/a | def test_broken_parent_from(self): |
|---|
| 1027 | n/a | init_path, bar_path = self._setup_broken_package("1/0", "") |
|---|
| 1028 | n/a | try: |
|---|
| 1029 | n/a | from _parent_foo import bar |
|---|
| 1030 | n/a | except ZeroDivisionError as e: |
|---|
| 1031 | n/a | tb = e.__traceback__ |
|---|
| 1032 | n/a | else: |
|---|
| 1033 | n/a | self.fail("ZeroDivisionError should have been raised") |
|---|
| 1034 | n/a | self.assert_traceback(tb, [__file__, init_path]) |
|---|
| 1035 | n/a | |
|---|
| 1036 | n/a | @cpython_only |
|---|
| 1037 | n/a | def test_import_bug(self): |
|---|
| 1038 | n/a | # We simulate a bug in importlib and check that it's not stripped |
|---|
| 1039 | n/a | # away from the traceback. |
|---|
| 1040 | n/a | self.create_module("foo", "") |
|---|
| 1041 | n/a | importlib = sys.modules['_frozen_importlib'] |
|---|
| 1042 | n/a | old_load_module = importlib.SourceLoader.load_module |
|---|
| 1043 | n/a | try: |
|---|
| 1044 | n/a | def load_module(*args): |
|---|
| 1045 | n/a | 1/0 |
|---|
| 1046 | n/a | importlib.SourceLoader.load_module = load_module |
|---|
| 1047 | n/a | try: |
|---|
| 1048 | n/a | import foo |
|---|
| 1049 | n/a | except ZeroDivisionError as e: |
|---|
| 1050 | n/a | tb = e.__traceback__ |
|---|
| 1051 | n/a | else: |
|---|
| 1052 | n/a | self.fail("ZeroDivisionError should have been raised") |
|---|
| 1053 | n/a | self.assert_traceback(tb, [__file__, '<frozen importlib', __file__]) |
|---|
| 1054 | n/a | finally: |
|---|
| 1055 | n/a | importlib.SourceLoader.load_module = old_load_module |
|---|
| 1056 | n/a | |
|---|
| 1057 | n/a | |
|---|
| 1058 | n/a | if __name__ == '__main__': |
|---|
| 1059 | n/a | # Test needs to be a package, so we can do relative imports. |
|---|
| 1060 | n/a | unittest.main() |
|---|