Skip to content

Commit

Permalink
Merge pull request #2563 from pypa/cmd-quote-better
Browse files Browse the repository at this point in the history
Only quote arguments during cmdify if needed
  • Loading branch information
uranusjr committed Jul 11, 2018
2 parents 399c879 + 8696520 commit aec7881
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
2 changes: 2 additions & 0 deletions news/2563.bugfix
@@ -0,0 +1,2 @@
Improve quoting logic for ``pipenv run`` so it works better with Windows
built-in commands.
9 changes: 8 additions & 1 deletion pipenv/cmdparse.py
Expand Up @@ -55,11 +55,18 @@ def cmdify(self):
The result is then quoted into a pair of double quotes to be grouped.
An argument is intentionally not quoted if it does not contain
whitespaces. This is done to be compatible with Windows built-in
commands that don't work well with quotes, e.g. everything with `echo`,
and DOS-style (forward slash) switches.
The intended use of this function is to pre-process an argument list
before passing it into ``subprocess.Popen(..., shell=True)``.
See also: https://docs.python.org/3/library/subprocess.html#converting-argument-sequence
"""
return " ".join(
'"{0}"'.format(re.sub(r'(\\*)"', r'\1\1\\"', arg)) for arg in self._parts
arg if not next(re.finditer(r'\s', arg), None)
else '"{0}"'.format(re.sub(r'(\\*)"', r'\1\1\\"', arg))
for arg in self._parts
)
6 changes: 3 additions & 3 deletions tests/unit/test_cmdparse.py
Expand Up @@ -30,9 +30,9 @@ def test_extend():
@pytest.mark.run
@pytest.mark.script
def test_cmdify():
script = Script('python', ['-c', "print('hello')"])
script = Script('python', ['-c', "print('hello world')"])
cmd = script.cmdify()
assert cmd == '"python" "-c" "print(\'hello\')"', script
assert cmd == 'python -c "print(\'hello world\')"', script


@pytest.mark.run
Expand All @@ -44,6 +44,6 @@ def test_cmdify_complex():
]))
assert script.cmdify() == ' '.join([
'"C:\\Program Files\\Python36\\python.exe"',
'"-c"',
'-c',
""" "print(\'Double quote: \\\"\')" """.strip(),
]), script

0 comments on commit aec7881

Please sign in to comment.