]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-114272: Fix or skip tests that fail due to spaces in paths (GH-114451)
authorSteve Dower <steve.dower@python.org>
Thu, 25 Jan 2024 00:38:34 +0000 (00:38 +0000)
committerGitHub <noreply@github.com>
Thu, 25 Jan 2024 00:38:34 +0000 (00:38 +0000)
Lib/test/test_asyncio/test_subprocess.py
Lib/test/test_launcher.py
Lib/test/test_os.py
Lib/test/test_webbrowser.py

index 859d2932c33fedec4a64f13166d36f3ef39ebe74..808b21c66175511c58c49d16b8d266d31939ca47 100644 (file)
@@ -207,7 +207,7 @@ class SubprocessMixin:
 
     def test_kill_issue43884(self):
         if sys.platform == 'win32':
-            blocking_shell_command = f'{sys.executable} -c "import time; time.sleep(2)"'
+            blocking_shell_command = f'"{sys.executable}" -c "import time; time.sleep(2)"'
         else:
             blocking_shell_command = 'sleep 1; sleep 1'
         creationflags = 0
@@ -745,7 +745,10 @@ class SubprocessMixin:
 
     def test_create_subprocess_env_shell(self) -> None:
         async def main() -> None:
-            cmd = f'''{sys.executable} -c "import os, sys; sys.stdout.write(os.getenv('FOO'))"'''
+            executable = sys.executable
+            if sys.platform == "win32":
+                executable = f'"{executable}"'
+            cmd = f'''{executable} -c "import os, sys; sys.stdout.write(os.getenv('FOO'))"'''
             env = os.environ.copy()
             env["FOO"] = "bar"
             proc = await asyncio.create_subprocess_shell(
index 3da6173cfd3f1349d5a15c5401f40a1671be35aa..2528a51240fbf7646ca0176b3456567a5b894055 100644 (file)
@@ -90,6 +90,12 @@ TEST_PY_COMMANDS = "\n".join([
     "test-command=TEST_EXE.exe",
 ])
 
+
+def quote(s):
+    s = str(s)
+    return f'"{s}"' if " " in s else s
+
+
 def create_registry_data(root, data):
     def _create_registry_data(root, key, value):
         if isinstance(value, dict):
@@ -542,10 +548,10 @@ class TestLauncher(unittest.TestCase, RunPyMixin):
             data1 = self.run_py([], env={**env, "PY_PYTHON": "PythonTestSuite/3"})
             data2 = self.run_py(["-V:PythonTestSuite/3"], env={**env, "PY_PYTHON": "PythonTestSuite/3"})
         # Compare stdout, because stderr goes via ascii
-        self.assertEqual(data1["stdout"].strip(), str(venv_exe))
+        self.assertEqual(data1["stdout"].strip(), quote(venv_exe))
         self.assertEqual(data1["SearchInfo.lowPriorityTag"], "True")
         # Ensure passing the argument doesn't trigger the same behaviour
-        self.assertNotEqual(data2["stdout"].strip(), str(venv_exe))
+        self.assertNotEqual(data2["stdout"].strip(), quote(venv_exe))
         self.assertNotEqual(data2["SearchInfo.lowPriorityTag"], "True")
 
     def test_py_shebang(self):
@@ -554,7 +560,7 @@ class TestLauncher(unittest.TestCase, RunPyMixin):
                 data = self.run_py([script, "-postarg"])
         self.assertEqual("PythonTestSuite", data["SearchInfo.company"])
         self.assertEqual("3.100", data["SearchInfo.tag"])
-        self.assertEqual(f"X.Y.exe -prearg {script} -postarg", data["stdout"].strip())
+        self.assertEqual(f"X.Y.exe -prearg {quote(script)} -postarg", data["stdout"].strip())
 
     def test_python_shebang(self):
         with self.py_ini(TEST_PY_DEFAULTS):
@@ -562,7 +568,7 @@ class TestLauncher(unittest.TestCase, RunPyMixin):
                 data = self.run_py([script, "-postarg"])
         self.assertEqual("PythonTestSuite", data["SearchInfo.company"])
         self.assertEqual("3.100", data["SearchInfo.tag"])
-        self.assertEqual(f"X.Y.exe -prearg {script} -postarg", data["stdout"].strip())
+        self.assertEqual(f"X.Y.exe -prearg {quote(script)} -postarg", data["stdout"].strip())
 
     def test_py2_shebang(self):
         with self.py_ini(TEST_PY_DEFAULTS):
@@ -570,7 +576,8 @@ class TestLauncher(unittest.TestCase, RunPyMixin):
                 data = self.run_py([script, "-postarg"])
         self.assertEqual("PythonTestSuite", data["SearchInfo.company"])
         self.assertEqual("3.100-32", data["SearchInfo.tag"])
-        self.assertEqual(f"X.Y-32.exe -prearg {script} -postarg", data["stdout"].strip())
+        self.assertEqual(f"X.Y-32.exe -prearg {quote(script)} -postarg",
+                         data["stdout"].strip())
 
     def test_py3_shebang(self):
         with self.py_ini(TEST_PY_DEFAULTS):
@@ -578,7 +585,8 @@ class TestLauncher(unittest.TestCase, RunPyMixin):
                 data = self.run_py([script, "-postarg"])
         self.assertEqual("PythonTestSuite", data["SearchInfo.company"])
         self.assertEqual("3.100-arm64", data["SearchInfo.tag"])
-        self.assertEqual(f"X.Y-arm64.exe -X fake_arg_for_test -prearg {script} -postarg", data["stdout"].strip())
+        self.assertEqual(f"X.Y-arm64.exe -X fake_arg_for_test -prearg {quote(script)} -postarg",
+                         data["stdout"].strip())
 
     def test_py_shebang_nl(self):
         with self.py_ini(TEST_PY_DEFAULTS):
@@ -586,7 +594,8 @@ class TestLauncher(unittest.TestCase, RunPyMixin):
                 data = self.run_py([script, "-postarg"])
         self.assertEqual("PythonTestSuite", data["SearchInfo.company"])
         self.assertEqual("3.100", data["SearchInfo.tag"])
-        self.assertEqual(f"X.Y.exe -prearg {script} -postarg", data["stdout"].strip())
+        self.assertEqual(f"X.Y.exe -prearg {quote(script)} -postarg",
+                         data["stdout"].strip())
 
     def test_py2_shebang_nl(self):
         with self.py_ini(TEST_PY_DEFAULTS):
@@ -594,7 +603,8 @@ class TestLauncher(unittest.TestCase, RunPyMixin):
                 data = self.run_py([script, "-postarg"])
         self.assertEqual("PythonTestSuite", data["SearchInfo.company"])
         self.assertEqual("3.100-32", data["SearchInfo.tag"])
-        self.assertEqual(f"X.Y-32.exe -prearg {script} -postarg", data["stdout"].strip())
+        self.assertEqual(f"X.Y-32.exe -prearg {quote(script)} -postarg",
+                         data["stdout"].strip())
 
     def test_py3_shebang_nl(self):
         with self.py_ini(TEST_PY_DEFAULTS):
@@ -602,7 +612,8 @@ class TestLauncher(unittest.TestCase, RunPyMixin):
                 data = self.run_py([script, "-postarg"])
         self.assertEqual("PythonTestSuite", data["SearchInfo.company"])
         self.assertEqual("3.100-arm64", data["SearchInfo.tag"])
-        self.assertEqual(f"X.Y-arm64.exe -X fake_arg_for_test -prearg {script} -postarg", data["stdout"].strip())
+        self.assertEqual(f"X.Y-arm64.exe -X fake_arg_for_test -prearg {quote(script)} -postarg",
+                         data["stdout"].strip())
 
     def test_py_shebang_short_argv0(self):
         with self.py_ini(TEST_PY_DEFAULTS):
@@ -630,7 +641,8 @@ class TestLauncher(unittest.TestCase, RunPyMixin):
                     [script, "-postarg"],
                     env={"PATH": f"{exe.parent};{os.getenv('PATH')}"},
                 )
