From 51b00f63dcf3933949387effe325c185c5109a76 Mon Sep 17 00:00:00 2001 From: John Keith Hohm Date: Thu, 22 May 2025 16:38:33 -0400 Subject: [PATCH 1/3] Also quote arguments containing `:&<>^|` on Windows --- Lib/subprocess.py | 2 +- Lib/test/test_subprocess.py | 9 +++++++++ .../2025-05-22-16-36-52.gh-issue-133545.YHvz8H.rst | 2 ++ 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Windows/2025-05-22-16-36-52.gh-issue-133545.YHvz8H.rst diff --git a/Lib/subprocess.py b/Lib/subprocess.py index 54c2eb515b60da..b341ccd3639f17 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -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('"') diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py index ca35804fb36076..7b437e815538a4 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -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 diff --git a/Misc/NEWS.d/next/Windows/2025-05-22-16-36-52.gh-issue-133545.YHvz8H.rst b/Misc/NEWS.d/next/Windows/2025-05-22-16-36-52.gh-issue-133545.YHvz8H.rst new file mode 100644 index 00000000000000..fa8df0cb20d588 --- /dev/null +++ b/Misc/NEWS.d/next/Windows/2025-05-22-16-36-52.gh-issue-133545.YHvz8H.rst @@ -0,0 +1,2 @@ +In addition to space, tab, and empty string, quote arguments containing +``:&<>^|`` on Windows. Patch by John Keith Hohm. From 9bbcfcd4957130087691de7299250ad598d0962a Mon Sep 17 00:00:00 2001 From: John Keith Hohm Date: Thu, 22 May 2025 17:13:18 -0400 Subject: [PATCH 2/3] Do not quote `:` after all, it is far too common and not always treated specially --- Lib/subprocess.py | 2 +- .../next/Windows/2025-05-22-16-36-52.gh-issue-133545.YHvz8H.rst | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/subprocess.py b/Lib/subprocess.py index b341ccd3639f17..70bc8af4dcc0f1 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -618,7 +618,7 @@ def list2cmdline(seq): if result: result.append(' ') - needquote = not arg or not set(" \t:&<>^|").isdisjoint(arg) + needquote = not arg or not set(" \t&<>^|").isdisjoint(arg) if needquote: result.append('"') diff --git a/Misc/NEWS.d/next/Windows/2025-05-22-16-36-52.gh-issue-133545.YHvz8H.rst b/Misc/NEWS.d/next/Windows/2025-05-22-16-36-52.gh-issue-133545.YHvz8H.rst index fa8df0cb20d588..171c44cfe315ba 100644 --- a/Misc/NEWS.d/next/Windows/2025-05-22-16-36-52.gh-issue-133545.YHvz8H.rst +++ b/Misc/NEWS.d/next/Windows/2025-05-22-16-36-52.gh-issue-133545.YHvz8H.rst @@ -1,2 +1,2 @@ In addition to space, tab, and empty string, quote arguments containing -``:&<>^|`` on Windows. Patch by John Keith Hohm. +``&<>^|`` on Windows. Patch by John Keith Hohm. From 3c620ade20a64adb049d461ebe4d7421c717a638 Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" Date: Fri, 23 May 2025 15:06:59 -0700 Subject: [PATCH 3/3] reword NEWS entry --- .../Windows/2025-05-22-16-36-52.gh-issue-133545.YHvz8H.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Windows/2025-05-22-16-36-52.gh-issue-133545.YHvz8H.rst b/Misc/NEWS.d/next/Windows/2025-05-22-16-36-52.gh-issue-133545.YHvz8H.rst index 171c44cfe315ba..e4668ed977c665 100644 --- a/Misc/NEWS.d/next/Windows/2025-05-22-16-36-52.gh-issue-133545.YHvz8H.rst +++ b/Misc/NEWS.d/next/Windows/2025-05-22-16-36-52.gh-issue-133545.YHvz8H.rst @@ -1,2 +1,2 @@ -In addition to space, tab, and empty string, quote arguments containing -``&<>^|`` on Windows. Patch by John Keith Hohm. +In :mod:`subprocess` on Windows, in addition to space, tab, and empty string, quote arguments containing +``&<>^|``. Patch by John Keith Hohm.