Skip to content

Commit 7423214

Browse files
committed
subprocess: Do not escape double quotes for command line arguments on Windows
* refactor: Guard `util::quote_argument()` with `#ifdef __USING_WINDOWS__` The `util::quote_argument()` function is specific to Windows and is used in code already guarded by `#ifdef __USING_WINDOWS__`. * Do not escape double quotes for command line arguments on Windows This change fixes the handling of double quotes and aligns the behavior with Python's `Popen` class. For example: ``` >py -3 >>> import subprocess >>> p = subprocess.Popen("cmd.exe /c dir \"C:\\Program Files\"", stdout=subprocess.PIPE, text=True) >>> print(f"Captured stdout:\n{stdout}") ``` Currently, the same command line processed by the `quote_argument()` function looks like `cmd.exe /c dir "\"C:\Program" "Files\""`, which is broken. With this change, it looks correct: `cmd.exe /c dir "C:\Program Files"`. Github-Pull: arun11299/cpp-subprocess#113 Rebased-From: ed313971c04ac10dc006104aba07d016ffc6542a
1 parent bb9ffea commit 7423214

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/util/subprocess.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ class OSError: public std::runtime_error
171171
//--------------------------------------------------------------------
172172
namespace util
173173
{
174+
#ifdef __USING_WINDOWS__
174175
inline void quote_argument(const std::wstring &argument, std::wstring &command_line,
175176
bool force)
176177
{
@@ -181,7 +182,7 @@ namespace util
181182
//
182183

183184
if (force == false && argument.empty() == false &&
184-
argument.find_first_of(L" \t\n\v\"") == argument.npos) {
185+
argument.find_first_of(L" \t\n\v") == argument.npos) {
185186
command_line.append(argument);
186187
}
187188
else {
@@ -231,7 +232,6 @@ namespace util
231232
}
232233
}
233234

234-
#ifdef __USING_WINDOWS__
235235
inline std::string get_last_error(DWORD errorMessageID)
236236
{
237237
if (errorMessageID == 0)

0 commit comments

Comments
 (0)