-        self.assertEqual(f"{exe} -prearg {script} -postarg", data["stdout"].strip())
+        self.assertEqual(f"{quote(exe)} -prearg {quote(script)} -postarg",
+                         data["stdout"].strip())
 
     def test_search_path_exe(self):
         # Leave the .exe on the name to ensure we don't add it a second time
@@ -643,7 +655,8 @@ class TestLauncher(unittest.TestCase, RunPyMixin):
                     [script, "-postarg"],
                     env={"PATH": f"{exe.parent};{os.getenv('PATH')}"},
                 )
-        self.assertEqual(f"{exe} -prearg {script} -postarg", data["stdout"].strip())
+        self.assertEqual(f"{quote(exe)} -prearg {quote(script)} -postarg",
+                         data["stdout"].strip())
 
     def test_recursive_search_path(self):
         stem = self.get_py_exe().stem
@@ -654,7 +667,7 @@ class TestLauncher(unittest.TestCase, RunPyMixin):
                     env={"PATH": f"{self.get_py_exe().parent};{os.getenv('PATH')}"},
                 )
         # The recursive search is ignored and we get normal "py" behavior
-        self.assertEqual(f"X.Y.exe {script}", data["stdout"].strip())
+        self.assertEqual(f"X.Y.exe {quote(script)}", data["stdout"].strip())
 
     def test_install(self):
         data = self.run_py(["-V:3.10"], env={"PYLAUNCHER_ALWAYS_INSTALL": "1"}, expect_returncode=111)
