Skip to content

gh-133545: Also quote arguments containing &<>^| on Windows #134544

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
@@ -618,7 +618,7 @@ def list2cmdline(seq):
if result:
result.append(' ')

needquote = (" " in arg) or ("\t" in arg) or not arg
needquote = not arg or not set(" \t&<>^|").isdisjoint(arg)
if needquote:
result.append('"')

9 changes: 9 additions & 0 deletions Lib/test/test_subprocess.py
Original file line number Diff line number Diff line change
@@ -3560,6 +3560,15 @@ def test_invalid_args(self):
"import sys; sys.exit(47)"],
preexec_fn=lambda: 1)

def test_args_quoting(self):
with tempfile.NamedTemporaryFile(suffix=".bat", delete_on_close=False) as f:
f.write(b"echo %*\n")
f.close()
p = subprocess.run([f.file.name, "a b", "", "c<>d", "e"],
capture_output=True)
self.assertEqual(p.returncode, 0, p.stderr)
self.assertEndsWith(p.stdout.strip(), b'"a b" "" "c<>d" e')

@support.cpython_only
def test_issue31471(self):
# There shouldn't be an assertion failure in Popen() in case the env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
In :mod:`subprocess` on Windows, in addition to space, tab, and empty string, quote arguments containing
``&<>^|``. Patch by John Keith Hohm.
Loading
Oops, something went wrong.

Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant