diff options
Diffstat (limited to 'Lib/test/test_import.py')
-rw-r--r-- | Lib/test/test_import.py | 40 |
1 files changed, 27 insertions, 13 deletions
diff --git a/Lib/test/test_import.py b/Lib/test/test_import.py index 48443ea..bd2da72 100644 --- a/Lib/test/test_import.py +++ b/Lib/test/test_import.py @@ -2,6 +2,7 @@ import builtins import imp from importlib.test.import_ import test_relative_imports from importlib.test.import_ import util as importlib_util +import importlib import marshal import os import platform @@ -16,7 +17,7 @@ import errno from test.support import ( EnvironmentVarGuard, TESTFN, check_warnings, forget, is_jython, make_legacy_pyc, rmtree, run_unittest, swap_attr, swap_item, temp_umask, - unlink, unload) + unlink, unload, create_empty_file) from test import script_helper @@ -34,6 +35,7 @@ class ImportTests(unittest.TestCase): def setUp(self): remove_files(TESTFN) + importlib.invalidate_caches() def tearDown(self): unload(TESTFN) @@ -98,25 +100,24 @@ class ImportTests(unittest.TestCase): @unittest.skipUnless(os.name == 'posix', "test meaningful only on posix systems") - def test_execute_bit_not_copied(self): - # Issue 6070: under posix .pyc files got their execute bit set if - # the .py file had the execute bit set, but they aren't executable. - with temp_umask(0o022): + def test_creation_mode(self): + mask = 0o022 + with temp_umask(mask): sys.path.insert(0, os.curdir) try: fname = TESTFN + os.extsep + "py" - open(fname, 'w').close() - os.chmod(fname, (stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH | - stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)) + create_empty_file(fname) fn = imp.cache_from_source(fname) unlink(fn) + importlib.invalidate_caches() __import__(TESTFN) if not os.path.exists(fn): self.fail("__import__ did not result in creation of " "either a .pyc or .pyo file") s = os.stat(fn) - self.assertEqual(stat.S_IMODE(s.st_mode), - stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH) + # Check that the umask is respected, and the executable bits + # aren't set. + self.assertEqual(stat.S_IMODE(s.st_mode), 0o666 & ~mask) finally: del sys.path[0] remove_files(TESTFN) @@ -262,6 +263,7 @@ class ImportTests(unittest.TestCase): os.remove(source) del sys.modules[TESTFN] make_legacy_pyc(source) + importlib.invalidate_caches() mod = __import__(TESTFN) base, ext = os.path.splitext(mod.__file__) self.assertIn(ext, ('.pyc', '.pyo')) @@ -297,8 +299,6 @@ class ImportTests(unittest.TestCase): self.skipTest('path is not encodable to {}'.format(encoding)) with self.assertRaises(ImportError) as c: __import__(path) - self.assertEqual("Import by filename is not supported.", - c.exception.args[0]) def test_import_in_del_does_not_crash(self): # Issue 4236 @@ -362,6 +362,7 @@ func_filename = func.__code__.co_filename with open(self.file_name, "w") as f: f.write(self.module_source) sys.path.insert(0, self.dir_name) + importlib.invalidate_caches() def tearDown(self): sys.path[:] = self.sys_path @@ -409,7 +410,7 @@ func_filename = func.__code__.co_filename def test_foreign_code(self): py_compile.compile(self.file_name) with open(self.compiled_name, "rb") as f: - header = f.read(8) + header = f.read(12) code = marshal.load(f) constants = list(code.co_consts) foreign_code = test_main.__code__ @@ -556,6 +557,7 @@ class PycacheTests(unittest.TestCase): with open(self.source, 'w') as fp: print('# This is a test file written by test_import.py', file=fp) sys.path.insert(0, os.curdir) + importlib.invalidate_caches() def tearDown(self): assert sys.path[0] == os.curdir, 'Unexpected sys.path[0]' @@ -603,6 +605,7 @@ class PycacheTests(unittest.TestCase): pyc_file = make_legacy_pyc(self.source) os.remove(self.source) unload(TESTFN) + importlib.invalidate_caches() m = __import__(TESTFN) self.assertEqual(m.__file__, os.path.join(os.curdir, os.path.relpath(pyc_file))) @@ -623,6 +626,7 @@ class PycacheTests(unittest.TestCase): pyc_file = make_legacy_pyc(self.source) os.remove(self.source) unload(TESTFN) + importlib.invalidate_caches() m = __import__(TESTFN) self.assertEqual(m.__cached__, os.path.join(os.curdir, os.path.relpath(pyc_file))) @@ -673,6 +677,16 @@ class PycacheTests(unittest.TestCase): self.assertEqual(sys.modules['pep3147.foo'].__cached__, os.path.join(os.curdir, foo_pyc)) + def test_recompute_pyc_same_second(self): + # Even when the source file doesn't change timestamp, a change in + # source size is enough to trigger recomputation of the pyc file. + __import__(TESTFN) + unload(TESTFN) + with open(self.source, 'a') as fp: + print("x = 5", file=fp) + m = __import__(TESTFN) + self.assertEqual(m.x, 5) + class RelativeImportFromImportlibTests(test_relative_imports.RelativeImports): |