@@ -674,7 +687,7 @@ class TestLauncher(unittest.TestCase, RunPyMixin):
         with self.script("#! C:/some_random_app -witharg") as script:
             data = self.run_py([script])
         self.assertEqual(
-            f"C:\\some_random_app -witharg {script}",
+            f"C:\\some_random_app -witharg {quote(script)}",
             data["stdout"].strip(),
         )
 
@@ -682,7 +695,7 @@ class TestLauncher(unittest.TestCase, RunPyMixin):
         with self.script("#! ..\\some_random_app -witharg") as script:
             data = self.run_py([script])
         self.assertEqual(
-            f"{script.parent.parent}\\some_random_app -witharg {script}",
+            f"{quote(script.parent.parent / 'some_random_app')} -witharg {quote(script)}",
             data["stdout"].strip(),
         )
 
@@ -690,14 +703,14 @@ class TestLauncher(unittest.TestCase, RunPyMixin):
         with self.script('#! "some random app" -witharg') as script:
             data = self.run_py([script])
         self.assertEqual(
-            f'"{script.parent}\\some random app" -witharg {script}',
+            f"{quote(script.parent / 'some random app')} -witharg {quote(script)}",
             data["stdout"].strip(),
         )
 
         with self.script('#! some" random "app -witharg') as script:
             data = self.run_py([script])
         self.assertEqual(
-            f'"{script.parent}\\some random app" -witharg {script}',
+            f"{quote(script.parent / 'some random app')} -witharg {quote(script)}",
             data["stdout"].strip(),
         )
 
@@ -705,7 +718,7 @@ class TestLauncher(unittest.TestCase, RunPyMixin):
         with self.script('#! some\\" random "app -witharg') as script:
             data = self.run_py([script])
         self.assertEqual(
-            f'"{script.parent}\\some\\ random app" -witharg {script}',
+            f"{quote(script.parent / 'some/ random app')} -witharg {quote(script)}",
             data["stdout"].strip(),
         )
 
@@ -714,7 +727,7 @@ class TestLauncher(unittest.TestCase, RunPyMixin):
             with self.script('#! test-command arg1') as script:
                 data = self.run_py([script])
         self.assertEqual(
-            f"TEST_EXE.exe arg1 {script}",
+            f"TEST_EXE.exe arg1 {quote(script)}",
             data["stdout"].strip(),
         )
 
@@ -723,7 +736,7 @@ class TestLauncher(unittest.TestCase, RunPyMixin):
             data = self.run_py([script])
         expect = script.parent / "/usr/bin/not-python"
         self.assertEqual(
-            f"{expect} arg1 {script}",
+            f"{quote(expect)} arg1 {quote(script)}",
             data["stdout"].strip(),
         )
 
@@ -746,8 +759,8 @@ class TestLauncher(unittest.TestCase, RunPyMixin):
 
             with self.script(f'#! /usr/bin/env {stem} arg1') as script:
                 data = self.run_py([script], env=env)
-            self.assertEqual(data["stdout"].strip(), f"{venv_exe} arg1 {script}")
+            self.assertEqual(data["stdout"].strip(), f"{quote(venv_exe)} arg1 {quote(script)}")
 
             with self.script(f'#! /usr/bin/env {exe.stem} arg1') as script:
                 data = self.run_py([script], env=env)
-            self.assertEqual(data["stdout"].strip(), f"{exe} arg1 {script}")
+            self.assertEqual(data["stdout"].strip(), f"{quote(exe)} arg1 {quote(script)}")
index 98b30d2108a1a12c0e398e96d658a33ff5e9085b..ed1f304c6c8cac747b04bf17cf01b03f76931be6 100644 (file)
@@ -4596,8 +4596,11 @@ class FDInheritanceTests(unittest.TestCase):
         with open(filename, "w") as fp:
             print(code, file=fp, end="")
 
-        cmd = [sys.executable, filename]
-        exitcode = os.spawnl(os.P_WAIT, cmd[0], *cmd)
+        executable = sys.executable
+        cmd = [executable, filename]
+        if os.name == "nt" and " " in cmd[0]:
+            cmd[0] = f'"{cmd[0]}"'
+        exitcode = os.spawnl(os.P_WAIT, executable, *cmd)
         self.assertEqual(exitcode, 0)
 
 
index ca481c57c3d97241f801a27dcbbada4c517b2cc7..8c074cb28a87e38acd586be188f87bdf945e96aa 100644 (file)
@@ -307,6 +307,7 @@ class ImportTest(unittest.TestCase):
             webbrowser.get('fakebrowser')
         self.assertIsNotNone(webbrowser._tryorder)
 
+    @unittest.skipIf(" " in sys.executable, "test assumes no space in path (GH-114452)")
     def test_synthesize(self):
         webbrowser = import_helper.import_fresh_module('webbrowser')
         name = os.path.basename(sys.executable).lower()