[Python-Dev] subprocess shell=True on Windows doesn't escape ^ character (original) (raw)

Tim Golden mail at timgolden.me.uk
Fri Jun 13 09:35:41 CEST 2014


On 13/06/2014 03:11, Nikolaus Rath wrote:

"R. David Murray" <rdmurray at bitdance.com> writes:

Also notice that using a list with shell=True is using the API incorrectly. It wouldn't even work on Linux, so that torpedoes the cross-platform concern already :)

This kind of confusion is why I opened http://bugs.python.org/issue7839. Can someone describe an use case where shell=True actually makes sense at all?

On Windows (where I think the OP is), Popen & friends ultimately invoke CreateProcess.

In the case where shell=True, subprocess invokes the command interpreter explictly under the covers and tweaks a few other things to avoid a Brief Flash of Unstyled Console. This is the relevant snippet from subprocess.py:

        if shell:
            startupinfo.dwFlags |= _winapi.STARTF_USESHOWWINDOW
            startupinfo.wShowWindow = _winapi.SW_HIDE
            comspec = os.environ.get("COMSPEC", "cmd.exe")
            args = '{} /c "{}"'.format (comspec, args)

That's all. It's more or less equivalent to your prefixing your commands with "cmd.exe /c".

The only reasons you should need to do this are:

I use hg.exe (from TortoiseHg) but ISTR that the base Mercurial install supplies a .bat/.cmd. If that's the OP's case then he might find it necessary to pass shell=True.

TJG



More information about the Python-Dev mailing list