Change format_command() to use shlex.quote().

This commit is contained in:
Chris Jerdonek 2019-02-11 08:45:38 -08:00
parent 1d0645e86f
commit 0f6776388c
3 changed files with 5 additions and 10 deletions

2
news/6290.feature Normal file
View File

@ -0,0 +1,2 @@
Command arguments in ``subprocess`` log messages are now quoted using
``shlex.quote()``.

View File

@ -23,7 +23,7 @@ from pip._vendor import pkg_resources
# why we ignore the type on this import.
from pip._vendor.retrying import retry # type: ignore
from pip._vendor.six import PY2
from pip._vendor.six.moves import input
from pip._vendor.six.moves import input, shlex_quote
from pip._vendor.six.moves.urllib import parse as urllib_parse
from pip._vendor.six.moves.urllib.parse import unquote as urllib_unquote
@ -655,14 +655,7 @@ def format_command_args(args):
"""
Format command arguments for display.
"""
parts = []
for arg in args:
if ' ' in arg or '\n' in arg or '"' in arg or "'" in arg:
arg = '"%s"' % arg.replace('"', '\\"')
parts.append(arg)
command = ' '.join(parts)
return command
return ' '.join(shlex_quote(arg) for arg in args)
def call_subprocess(

View File

@ -728,7 +728,7 @@ class TestGetProg(object):
@pytest.mark.parametrize('args, expected', [
(['pip', 'list'], 'pip list'),
(['foo', 'space space', 'new\nline', 'double"quote', "single'quote"],
'foo "space space" "new\nline" "double\\"quote" "single\'quote"'),
"""foo 'space space' 'new\nline' 'double"quote' 'single'"'"'quote'"""),
])
def test_format_command_args(args, expected):
actual = format_command_args(args)