def run_benchmark(exe): from pypy.translator.cli.test.runtest import CliFunctionWrapper from pypy.translator.jvm.test.runtest import JvmGeneratedSourceWrapper if exe.__class__ in [CliFunctionWrapper, JvmGeneratedSourceWrapper]: stdout, stderr, retval = exe.run() else: assert isinstance(exe, str) bench = subprocess.Popen(exe, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = bench.communicate() retval = bench.wait() if retval != 0: print 'Running benchmark failed' print 'Standard Output:' print stdout print '-' * 40 print 'Standard Error:' print stderr raise SystemExit(-1) mydict = {} for line in stdout.splitlines(): name, res = line.split(':') mydict[name.strip()] = float(res) return mydict
def run(args, cwd, out, timeout=None): f = out.open('w') try: try: p = subprocess.Popen(args, cwd=str(cwd), stdout=f, stderr=f) except Exception, e: f.write("Failed to run %s with cwd='%s' timeout=%s:\n" " %s\n" % (args, cwd, timeout, e)) return RUNFAILED if timeout is None: return p.wait() else: timedout = None t0 = time.time() while True: returncode = p.poll() if returncode is not None: return timedout or returncode tnow = time.time() if (tnow - t0) > timeout: if timedout: _kill(p.pid, signal.SIGKILL) return TIMEDOUT else: timedout = TIMEDOUT _kill(p.pid, signal.SIGTERM) time.sleep(min(timeout, 10))
def test_stderr_none(self): # .stderr is None when not redirected p = subprocess.Popen([sys.executable, "-c", 'print "banana"'], stdin=subprocess.PIPE, stdout=subprocess.PIPE) p.wait() self.assertEqual(p.stderr, None)
def test_communicate_returns(self): # communicate() should return None if no redirection is active p = subprocess.Popen( [sys.executable, "-c", "import sys; sys.exit(47)"]) (stdout, stderr) = p.communicate() self.assertEqual(stdout, None) self.assertEqual(stderr, None)
def test_universal_newlines_communicate(self): # universal newlines through communicate() p = subprocess.Popen([ sys.executable, "-c", 'import sys,os;' + SETBINARY + 'sys.stdout.write("line1\\n");' 'sys.stdout.flush();' 'sys.stdout.write("line2\\r");' 'sys.stdout.flush();' 'sys.stdout.write("line3\\r\\n");' 'sys.stdout.flush();' 'sys.stdout.write("line4\\r");' 'sys.stdout.flush();' 'sys.stdout.write("\\nline5");' 'sys.stdout.flush();' 'sys.stdout.write("\\nline6");' ], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=1) (stdout, stderr) = p.communicate() if hasattr(open, 'newlines'): # Interpreter with universal newline support self.assertEqual(stdout, "line1\nline2\nline3\nline4\nline5\nline6") else: # Interpreter without universal newline support self.assertEqual(stdout, "line1\nline2\rline3\r\nline4\r\nline5\nline6")
def test_killsubprocess(): popen = subprocess.Popen([sys.executable, '-c', 'raw_input()'], stdin=subprocess.PIPE) time.sleep(0.9) assert popen.poll() is None assert popen.poll() is None killsubprocess(popen) waitdead(popen)
def test_stderr_pipe(self): # stderr redirection p = subprocess.Popen([ sys.executable, "-c", 'import sys; sys.stderr.write("strawberry")' ], stderr=subprocess.PIPE) self.assertEqual(remove_stderr_debug_decorations(p.stderr.read()), "strawberry")
def test_preexec(self): # preexec function p = subprocess.Popen([sys.executable, "-c", 'import sys,os;' \ 'sys.stdout.write(os.getenv("FRUIT"))'], stdout=subprocess.PIPE, preexec_fn=lambda: os.putenv("FRUIT", "apple")) self.assertEqual(p.stdout.read(), "apple")
def test_stdout_fileobj(self): # stdout is set to open file object tf = tempfile.TemporaryFile() p = subprocess.Popen( [sys.executable, "-c", 'import sys; sys.stdout.write("orange")'], stdout=tf) p.wait() tf.seek(0) self.assertEqual(tf.read(), "orange")
def test_shell_string(self): # Run command through the shell (string) newenv = os.environ.copy() newenv["FRUIT"] = "apple" p = subprocess.Popen("echo $FRUIT", shell=1, stdout=subprocess.PIPE, env=newenv) self.assertEqual(p.stdout.read().strip(), "apple")
def test_exceptions(self): # catched & re-raised exceptions try: p = subprocess.Popen([sys.executable, "-c", ""], cwd="/this/path/does/not/exist") except OSError, e: # The attribute child_traceback should contain "os.chdir" # somewhere. self.assertNotEqual(e.child_traceback.find("os.chdir"), -1)
def test_env(self): newenv = os.environ.copy() newenv["FRUIT"] = "orange" p = subprocess.Popen([sys.executable, "-c", 'import sys,os;' \ 'sys.stdout.write(os.getenv("FRUIT"))'], stdout=subprocess.PIPE, env=newenv) self.assertEqual(p.stdout.read(), "orange")
def test_shell_string(self): # Run command through the shell (string) newenv = os.environ.copy() newenv["FRUIT"] = "physalis" p = subprocess.Popen("set", shell=1, stdout=subprocess.PIPE, env=newenv) self.assertNotEqual(p.stdout.read().find("physalis"), -1)
def test_invalid_bufsize(self): # an invalid type of the bufsize argument should raise # TypeError. try: subprocess.Popen([sys.executable, "-c", "pass"], "orange") except TypeError: pass else: self.fail("Expected TypeError")
def test_stdout_filedes(self): # stdout is set to open file descriptor tf = tempfile.TemporaryFile() d = tf.fileno() p = subprocess.Popen( [sys.executable, "-c", 'import sys; sys.stdout.write("orange")'], stdout=d) p.wait() os.lseek(d, 0, 0) self.assertEqual(os.read(d, 1024), "orange")
def test_stdout_none(self): # .stdout is None when not redirected p = subprocess.Popen([ sys.executable, "-c", 'print " this bit of output is from a ' 'test of stdout in a different ' 'process ..."' ], stdin=subprocess.PIPE, stderr=subprocess.PIPE) p.wait() self.assertEqual(p.stdout, None)
def test_stderr_fileobj(self): # stderr is set to open file object tf = tempfile.TemporaryFile() p = subprocess.Popen([ sys.executable, "-c", 'import sys; sys.stderr.write("strawberry")' ], stderr=tf) p.wait() tf.seek(0) self.assertEqual(remove_stderr_debug_decorations(tf.read()), "strawberry")
def test_communicate(self): p = subprocess.Popen([sys.executable, "-c", 'import sys,os;' \ 'sys.stderr.write("pineapple");' \ 'sys.stdout.write(sys.stdin.read())'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) (stdout, stderr) = p.communicate("banana") self.assertEqual(stdout, "banana") self.assertEqual(remove_stderr_debug_decorations(stderr), "pineapple")
def test_stdin_pipe(self): # stdin redirection p = subprocess.Popen([ sys.executable, "-c", 'import sys; sys.exit(sys.stdin.read() == "pear")' ], stdin=subprocess.PIPE) p.stdin.write("pear") p.stdin.close() p.wait() self.assertEqual(p.returncode, 1)
def test_args_string(self): # args is a string f, fname = self.mkstemp() os.write(f, "#!/bin/sh\n") os.write( f, "exec %s -c 'import sys; sys.exit(47)'\n" % sys.executable) os.close(f) os.chmod(fname, 0700) p = subprocess.Popen(fname) p.wait() os.remove(fname) self.assertEqual(p.returncode, 47)
def test_stdin_fileobj(self): # stdin is set to open file object tf = tempfile.TemporaryFile() tf.write("pear") tf.seek(0) p = subprocess.Popen([ sys.executable, "-c", 'import sys; sys.exit(sys.stdin.read() == "pear")' ], stdin=tf) p.wait() self.assertEqual(p.returncode, 1)
def test_stderr_filedes(self): # stderr is set to open file descriptor tf = tempfile.TemporaryFile() d = tf.fileno() p = subprocess.Popen([ sys.executable, "-c", 'import sys; sys.stderr.write("strawberry")' ], stderr=d) p.wait() os.lseek(d, 0, 0) self.assertEqual(remove_stderr_debug_decorations(os.read(d, 1024)), "strawberry")
def test_stdout_stderr_pipe(self): # capture stdout and stderr to the same pipe p = subprocess.Popen([sys.executable, "-c", 'import sys;' \ 'sys.stdout.write("apple");' \ 'sys.stdout.flush();' \ 'sys.stderr.write("orange")'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) output = p.stdout.read() stripped = remove_stderr_debug_decorations(output) self.assertEqual(stripped, "appleorange")
def test_writes_before_communicate(self): # stdin.write before communicate() p = subprocess.Popen([sys.executable, "-c", 'import sys,os;' \ 'sys.stdout.write(sys.stdin.read())'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) p.stdin.write("banana") (stdout, stderr) = p.communicate("split") self.assertEqual(stdout, "bananasplit") self.assertEqual(remove_stderr_debug_decorations(stderr), "")
def compile(cls, sources, out): log.red("Compiling %s" % (cls.ALIAS or cls.OUTPUT)) oldcwd = os.getcwd() os.chdir(cls.SRC_DIR) compiler = subprocess.Popen([cls.get_COMPILER()] + cls.FLAGS + ['/out:%s' % out] + sources, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = compiler.communicate() retval = compiler.wait() assert retval == 0, 'Failed to compile %s: the compiler said:\n %s' % (cls.OUTPUT, stderr) if cls.ALIAS is not None: alias = cls._filename(cls.ALIAS) shutil.copy(out, alias) os.chdir(oldcwd)
def _exec_helper(self, helper, args, msg, timeout=None): args = [helper] + args if timeout and not sys.platform.startswith('win'): import os from pypy.tool import autopath watchdog = os.path.join(autopath.pypydir, 'tool', 'watchdog.py') args[:0] = [sys.executable, watchdog, str(float(timeout))] proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = proc.communicate() retval = proc.wait() assert retval == 0, msg % (args[0], stdout, stderr)
def test_stdin_filedes(self): # stdin is set to open file descriptor tf = tempfile.TemporaryFile() d = tf.fileno() os.write(d, "pear") os.lseek(d, 0, 0) p = subprocess.Popen([ sys.executable, "-c", 'import sys; sys.exit(sys.stdin.read() == "pear")' ], stdin=d) p.wait() self.assertEqual(p.returncode, 1)
def __init__(self, args, executable=None): """'args' should a sequence of argument for the subprocess, starting with the full path of the executable. """ self.popen = subprocess.Popen(args, executable=executable, bufsize=-1, stdin=subprocess.PIPE, stdout=subprocess.PIPE, close_fds=True, env={}) self.popenlock = None self.currenttimeout = None self.currentlyidlefrom = None
def test_poll(self): p = subprocess.Popen( [sys.executable, "-c", "import time; time.sleep(1)"]) count = 0 while p.poll() is None: time.sleep(0.1) count += 1 # We expect that the poll loop probably went around about 10 times, # but, based on system scheduling we can't control, it's possible # poll() never returned None. It "should be" very rare that it # didn't go around at least twice. self.assert_(count >= 2) # Subsequent invocations should just return the returncode self.assertEqual(p.poll(), 0)
def test_cwd(self): tmpdir = os.getenv("TEMP", "/tmp") # We cannot use os.path.realpath to canonicalize the path, # since it doesn't expand Tru64 {memb} strings. See bug 1063571. cwd = os.getcwd() os.chdir(tmpdir) tmpdir = os.getcwd() os.chdir(cwd) p = subprocess.Popen([sys.executable, "-c", 'import sys,os;' \ 'sys.stdout.write(os.getcwd())'], stdout=subprocess.PIPE, cwd=tmpdir) normcase = os.path.normcase self.assertEqual(normcase(p.stdout.read()), normcase(tmpdir))