Skip to content

Commit 84e5946

Browse files
committed
allows path-like objects in program arguments in Windows
1 parent fd512d7 commit 84e5946

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

Lib/subprocess.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ def list2cmdline(seq):
515515
# "Parsing C++ Command-Line Arguments"
516516
result = []
517517
needquote = False
518-
for arg in seq:
518+
for arg in map(os.fspath, seq):
519519
bs_buf = []
520520

521521
# Add a space to separate this argument from the others

Lib/test/test_subprocess.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,18 @@ def test_cwd_with_pathlike(self):
363363
temp_dir = self._normalize_cwd(temp_dir)
364364
self._assert_cwd(temp_dir, sys.executable, cwd=FakePath(temp_dir))
365365

366+
def test_args_with_pathlike(self):
367+
temp_dir = tempfile.gettempdir()
368+
temp_dir = self._normalize_cwd(temp_dir)
369+
p = subprocess.Popen([FakePath(sys.executable), "-c",
370+
"import sys; sys.exit(47)"])
371+
self.assertEqual(p.wait(), 47)
372+
p = subprocess.Popen([sys.executable, "-c",
373+
"import sys; print(sys.argv[1], end='')",
374+
FakePath(temp_dir)], stdout=subprocess.PIPE)
375+
with p:
376+
self.assertEqual(os.fsdecode(p.stdout.read()), temp_dir)
377+
366378
@unittest.skipIf(mswindows, "pending resolution of issue #15533")
367379
def test_cwd_with_relative_arg(self):
368380
# Check that Popen looks for args[0] relative to cwd if args[0]
@@ -1048,6 +1060,8 @@ def test_no_leaking(self):
10481060
def test_list2cmdline(self):
10491061
self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
10501062
'"a b c" d e')
1063+
self.assertEqual(subprocess.list2cmdline(['a b c', 'd', FakePath('e')]),
1064+
'"a b c" d e')
10511065
self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
10521066
'ab\\"c \\ d')
10531067
self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']),
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Currently, the `subprocess.Popen` function allows for path-like objects in
2+
the argument list for POSIX but not in Windows. This PR makes Windows'
3+
`subprocess.Popen` accept path-like objects in the argument list.

0 commit comments

Comments
 (0)