Skip to content

Commit 51b00f6

Browse files
committed
Also quote arguments containing :&<>^| on Windows
1 parent 4eacf38 commit 51b00f6

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

Lib/subprocess.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ def list2cmdline(seq):
618618
if result:
619619
result.append(' ')
620620

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

Lib/test/test_subprocess.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3560,6 +3560,15 @@ def test_invalid_args(self):
35603560
"import sys; sys.exit(47)"],
35613561
preexec_fn=lambda: 1)
35623562

3563+
def test_args_quoting(self):
3564+
with tempfile.NamedTemporaryFile(suffix=".bat", delete_on_close=False) as f:
3565+
f.write(b"echo %*\n")
3566+
f.close()
3567+
p = subprocess.run([f.file.name, "a b", "", "c<>d", "e"],
3568+
capture_output=True)
3569+
self.assertEqual(p.returncode, 0, p.stderr)
3570+
self.assertEndsWith(p.stdout.strip(), b'"a b" "" "c<>d" e')
3571+
35633572
@support.cpython_only
35643573
def test_issue31471(self):
35653574
# There shouldn't be an assertion failure in Popen() in case the env
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
In addition to space, tab, and empty string, quote arguments containing
2+
``:&<>^|`` on Windows. Patch by John Keith Hohm.

0 commit comments

Comments
 (0)