From 0aa72e0a99e330c001db346bcc9b415e557a729c Mon Sep 17 00:00:00 2001 From: Jean-Christophe Fillion-Robin Date: Thu, 1 Dec 2016 15:51:09 -0500 Subject: [PATCH] virtualenv/installed_packages: Add support for windows Invoking "installed_packages" on windows fails because it is trying to execute multi-line python code as a string with "shell=true". In that case, "cmd.exe" fails because it does NOT know how to handle newline character. This confirmed by example (2) reported below. Instead, this commit passes the command as an array of arguments that ends up calling subprocess.Popen with "shell=false". Examples: 1) Executing "import sys; print(sys.path)": OK ``` C:\Users\appveyor>cmd.exe /c python -c "import sys; print(sys.path)" ['', 'C:\\windows\\SYSTEM32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\ \lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27 ', 'C:\\Python27\\lib\\site-packages'] ``` 2) Executing "import sys\nfor p in sys.path: print(p)": FAIL ``` C:\Users\appveyor>cmd.exe /c python -c "import sys\nfor p in sys.path: print(p)" File "", line 1 import sys\nfor p in sys.path: print(p) ^ SyntaxError: unexpected character after line continuation character ``` --- pytest-virtualenv/pytest_virtualenv.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytest-virtualenv/pytest_virtualenv.py b/pytest-virtualenv/pytest_virtualenv.py index caf4341f406..36f3dffa083 100644 --- a/pytest-virtualenv/pytest_virtualenv.py +++ b/pytest-virtualenv/pytest_virtualenv.py @@ -214,7 +214,7 @@ def installed_packages(self, package_type=None): res = {} code = "from pkg_resources import working_set\n"\ "for i in working_set: print(i.project_name + ' ' + i.version + ' ' + i.location)" - lines = self.run('%s -c "%s"' % (self.python, code), capture=True).split('\n') + lines = self.run([self.python, "-c", code], capture=True).split('\n') for line in [i.strip() for i in lines if i.strip()]: name, version, location = line.split() res[name] = PackageEntry(name, version, location)