New submission from James Xu <ke...@microsoft.com>:

While working on our project, we have noticed that for 
`subprocess.Popen(command, ...)`, when `command` is a string that contains 
escaped double quote, for example, `command = '"path to executable" --flag 
arg'`, this works fine. However, when command is changed to 
`shlex.split(command, posix=False)`, the Popen command fails. Looking a bit 
into the source code, it seems that for the command above,

```
>>> shlex.split('"path to executable" --flag arg', posix=False)
['"path to executable"', '--flag', 'arg']
```

and when this array of strings gets passed into `Popen`, the escaped double 
quote gets escaped again, since `subprocess.list2cmdline` does not check if a 
pair of double quote or single quote are wrapping the entire string `arg`. And 
this is the same behavior for both py2 and py3, 
https://github.com/python/cpython/blob/master/Lib/subprocess.py#L508. As a 
result, upon execution the command becomes, `'"\\"path to executable\\"" --flag 
arg'`

example:
```
>>> sp.list2cmdline(['"do things"'])
'"\\"do things\\""'
>>> sp.list2cmdline(['do things'])
'"do things"'
>
```

----------
components: Windows
messages: 348342
nosy: kejxu, paul.moore, steve.dower, tim.golden, zach.ware
priority: normal
severity: normal
status: open
title: subprocess.list2cmdline() should not escape wrapping single/double quotes
type: behavior
versions: Python 3.9

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue37659>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to