Subject: bug#38043: [PATCH 2/6] py-compile: handle filenames with whitespace

From: Mike Frysinger <[email-masked]>
Date: Sun, 6 Feb 2022 01:57:10 -0500

The list of files is put into a string and then split on whitespace.
Fix the way the list of files are passed to the compile script.

* lib/py-compile: Pass files as arguments, not as a string.
* t/py-compile-files.sh: New test.
---
 lib/py-compile        | 14 +++++---------
 t/py-compile-files.sh | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+), 9 deletions(-)
 create mode 100644 t/py-compile-files.sh

diff --git a/lib/py-compile b/lib/py-compile
index 4917cfed008c..0e1df24024e8 100755
--- a/lib/py-compile
+++ b/lib/py-compile
@@ -100,8 +100,7 @@ EOF
   shift
 done
 
-files=$*
-if test -z "$files"; then
+if test $# -eq 0; then
   usage_error "no files given"
 fi
 
@@ -143,10 +142,8 @@ fi
 $PYTHON -c "
 import sys, os, py_compile, $import_lib
 
-files = '''$files'''
-
 sys.stdout.write('Byte-compiling python modules...\n')
-for file in files.split():
+for file in sys.argv[1:]:
     $pathtrans
     $filetrans
     if not os.path.exists(filepath) or not (len(filepath) >= 3
@@ -158,7 +155,7 @@ for file in files.split():
         py_compile.compile(filepath, $import_call(filepath), path)
     else:
         py_compile.compile(filepath, filepath + 'c', path)
-sys.stdout.write('\n')" || exit $?
+sys.stdout.write('\n')" "$@" || exit $?
 
 # this will fail for python < 1.5, but that doesn't matter ...
 $PYTHON -O -c "
@@ -168,9 +165,8 @@ import sys, os, py_compile, $import_lib
 if hasattr(sys, 'pypy_translation_info'):
     sys.exit(0)
 
-files = '''$files'''
 sys.stdout.write('Byte-compiling python modules (optimized versions) ...\n')
-for file in files.split():
+for file in sys.argv[1:]:
     $pathtrans
     $filetrans
     if not os.path.exists(filepath) or not (len(filepath) >= 3
@@ -182,7 +178,7 @@ for file in files.split():
         py_compile.compile(filepath, $import_call(filepath$import_arg2), path)
     else:
         py_compile.compile(filepath, filepath + 'o', path)
-sys.stdout.write('\n')" 2>/dev/null || exit $?
+sys.stdout.write('\n')" "$@" 2>/dev/null || exit $?
 
 # Local Variables:
 # mode: shell-script
diff --git a/t/py-compile-files.sh b/t/py-compile-files.sh
new file mode 100644
index 000000000000..fa2dd3251372
--- /dev/null
+++ b/t/py-compile-files.sh
@@ -0,0 +1,36 @@
+#! /bin/sh
+# Copyright (C) 2022 Free Software Foundation, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2, or (at your option)
+# any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+# Verify 'py-compile' script can handle inputs with spaces, etc...
+
+required=python
+. test-init.sh
+
+cp "$am_scriptdir/py-compile" . \
+  || fatal_ "failed to fetch auxiliary script py-compile"
+
+# Create files that require proper quoting.
+mkdir "dir with spaces"
+touch "nospace.py" "has space.py" "*.py" "dir with spaces/|.py"
+
+./py-compile "nospace.py" "has space.py" "*.py" "dir with spaces/|.py"
+
+py_installed "nospace.pyc"
+py_installed "has space.pyc"
+py_installed "*.pyc"
+py_installed "dir with spaces/|.pyc"
+
+:
-- 
2.34.1