From 97b821af125231ed02caee07819374bedb6eab20 Mon Sep 17 00:00:00 2001 From: Brendan Molloy Date: Wed, 6 Jan 2016 19:31:35 +1100 Subject: [PATCH] Check bytecode file actually exists and tests Should solve issue 20397, where using the --record argument results in files that failed to generate bytecode files are added to the record file nonetheless. --- Lib/distutils/command/install_lib.py | 12 ++++++++---- Lib/distutils/tests/test_install_lib.py | 8 ++++++-- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Lib/distutils/command/install_lib.py b/Lib/distutils/command/install_lib.py index 043e8b6..dc74a34 100644 --- a/Lib/distutils/command/install_lib.py +++ b/Lib/distutils/command/install_lib.py @@ -168,10 +168,14 @@ def _bytecode_filenames(self, py_filenames): ext = os.path.splitext(os.path.normcase(py_file))[1] if ext != PYTHON_SOURCE_EXTENSION: continue - if self.compile: - bytecode_files.append(py_file + "c") - if self.optimize > 0: - bytecode_files.append(py_file + "o") + + pyc_file = py_file + "c" + if self.compile and os.path.isfile(pyc_file): + bytecode_files.append(pyc_file) + + pyo_file = py_file + "o" + if self.optimize > 0 and os.path.isfile(pyo_file): + bytecode_files.append(pyo_file) return bytecode_files diff --git a/Lib/distutils/tests/test_install_lib.py b/Lib/distutils/tests/test_install_lib.py index 0defbd6..afee308 100644 --- a/Lib/distutils/tests/test_install_lib.py +++ b/Lib/distutils/tests/test_install_lib.py @@ -64,8 +64,12 @@ def test_get_outputs(self): cmd.distribution.packages = [pkg_dir] cmd.distribution.script_name = 'setup.py' - # get_output should return 4 elements - self.assertGreaterEqual(len(cmd.get_outputs()), 2) + # Create rubbish, uncompilable file + f = os.path.join(pkg_dir, 'rubbish.py') + self.write_file(f, 'rubbish()') + + # get_output should return 3 elements + self.assertEqual(len(cmd.get_outputs()), 3) def test_get_inputs(self): pkg_dir, dist = self.create_dist()