New submission from Jim Jewett:

Inspired by https://mail.python.org/pipermail/python-dev/2014-June/135029.html 
and the following thread.  

"""
Normal Windows behavior:

  >hg status --rev ".^1"
  M mercurial\commands.py
  ? pysptest.py

  >hg status --rev .^1
  abort: unknown revision '.1'!

So, ^ is an escape character. See
http://www.tomshardware.co.uk/forum/35565-45-when-special-command-line
"""

It probably isn't possible to pre-escape commands for every possible command 
interpreter, but it makes sense to get the standard shells right. 

In fact, global function list2cmdline already exists (and is apparently 
specific to the Microsoft compiler), but ... its rules are not the same as 
those of the default windows shell.  (Per the tomshardware link, those rules 
(for windows) did change a while back.)

I propose a similar list2shellcmd function.  Based on my own very incomplete 
information, it would currently look like:

def list2shellcmd(seq):
    """Turn the sequence of arguments into a single command line, with escaped 
characters."""
    if mswindows:
        line=list2cmdline(seq).replace("^", "^^")
    else:
        line=" ".join(seq)
    return line

but there may well be escapes (such as \) that are appropriate even for posix.

Note that related issue http://bugs.python.org/issue7839 proposes a larger 
cleanup, such as forbidding the problematic functionality entirely.

----------
components: Library (Lib)
messages: 220483
nosy: Jim.Jewett
priority: normal
severity: normal
status: open
title: subprocess shell=True on Windows character escaping
type: enhancement
versions: Python 3.5

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

Reply via email to