Пример #1
0
 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)
Пример #2
0
 def test_communicate_stdout(self):
     p = subprocess.Popen([sys.executable, "-c",
                           'import sys; sys.stdout.write("pineapple")'],
                          stdout=subprocess.PIPE)
     (stdout, stderr) = p.communicate()
     self.assertEqual(stdout, "pineapple")
     self.assertEqual(stderr, None)
Пример #3
0
 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(file, '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")
Пример #4
0
 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")
Пример #5
0
 def test_stdin_none(self):
     # .stdin is None when not redirected
     p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
                          stdout=subprocess.PIPE,
                          stderr=subprocess.PIPE)
     p.wait()
     self.assertEqual(p.stdin, None)
Пример #6
0
 def test_communicate_stdin(self):
     p = subprocess.Popen([
         sys.executable, "-c",
         'import sys; sys.exit(sys.stdin.read() == "pear")'
     ],
                          stdin=subprocess.PIPE)
     p.communicate("pear")
     self.assertEqual(p.returncode, 1)
Пример #7
0
 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)
Пример #8
0
 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")
Пример #9
0
 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")
Пример #10
0
 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)
Пример #11
0
 def test_communicate_stderr(self):
     p = subprocess.Popen([sys.executable, "-c",
                           'import sys; sys.stderr.write("pineapple")'],
                          stderr=subprocess.PIPE)
     (stdout, stderr) = p.communicate()
     self.assertEqual(stdout, None)
     # When running with a pydebug build, the # of references is outputted
     # to stderr, so just check if stderr at least started with "pinapple"
     self.assertTrue(stderr.startswith("pineapple"))
Пример #12
0
 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")
Пример #13
0
 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)
Пример #14
0
 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")
Пример #15
0
 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")
Пример #16
0
 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)
Пример #17
0
 def test_run_abort(self):
     # returncode handles signal termination
     old_limit = self._suppress_core_files()
     try:
         p = subprocess.Popen(
             [sys.executable, "-c", "import os; os.abort()"])
     finally:
         self._unsuppress_core_files(old_limit)
     p.wait()
     self.assertEqual(-p.returncode, signal.SIGABRT)
Пример #18
0
 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)
Пример #19
0
 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")
Пример #20
0
 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")
Пример #21
0
 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")
Пример #22
0
 def test_communicate_pipe_fd_leak(self):
     fd_directory = '/proc/%d/fd' % os.getpid()
     num_fds_before_popen = len(os.listdir(fd_directory))
     p = subprocess.Popen([sys.executable, '-c', 'print()'],
                          stdout=subprocess.PIPE)
     p.communicate()
     num_fds_after_communicate = len(os.listdir(fd_directory))
     del p
     num_fds_after_destruction = len(os.listdir(fd_directory))
     self.assertEqual(num_fds_before_popen, num_fds_after_destruction)
     self.assertEqual(num_fds_before_popen, num_fds_after_communicate)
Пример #23
0
 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")
Пример #24
0
 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)
Пример #25
0
 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), "")
Пример #26
0
 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")
Пример #27
0
 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)
Пример #28
0
 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)
Пример #29
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))
Пример #30
0
 def test_stdout_stderr_file(self):
     # capture stdout and stderr to the same open file
     tf = tempfile.TemporaryFile()
     p = subprocess.Popen([sys.executable, "-c",
                       'import sys;' \
                       'sys.stdout.write("apple");' \
                       'sys.stdout.flush();' \
                       'sys.stderr.write("orange")'],
                      stdout=tf,
                      stderr=tf)
     p.wait()
     tf.seek(0)
     output = tf.read()
     stripped = remove_stderr_debug_decorations(output)
     self.assertEqual(stripped, "appleorange")