diff options
author | anatoly techtonik <techtonik@gmail.com> | 2012-12-12 15:02:28 (GMT) |
---|---|---|
committer | anatoly techtonik <techtonik@gmail.com> | 2012-12-12 15:02:28 (GMT) |
commit | 2e3fb8d5b786eec70b69ae2a058581d6fa892bf9 (patch) | |
tree | f73340d6d42635cd7d632826a204df1e2f70da01 /runtest.py | |
parent | 58b9a8a944854346f09f0502147429303211f8dc (diff) | |
download | SCons-2e3fb8d5b786eec70b69ae2a058581d6fa892bf9.zip SCons-2e3fb8d5b786eec70b69ae2a058581d6fa892bf9.tar.gz SCons-2e3fb8d5b786eec70b69ae2a058581d6fa892bf9.tar.bz2 |
Remove subprocess compatibility code used for Python < 2.4
Diffstat (limited to 'runtest.py')
-rw-r--r-- | runtest.py | 255 |
1 files changed, 107 insertions, 148 deletions
@@ -308,93 +308,79 @@ def escape(s): s = s.replace('\\', '\\\\') return s -# Try to use subprocess instead of the more low-level -# spawn command... -use_subprocess = True -try: - import subprocess -except: - use_subprocess = False - -if use_subprocess: - if not suppress_stdout and not suppress_stderr: - # Without any output suppressed, we let the subprocess - # write its stuff freely to stdout/stderr. + +import subprocess + +if not suppress_stdout and not suppress_stderr: + # Without any output suppressed, we let the subprocess + # write its stuff freely to stdout/stderr. + def spawn_it(command_args): + p = subprocess.Popen(' '.join(command_args), + shell=True) + return (None, None, p.wait()) +else: + # Else, we catch the output of both pipes... + if allow_pipe_files: + # The subprocess.Popen() suffers from a well-known + # problem. Data for stdout/stderr is read into a + # memory buffer of fixed size, 65K which is not very much. + # When it fills up, it simply stops letting the child process + # write to it. The child will then sit and patiently wait to + # be able to write the rest of its output. Hang! + # In order to work around this, we follow a suggestion + # by Anders Pearson in + # http://http://thraxil.org/users/anders/posts/2008/03/13/Subprocess-Hanging-PIPE-is-your-enemy/ + # and pass temp file objects to Popen() instead of the ubiquitous + # subprocess.PIPE. def spawn_it(command_args): + # Create temporary files + import tempfile + tmp_stdout = tempfile.TemporaryFile(mode='w+t') + tmp_stderr = tempfile.TemporaryFile(mode='w+t') + # Start subprocess... p = subprocess.Popen(' '.join(command_args), + stdout=tmp_stdout, + stderr=tmp_stderr, shell=True) - return (None, None, p.wait()) - else: - # Else, we catch the output of both pipes... - if allow_pipe_files: - # The subprocess.Popen() suffers from a well-known - # problem. Data for stdout/stderr is read into a - # memory buffer of fixed size, 65K which is not very much. - # When it fills up, it simply stops letting the child process - # write to it. The child will then sit and patiently wait to - # be able to write the rest of its output. Hang! - # In order to work around this, we follow a suggestion - # by Anders Pearson in - # http://http://thraxil.org/users/anders/posts/2008/03/13/Subprocess-Hanging-PIPE-is-your-enemy/ - # and pass temp file objects to Popen() instead of the ubiquitous - # subprocess.PIPE. - def spawn_it(command_args): - # Create temporary files - import tempfile - tmp_stdout = tempfile.TemporaryFile(mode='w+t') - tmp_stderr = tempfile.TemporaryFile(mode='w+t') - # Start subprocess... - p = subprocess.Popen(' '.join(command_args), - stdout=tmp_stdout, - stderr=tmp_stderr, - shell=True) - # ... and wait for it to finish. - ret = p.wait() - - try: - # Rewind to start of files - tmp_stdout.seek(0) - tmp_stderr.seek(0) - # Read output - spawned_stdout = tmp_stdout.read() - spawned_stderr = tmp_stderr.read() - finally: - # Remove temp files by closing them - tmp_stdout.close() - tmp_stderr.close() - - # Return values - return (spawned_stderr, spawned_stdout, ret) + # ... and wait for it to finish. + ret = p.wait() - else: - # We get here only if the user gave the '--nopipefiles' - # option, meaning the "temp file" approach for - # subprocess.communicate() above shouldn't be used. - # He hopefully knows what he's doing, but again we have a - # potential deadlock situation in the following code: - # If the subprocess writes a lot of data to its stderr, - # the pipe will fill up (nobody's reading it yet) and the - # subprocess will wait for someone to read it. - # But the parent process is trying to read from stdin - # (but the subprocess isn't writing anything there). - # Hence a deadlock. - # Be dragons here! Better don't use this! - def spawn_it(command_args): - p = subprocess.Popen(' '.join(command_args), - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - shell=True) - spawned_stdout = p.stdout.read() - spawned_stderr = p.stderr.read() - return (spawned_stderr, spawned_stdout, p.wait()) -else: - has_subprocess = False - # Set up lowest-common-denominator spawning of a process on both Windows - # and non-Windows systems that works all the way back to Python 1.6 - def spawn_it(command_args): - command = command_args[0] - command_args = [escape(c) for c in command_args] - return (None, None, os.spawnv(os.P_WAIT, command, command_args)) + try: + # Rewind to start of files + tmp_stdout.seek(0) + tmp_stderr.seek(0) + # Read output + spawned_stdout = tmp_stdout.read() + spawned_stderr = tmp_stderr.read() + finally: + # Remove temp files by closing them + tmp_stdout.close() + tmp_stderr.close() + + # Return values + return (spawned_stderr, spawned_stdout, ret) + + else: + # We get here only if the user gave the '--nopipefiles' + # option, meaning the "temp file" approach for + # subprocess.communicate() above shouldn't be used. + # He hopefully knows what he's doing, but again we have a + # potential deadlock situation in the following code: + # If the subprocess writes a lot of data to its stderr, + # the pipe will fill up (nobody's reading it yet) and the + # subprocess will wait for someone to read it. + # But the parent process is trying to read from stdin + # (but the subprocess isn't writing anything there). + # Hence a deadlock. + # Be dragons here! Better don't use this! + def spawn_it(command_args): + p = subprocess.Popen(' '.join(command_args), + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + shell=True) + spawned_stdout = p.stdout.read() + spawned_stderr = p.stderr.read() + return (spawned_stderr, spawned_stdout, p.wait()) class Base(object): def __init__(self, path, spe=None): @@ -415,71 +401,44 @@ class SystemExecutor(Base): if s < 0 or s > 2: sys.stdout.write("Unexpected exit status %d\n" % s) -if not use_subprocess: - import popen2 - try: - popen2.Popen3 - except AttributeError: - class PopenExecutor(Base): - def execute(self): - (tochild, fromchild, childerr) = os.popen3(self.command_str) - tochild.close() - self.stderr = childerr.read() - self.stdout = fromchild.read() - fromchild.close() - self.status = childerr.close() - if not self.status: - self.status = 0 - else: - self.status = self.status >> 8 - else: - class PopenExecutor(Base): - def execute(self): - p = popen2.Popen3(self.command_str, 1) - p.tochild.close() - self.stdout = p.fromchild.read() - self.stderr = p.childerr.read() - self.status = p.wait() - self.status = self.status >> 8 -else: - class PopenExecutor(Base): - # For an explanation of the following 'if ... else' - # and the 'allow_pipe_files' option, please check out the - # use_subprocess path in the definition of spawn_it() above. - if allow_pipe_files: - def execute(self): - # Create temporary files - import tempfile - tmp_stdout = tempfile.TemporaryFile(mode='w+t') - tmp_stderr = tempfile.TemporaryFile(mode='w+t') - # Start subprocess... - p = subprocess.Popen(self.command_str, - stdout=tmp_stdout, - stderr=tmp_stderr, - shell=True) - # ... and wait for it to finish. - self.status = p.wait() - - try: - # Rewind to start of files - tmp_stdout.seek(0) - tmp_stderr.seek(0) - # Read output - self.stdout = tmp_stdout.read() - self.stderr = tmp_stderr.read() - finally: - # Remove temp files by closing them - tmp_stdout.close() - tmp_stderr.close() - else: - def execute(self): - p = subprocess.Popen(self.command_str, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - shell=True) - self.stdout = p.stdout.read() - self.stderr = p.stderr.read() - self.status = p.wait() +class PopenExecutor(Base): + # For an explanation of the following 'if ... else' + # and the 'allow_pipe_files' option, please check out the + # definition of spawn_it() above. + if allow_pipe_files: + def execute(self): + # Create temporary files + import tempfile + tmp_stdout = tempfile.TemporaryFile(mode='w+t') + tmp_stderr = tempfile.TemporaryFile(mode='w+t') + # Start subprocess... + p = subprocess.Popen(self.command_str, + stdout=tmp_stdout, + stderr=tmp_stderr, + shell=True) + # ... and wait for it to finish. + self.status = p.wait() + + try: + # Rewind to start of files + tmp_stdout.seek(0) + tmp_stderr.seek(0) + # Read output + self.stdout = tmp_stdout.read() + self.stderr = tmp_stderr.read() + finally: + # Remove temp files by closing them + tmp_stdout.close() + tmp_stderr.close() + else: + def execute(self): + p = subprocess.Popen(self.command_str, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + shell=True) + self.stdout = p.stdout.read() + self.stderr = p.stderr.read() + self.status = p.wait() class XML(PopenExecutor): def header(self, f): |