提交 6e7018b6 authored 作者: Frederic's avatar Frederic

Always use stdin=devnull when calling Popen.

上级 fc732bc6
...@@ -1481,11 +1481,9 @@ def gcc_llvm(): ...@@ -1481,11 +1481,9 @@ def gcc_llvm():
""" """
if gcc_llvm.is_llvm is None: if gcc_llvm.is_llvm is None:
pass pass
dummy_in = open(os.devnull)
p = None p = None
try: try:
p = call_subprocess_Popen(['g++', '--version'], p = call_subprocess_Popen(['g++', '--version'],
stdin=dummy_in.fileno(),
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE) stderr=subprocess.PIPE)
p.wait() p.wait()
...@@ -1499,7 +1497,6 @@ def gcc_llvm(): ...@@ -1499,7 +1497,6 @@ def gcc_llvm():
# will crash later so supposing it is not llvm is "safe". # will crash later so supposing it is not llvm is "safe".
output = '' output = ''
del p del p
del dummy_in
gcc_llvm.is_llvm = "llvm" in output gcc_llvm.is_llvm = "llvm" in output
return gcc_llvm.is_llvm return gcc_llvm.is_llvm
gcc_llvm.is_llvm = None gcc_llvm.is_llvm = None
...@@ -1546,16 +1543,11 @@ class GCC_compiler(object): ...@@ -1546,16 +1543,11 @@ class GCC_compiler(object):
GCC_compiler.march_flags = [] GCC_compiler.march_flags = []
def get_lines(cmd, parse=True): def get_lines(cmd, parse=True):
dummy_in = open(os.devnull)
try:
p = call_subprocess_Popen(cmd, p = call_subprocess_Popen(cmd,
stdin=dummy_in.fileno(),
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
shell=True) shell=True)
p.wait() p.wait()
finally:
del dummy_in
stdout = p.stdout.readlines() stdout = p.stdout.readlines()
stderr = p.stderr.readlines() stderr = p.stderr.readlines()
lines = [] lines = []
...@@ -1650,7 +1642,6 @@ class GCC_compiler(object): ...@@ -1650,7 +1642,6 @@ class GCC_compiler(object):
try: try:
fd, path = tempfile.mkstemp(suffix='.c', prefix=tmp_prefix) fd, path = tempfile.mkstemp(suffix='.c', prefix=tmp_prefix)
exe_path = path[:-2] exe_path = path[:-2]
dummy_stdin = open(os.devnull)
try: try:
os.write(fd, src_code) os.write(fd, src_code)
os.close(fd) os.close(fd)
...@@ -1658,8 +1649,7 @@ class GCC_compiler(object): ...@@ -1658,8 +1649,7 @@ class GCC_compiler(object):
proc = call_subprocess_Popen( proc = call_subprocess_Popen(
['g++', path, '-o', exe_path] + flags, ['g++', path, '-o', exe_path] + flags,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stderr=subprocess.PIPE)
stdin=dummy_stdin.fileno())
proc.wait() proc.wait()
if proc.returncode != 0: if proc.returncode != 0:
compilation_ok = False compilation_ok = False
...@@ -1669,14 +1659,12 @@ class GCC_compiler(object): ...@@ -1669,14 +1659,12 @@ class GCC_compiler(object):
try: try:
proc = call_subprocess_Popen([exe_path], proc = call_subprocess_Popen([exe_path],
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stderr=subprocess.PIPE)
stdin=dummy_stdin.fileno())
proc.wait() proc.wait()
run_ok = (proc.returncode == 0) run_ok = (proc.returncode == 0)
finally: finally:
os.remove(exe_path) os.remove(exe_path)
finally: finally:
del dummy_stdin
try: try:
if fd is not None: if fd is not None:
os.close(fd) os.close(fd)
......
...@@ -18,13 +18,11 @@ from theano.misc.windows import call_subprocess_Popen ...@@ -18,13 +18,11 @@ from theano.misc.windows import call_subprocess_Popen
# Using the dummy file descriptors below is a workaround for a crash # Using the dummy file descriptors below is a workaround for a crash
# experienced in an unusual Python 2.4.4 Windows environment with the default # experienced in an unusual Python 2.4.4 Windows environment with the default
# None values. # None values.
dummy_in = open(os.devnull)
dummy_err = open(os.devnull, 'w') dummy_err = open(os.devnull, 'w')
p = None p = None
try: try:
p = call_subprocess_Popen(['g++', '-dumpversion'], p = call_subprocess_Popen(['g++', '-dumpversion'],
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stdin=dummy_in.fileno(),
stderr=dummy_err.fileno()) stderr=dummy_err.fileno())
p.wait() p.wait()
gcc_version_str = p.stdout.readline().strip().decode() gcc_version_str = p.stdout.readline().strip().decode()
...@@ -32,7 +30,6 @@ except OSError: ...@@ -32,7 +30,6 @@ except OSError:
# Typically means gcc cannot be found. # Typically means gcc cannot be found.
gcc_version_str = 'GCC_NOT_FOUND' gcc_version_str = 'GCC_NOT_FOUND'
del p del p
del dummy_in
del dummy_err del dummy_err
compiledir_format_dict = {"platform": platform.platform(), compiledir_format_dict = {"platform": platform.platform(),
......
...@@ -21,5 +21,18 @@ def call_subprocess_Popen(command, **params): ...@@ -21,5 +21,18 @@ def call_subprocess_Popen(command, **params):
# execute "g++" without extensions. # execute "g++" without extensions.
# (Executing "g++.bat" explicitly would also work.) # (Executing "g++.bat" explicitly would also work.)
params['shell'] = True params['shell'] = True
# Using the dummy file descriptors below is a workaround for a
# crash experienced in an unusual Python 2.4.4 Windows environment
# with the default None values.
stdin = None
if "stdin" not in params:
stdin = open(os.devnull)
params['stdin'] = stdin.fileno()
try:
proc = subprocess.Popen(command, startupinfo=startupinfo, **params) proc = subprocess.Popen(command, startupinfo=startupinfo, **params)
finally:
if stdin is not None:
del stdin
return proc return proc
...@@ -276,8 +276,7 @@ def run(stdout, stderr, argv, theano_nose, batch_size, time_profile, ...@@ -276,8 +276,7 @@ def run(stdout, stderr, argv, theano_nose, batch_size, time_profile,
# test name in display # test name in display
# (see class 'DisabDocString' in file theano-nose) # (see class 'DisabDocString' in file theano-nose)
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
stdout=dummy_out.fileno(), stdout=dummy_out.fileno())
stdin=dummy_in.fileno())
# recovering and processing data from pipe # recovering and processing data from pipe
err = proc.stderr.read() err = proc.stderr.read()
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论