diff --git a/Lib/distutils/tests/test_bdist_rpm.py b/Lib/distutils/tests/test_bdist_rpm.py --- a/Lib/distutils/tests/test_bdist_rpm.py +++ b/Lib/distutils/tests/test_bdist_rpm.py @@ -38,6 +38,8 @@ sys.argv[:] = self.old_sys_argv[1] super(BuildRpmTestCase, self).tearDown() + @unittest.skipIf(sys.dont_write_bytecode, + "bdist_rpm fails when not writing bytecode") def test_quiet(self): # XXX I am unable yet to make this test work without @@ -78,6 +80,8 @@ dist_created = os.listdir(os.path.join(pkg_dir, 'dist')) self.assertTrue('foo-0.1-1.noarch.rpm' in dist_created) + @unittest.skipIf(sys.dont_write_bytecode, + "bdist_rpm fails when not writing bytecode") def test_no_optimize_flag(self): # XXX I am unable yet to make this test work without diff --git a/Lib/distutils/tests/test_build_py.py b/Lib/distutils/tests/test_build_py.py --- a/Lib/distutils/tests/test_build_py.py +++ b/Lib/distutils/tests/test_build_py.py @@ -58,7 +58,10 @@ pkgdest = os.path.join(destination, "pkg") files = os.listdir(pkgdest) self.assertTrue("__init__.py" in files) - self.assertTrue("__init__.pyc" in files) + if sys.dont_write_bytecode: + self.assertFalse("__init__.pyc" in files) + else: + self.assertTrue("__init__.pyc" in files) self.assertTrue("README.txt" in files) def test_empty_package_dir (self): diff --git a/Lib/importlib/test/source/test_file_loader.py b/Lib/importlib/test/source/test_file_loader.py --- a/Lib/importlib/test/source/test_file_loader.py +++ b/Lib/importlib/test/source/test_file_loader.py @@ -126,7 +126,8 @@ finally: os.unlink(file_path) pycache = os.path.dirname(imp.cache_from_source(file_path)) - shutil.rmtree(pycache) + if os.path.exists(pycache): + shutil.rmtree(pycache) class BadBytecodeTest(unittest.TestCase): diff --git a/Lib/test/test_imp.py b/Lib/test/test_imp.py --- a/Lib/test/test_imp.py +++ b/Lib/test/test_imp.py @@ -153,9 +153,11 @@ mod = imp.load_source(temp_mod_name, temp_mod_name + '.py') self.assertEqual(mod.a, 1) - mod = imp.load_compiled( - temp_mod_name, imp.cache_from_source(temp_mod_name + '.py')) - self.assertEqual(mod.a, 1) + if not sys.dont_write_bytecode: + mod = imp.load_compiled( + temp_mod_name, + imp.cache_from_source(temp_mod_name + '.py')) + self.assertEqual(mod.a, 1) if not os.path.exists(test_package_name): os.mkdir(test_package_name) diff --git a/Lib/test/test_import.py b/Lib/test/test_import.py --- a/Lib/test/test_import.py +++ b/Lib/test/test_import.py @@ -93,6 +93,8 @@ @unittest.skipUnless(os.name == 'posix', "test meaningful only on posix systems") + @unittest.skipIf(sys.dont_write_bytecode, + "test meaningful only when writing bytecode") 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. @@ -235,6 +237,8 @@ remove_files(TESTFN) unload(TESTFN) + @unittest.skipIf(sys.dont_write_bytecode, + "test meaningful only when writing bytecode") def test_file_to_source(self): # check if __file__ points to the source file where available source = TESTFN + ".py" @@ -524,6 +528,8 @@ del sys.path[0] self._clean() + @unittest.skipIf(sys.dont_write_bytecode, + "test meaningful only when writing bytecode") def test_import_pyc_path(self): self.assertFalse(os.path.exists('__pycache__')) __import__(TESTFN) @@ -534,6 +540,8 @@ @unittest.skipUnless(os.name == 'posix', "test meaningful only on posix systems") + @unittest.skipIf(sys.dont_write_bytecode, + "test meaningful only when writing bytecode") def test_unwritable_directory(self): # When the umask causes the new __pycache__ directory to be # unwritable, the import still succeeds but no .pyc file is written. @@ -543,6 +551,8 @@ self.assertFalse(os.path.exists(os.path.join( '__pycache__', '{}.{}.pyc'.format(TESTFN, self.tag)))) + @unittest.skipIf(sys.dont_write_bytecode, + "test meaningful only when writing bytecode") def test_missing_source(self): # With PEP 3147 cache layout, removing the source but leaving the pyc # file does not satisfy the import. @@ -553,6 +563,8 @@ forget(TESTFN) self.assertRaises(ImportError, __import__, TESTFN) + @unittest.skipIf(sys.dont_write_bytecode, + "test meaningful only when writing bytecode") def test_missing_source_legacy(self): # Like test_missing_source() except that for backward compatibility, # when the pyc file lives where the py file would have been (and named @@ -573,6 +585,8 @@ pyc_file = imp.cache_from_source(TESTFN + '.py') self.assertEqual(m.__cached__, os.path.join(os.curdir, pyc_file)) + @unittest.skipIf(sys.dont_write_bytecode, + "test meaningful only when writing bytecode") def test___cached___legacy_pyc(self): # Like test___cached__() except that for backward compatibility, # when the pyc file lives where the py file would have been (and named @@ -587,6 +601,8 @@ self.assertEqual(m.__cached__, os.path.join(os.curdir, os.path.relpath(pyc_file))) + @unittest.skipIf(sys.dont_write_bytecode, + "test meaningful only when writing bytecode") def test_package___cached__(self): # Like test___cached__ but for packages. def cleanup(): diff --git a/Lib/test/test_runpy.py b/Lib/test/test_runpy.py --- a/Lib/test/test_runpy.py +++ b/Lib/test/test_runpy.py @@ -173,13 +173,14 @@ del d1 # Ensure __loader__ entry doesn't keep file open __import__(mod_name) os.remove(mod_fname) - make_legacy_pyc(mod_fname) - unload(mod_name) # In case loader caches paths - if verbose: print("Running from compiled:", mod_name) - d2 = run_module(mod_name) # Read from bytecode - self.assertIn("x", d2) - self.assertEqual(d2["x"], 1) - del d2 # Ensure __loader__ entry doesn't keep file open + if not sys.dont_write_bytecode: + make_legacy_pyc(mod_fname) + unload(mod_name) # In case loader caches paths + if verbose: print("Running from compiled:", mod_name) + d2 = run_module(mod_name) # Read from bytecode + self.assertIn("x", d2) + self.assertEqual(d2["x"], 1) + del d2 # Ensure __loader__ entry doesn't keep file open finally: self._del_pkg(pkg_dir, depth, mod_name) if verbose: print("Module executed successfully") @@ -197,13 +198,14 @@ del d1 # Ensure __loader__ entry doesn't keep file open __import__(mod_name) os.remove(mod_fname) - make_legacy_pyc(mod_fname) - unload(mod_name) # In case loader caches paths - if verbose: print("Running from compiled:", pkg_name) - d2 = run_module(pkg_name) # Read from bytecode - self.assertIn("x", d2) - self.assertTrue(d2["x"] == 1) - del d2 # Ensure __loader__ entry doesn't keep file open + if not sys.dont_write_bytecode: + make_legacy_pyc(mod_fname) + unload(mod_name) # In case loader caches paths + if verbose: print("Running from compiled:", pkg_name) + d2 = run_module(pkg_name) # Read from bytecode + self.assertIn("x", d2) + self.assertTrue(d2["x"] == 1) + del d2 # Ensure __loader__ entry doesn't keep file open finally: self._del_pkg(pkg_dir, depth, pkg_name) if verbose: print("Package executed successfully") @@ -253,15 +255,16 @@ del d1 # Ensure __loader__ entry doesn't keep file open __import__(mod_name) os.remove(mod_fname) - make_legacy_pyc(mod_fname) - unload(mod_name) # In case the loader caches paths - if verbose: print("Running from compiled:", mod_name) - d2 = run_module(mod_name, run_name=run_name) # Read from bytecode - self.assertIn("__package__", d2) - self.assertTrue(d2["__package__"] == pkg_name) - self.assertIn("sibling", d2) - self.assertIn("nephew", d2) - del d2 # Ensure __loader__ entry doesn't keep file open + if not sys.dont_write_bytecode: + make_legacy_pyc(mod_fname) + unload(mod_name) # In case the loader caches paths + if verbose: print("Running from compiled:", mod_name) + d2 = run_module(mod_name, run_name=run_name) # Read from bytecode + self.assertIn("__package__", d2) + self.assertTrue(d2["__package__"] == pkg_name) + self.assertIn("sibling", d2) + self.assertIn("nephew", d2) + del d2 # Ensure __loader__ entry doesn't keep file open finally: self._del_pkg(pkg_dir, depth, mod_name) if verbose: print("Module executed successfully")