Skip to content

bpo-34699: allow path-like objects in program arguments in Windows #9336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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']),
Expand Down
Original file line number Diff line number Diff line change
@@ -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.