From 84e59460d487ce9eec66fd9a4a9c2f45eb1a57ee Mon Sep 17 00:00:00 2001 From: guoci Date: Fri, 14 Sep 2018 18:16:12 -0400 Subject: [PATCH] allows path-like objects in program arguments in Windows --- Lib/subprocess.py | 2 +- Lib/test/test_subprocess.py | 14 ++++++++++++++ .../2018-09-15-15-32-04.bpo-34699.Q7XUTY.rst | 3 +++ 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2018-09-15-15-32-04.bpo-34699.Q7XUTY.rst diff --git a/Lib/subprocess.py b/Lib/subprocess.py index 5db6f0cc24ba33..c25a960496bf30 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -515,7 +515,7 @@ def list2cmdline(seq): # "Parsing C++ Command-Line Arguments" result = [] needquote = False - for arg in seq: + for arg in map(os.fspath, seq): bs_buf = [] # Add a space to separate this argument from the others diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py index b0b6b06e92759e..facec92377e385 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -363,6 +363,18 @@ def test_cwd_with_pathlike(self): temp_dir = self._normalize_cwd(temp_dir) self._assert_cwd(temp_dir, sys.executable, cwd=FakePath(temp_dir)) + def test_args_with_pathlike(self): + temp_dir = tempfile.gettempdir() + temp_dir = self._normalize_cwd(temp_dir) + p = subprocess.Popen([FakePath(sys.executable), "-c", + "import sys; sys.exit(47)"]) + self.assertEqual(p.wait(), 47) + p = subprocess.Popen([sys.executable, "-c", + "import sys; print(sys.argv[1], end='')", + FakePath(temp_dir)], stdout=subprocess.PIPE) + with p: + self.assertEqual(os.fsdecode(p.stdout.read()), temp_dir) + @unittest.skipIf(mswindows, "pending resolution of issue #15533") def test_cwd_with_relative_arg(self): # Check that Popen looks for args[0] relative to cwd if args[0] @@ -1048,6 +1060,8 @@ def test_no_leaking(self): def test_list2cmdline(self): self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']), '"a b c" d e') + self.assertEqual(subprocess.list2cmdline(['a b c', 'd', FakePath('e')]), + '"a b c" d e') self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']), 'ab\\"c \\ d') self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']), diff --git a/Misc/NEWS.d/next/Library/2018-09-15-15-32-04.bpo-34699.Q7XUTY.rst b/Misc/NEWS.d/next/Library/2018-09-15-15-32-04.bpo-34699.Q7XUTY.rst new file mode 100644 index 00000000000000..8b02a9e150890c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-09-15-15-32-04.bpo-34699.Q7XUTY.rst @@ -0,0 +1,3 @@ +Currently, the `subprocess.Popen` function allows for path-like objects in +the argument list for POSIX but not in Windows. This PR makes Windows' +`subprocess.Popen` accept path-like objects in the argument list.