Commit 4eafb11a authored by Nguyễn Gia Phong's avatar Nguyễn Gia Phong Committed by GitHub

Prevent test failures when Python executable path contains whitespace (GH-3372)

parent 3a7fafdb
...@@ -2,6 +2,8 @@ from __future__ import absolute_import ...@@ -2,6 +2,8 @@ from __future__ import absolute_import
import os import os
import unittest import unittest
import shlex
import sys
import tempfile import tempfile
from .Compiler import Errors from .Compiler import Errors
...@@ -184,34 +186,41 @@ class TreeAssertVisitor(VisitorTransform): ...@@ -184,34 +186,41 @@ class TreeAssertVisitor(VisitorTransform):
visit_Node = VisitorTransform.recurse_to_children visit_Node = VisitorTransform.recurse_to_children
def unpack_source_tree(tree_file, dir=None): def unpack_source_tree(tree_file, workdir, cython_root):
if dir is None: programs = {
dir = tempfile.mkdtemp() 'PYTHON': [sys.executable],
header = [] 'CYTHON': [sys.executable, os.path.join(cython_root, 'cython.py')],
cur_file = None 'CYTHONIZE': [sys.executable, os.path.join(cython_root, 'cythonize.py')]
f = open(tree_file) }
try:
lines = f.readlines() if workdir is None:
finally: workdir = tempfile.mkdtemp()
f.close() header, cur_file = [], None
del f with open(tree_file) as f:
try: try:
for line in lines: for line in f:
if line[:5] == '#####': if line.startswith('#####'):
filename = line.strip().strip('#').strip().replace('/', os.path.sep) filename = line.strip().strip('#').strip().replace('/', os.path.sep)
path = os.path.join(dir, filename) path = os.path.join(workdir, filename)
if not os.path.exists(os.path.dirname(path)): if not os.path.exists(os.path.dirname(path)):
os.makedirs(os.path.dirname(path)) os.makedirs(os.path.dirname(path))
if cur_file is not None: if cur_file is not None:
f, cur_file = cur_file, None to_close, cur_file = cur_file, None
f.close() to_close.close()
cur_file = open(path, 'w') cur_file = open(path, 'w')
elif cur_file is not None: elif cur_file is not None:
cur_file.write(line) cur_file.write(line)
elif line.strip() and not line.lstrip().startswith('#'): elif line.strip() and not line.lstrip().startswith('#'):
if line.strip() not in ('"""', "'''"): if line.strip() not in ('"""', "'''"):
header.append(line) command = shlex.split(line)
finally: if not command: continue
if cur_file is not None: # In Python 3: prog, *args = command
cur_file.close() prog, args = command[0], command[1:]
return dir, ''.join(header) try:
header.append(programs[prog]+args)
except KeyError:
header.append(command)
finally:
if cur_file is not None:
cur_file.close()
return workdir, header
...@@ -1728,7 +1728,7 @@ class EndToEndTest(unittest.TestCase): ...@@ -1728,7 +1728,7 @@ class EndToEndTest(unittest.TestCase):
def setUp(self): def setUp(self):
from Cython.TestUtils import unpack_source_tree from Cython.TestUtils import unpack_source_tree
_, self.commands = unpack_source_tree(self.treefile, self.workdir) _, self.commands = unpack_source_tree(self.treefile, self.workdir, self.cython_root)
self.old_dir = os.getcwd() self.old_dir = os.getcwd()
os.chdir(self.workdir) os.chdir(self.workdir)
if self.workdir not in sys.path: if self.workdir not in sys.path:
...@@ -1753,10 +1753,6 @@ class EndToEndTest(unittest.TestCase): ...@@ -1753,10 +1753,6 @@ class EndToEndTest(unittest.TestCase):
def runTest(self): def runTest(self):
self.success = False self.success = False
commands = (self.commands
.replace("CYTHONIZE", "PYTHON %s" % os.path.join(self.cython_root, 'cythonize.py'))
.replace("CYTHON", "PYTHON %s" % os.path.join(self.cython_root, 'cython.py'))
.replace("PYTHON", sys.executable))
old_path = os.environ.get('PYTHONPATH') old_path = os.environ.get('PYTHONPATH')
env = dict(os.environ) env = dict(os.environ)
new_path = self.cython_syspath new_path = self.cython_syspath
...@@ -1766,21 +1762,15 @@ class EndToEndTest(unittest.TestCase): ...@@ -1766,21 +1762,15 @@ class EndToEndTest(unittest.TestCase):
cmd = [] cmd = []
out = [] out = []
err = [] err = []
for command_no, command in enumerate(filter(None, commands.splitlines()), 1): for command_no, command in enumerate(self.commands, 1):
with self.stats.time('%s(%d)' % (self.name, command_no), 'c', with self.stats.time('%s(%d)' % (self.name, command_no), 'c',
'etoe-build' if ' setup.py ' in command else 'etoe-run'): 'etoe-build' if 'setup.py' in command else 'etoe-run'):
if self.capture: if self.capture:
p = subprocess.Popen(command, p = subprocess.Popen(command, stderr=subprocess.PIPE, stdout=subprocess.PIPE, env=env)
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
shell=True,
env=env)
_out, _err = p.communicate() _out, _err = p.communicate()
res = p.returncode res = p.returncode
else: else:
p = subprocess.call(command, p = subprocess.call(command, env=env)
shell=True,
env=env)
_out, _err = b'', b'' _out, _err = b'', b''
res = p res = p
cmd.append(command) cmd.append(command)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment