From 9315887101ff905a98a803a81f5ea44cf1941818 Mon Sep 17 00:00:00 2001 From: Tzu-ping Chung Date: Wed, 11 Jul 2018 17:17:08 +0800 Subject: [PATCH 1/2] Only quote arguments during cmdify if needed --- pipenv/cmdparse.py | 9 ++++++++- tests/unit/test_cmdparse.py | 6 +++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/pipenv/cmdparse.py b/pipenv/cmdparse.py index aa3442b8ef..087a95b14d 100644 --- a/pipenv/cmdparse.py +++ b/pipenv/cmdparse.py @@ -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 ) diff --git a/tests/unit/test_cmdparse.py b/tests/unit/test_cmdparse.py index 064eda7970..06012d0770 100644 --- a/tests/unit/test_cmdparse.py +++ b/tests/unit/test_cmdparse.py @@ -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 @@ -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 From 86965203bffd6e4593bb17cca4b533309f90de0c Mon Sep 17 00:00:00 2001 From: Tzu-ping Chung Date: Wed, 11 Jul 2018 17:26:18 +0800 Subject: [PATCH 2/2] Add news fragment --- news/2563.bugfix | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 news/2563.bugfix diff --git a/news/2563.bugfix b/news/2563.bugfix new file mode 100644 index 0000000000..4d2e65709b --- /dev/null +++ b/news/2563.bugfix @@ -0,0 +1,2 @@ +Improve quoting logic for ``pipenv run`` so it works better with Windows +built-in commands.