Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only quote arguments during cmdify if needed #2563

Merged
merged 2 commits into from
Jul 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions news/2563.bugfix
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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