Back to index

python3.2  3.2.2
Public Member Functions
test.test_subprocess.ContextManagerTests Class Reference
Inheritance diagram for test.test_subprocess.ContextManagerTests:
Inheritance graph
[legend]
Collaboration diagram for test.test_subprocess.ContextManagerTests:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def test_pipe
def test_returncode
def test_communicate_stdin
def test_invalid_args
def test_call_seq
def test_check_call_zero
def test_check_call_nonzero
def test_check_output
def test_check_output_nonzero
def test_check_output_stderr
def test_check_output_stdout_arg
def test_call_kwargs
def test_stdin_none
def test_stdout_none
def test_stderr_none
def test_executable_with_cwd
def test_executable_without_cwd
def test_stdin_pipe
def test_stdin_filedes
def test_stdin_fileobj
def test_stdout_pipe
def test_stdout_filedes
def test_stdout_fileobj
def test_stderr_pipe
def test_stderr_filedes
def test_stderr_fileobj
def test_stdout_stderr_pipe
def test_stdout_stderr_file
def test_stdout_filedes_of_stdout
def test_cwd
def test_env
def test_empty_env
def test_communicate_stdout
def test_communicate_stderr
def test_communicate
def test_communicate_pipe_fd_leak
def test_communicate_returns
def test_communicate_pipe_buf
def test_writes_before_communicate
def test_universal_newlines
def test_universal_newlines_communicate
def test_universal_newlines_communicate_stdin
def test_no_leaking
def test_list2cmdline
def test_poll
def test_wait
def test_invalid_bufsize
def test_bufsize_is_none
def test_leaking_fds_on_error
def test_issue8780
def test_handles_closed_on_exception
def test_communicate_epipe
def test_communicate_epipe_only_stdin
def test_communicate_eintr
def setUp
def tearDown
def assertStderrEqual

Detailed Description

Definition at line 1626 of file test_subprocess.py.


Member Function Documentation

def test.test_subprocess.BaseTestCase.assertStderrEqual (   self,
  stderr,
  expected,
  msg = None 
) [inherited]

Definition at line 56 of file test_subprocess.py.

00056 
00057     def assertStderrEqual(self, stderr, expected, msg=None):
00058         # In a debug build, stuff like "[6580 refs]" is printed to stderr at
00059         # shutdown time.  That frustrates tests trying to check stderr produced
00060         # from a spawned Python process.
00061         actual = support.strip_python_stderr(stderr)
00062         self.assertEqual(actual, expected, msg)
00063 

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_subprocess.BaseTestCase.setUp (   self) [inherited]

Reimplemented in test.test_subprocess.CommandsWithSpaces, and test.test_subprocess.ProcessTestCaseNoPoll.

Definition at line 45 of file test_subprocess.py.

00045 
00046     def setUp(self):
00047         # Try to minimize the number of children we have so this test
00048         # doesn't crash on some buildbots (Alphas in particular).
00049         support.reap_children()

Here is the caller graph for this function:

def test.test_subprocess.BaseTestCase.tearDown (   self) [inherited]

Reimplemented in test.test_subprocess.CommandsWithSpaces, and test.test_subprocess.ProcessTestCaseNoPoll.

Definition at line 50 of file test_subprocess.py.

00050 
00051     def tearDown(self):
00052         for inst in subprocess._active:
00053             inst.wait()
00054         subprocess._cleanup()
00055         self.assertFalse(subprocess._active, "subprocess._active not empty")

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 635 of file test_subprocess.py.

00635 
00636     def test_bufsize_is_none(self):
00637         # bufsize=None should be the same as bufsize=0.
00638         p = subprocess.Popen([sys.executable, "-c", "pass"], None)
00639         self.assertEqual(p.wait(), 0)
00640         # Again with keyword arg
00641         p = subprocess.Popen([sys.executable, "-c", "pass"], bufsize=None)
00642         self.assertEqual(p.wait(), 0)

Here is the call graph for this function:

Definition at line 114 of file test_subprocess.py.

00114 
00115     def test_call_kwargs(self):
00116         # call() function with keyword args
00117         newenv = os.environ.copy()
00118         newenv["FRUIT"] = "banana"
00119         rc = subprocess.call([sys.executable, "-c",
00120                               'import sys, os;'
00121                               'sys.exit(os.getenv("FRUIT")=="banana")'],
00122                              env=newenv)
00123         self.assertEqual(rc, 1)

Here is the call graph for this function:

Definition at line 66 of file test_subprocess.py.

00066 
00067     def test_call_seq(self):
00068         # call() function with sequence argument
00069         rc = subprocess.call([sys.executable, "-c",
00070                               "import sys; sys.exit(47)"])
00071         self.assertEqual(rc, 47)

Here is the call graph for this function:

Definition at line 78 of file test_subprocess.py.

00078 
00079     def test_check_call_nonzero(self):
00080         # check_call() function with non-zero return code
00081         with self.assertRaises(subprocess.CalledProcessError) as c:
00082             subprocess.check_call([sys.executable, "-c",
00083                                    "import sys; sys.exit(47)"])
00084         self.assertEqual(c.exception.returncode, 47)

Here is the call graph for this function:

Definition at line 72 of file test_subprocess.py.

00072 
00073     def test_check_call_zero(self):
00074         # check_call() function with zero return code
00075         rc = subprocess.check_call([sys.executable, "-c",
00076                                     "import sys; sys.exit(0)"])
00077         self.assertEqual(rc, 0)

Here is the call graph for this function:

Definition at line 85 of file test_subprocess.py.

00085 
00086     def test_check_output(self):
00087         # check_output() function with zero return code
00088         output = subprocess.check_output(
00089                 [sys.executable, "-c", "print('BDFL')"])
00090         self.assertIn(b'BDFL', output)

Here is the call graph for this function:

Definition at line 91 of file test_subprocess.py.

00091 
00092     def test_check_output_nonzero(self):
00093         # check_call() function with non-zero return code
00094         with self.assertRaises(subprocess.CalledProcessError) as c:
00095             subprocess.check_output(
00096                     [sys.executable, "-c", "import sys; sys.exit(5)"])
00097         self.assertEqual(c.exception.returncode, 5)

Here is the call graph for this function:

Definition at line 98 of file test_subprocess.py.

00098 
00099     def test_check_output_stderr(self):
00100         # check_output() function stderr redirected to stdout
00101         output = subprocess.check_output(
00102                 [sys.executable, "-c", "import sys; sys.stderr.write('BDFL')"],
00103                 stderr=subprocess.STDOUT)
00104         self.assertIn(b'BDFL', output)

Here is the call graph for this function:

Definition at line 105 of file test_subprocess.py.

00105 
00106     def test_check_output_stdout_arg(self):
00107         # check_output() function stderr redirected to stdout
00108         with self.assertRaises(ValueError) as c:
00109             output = subprocess.check_output(
00110                     [sys.executable, "-c", "print('will not be run')"],
00111                     stdout=sys.stdout)
00112             self.fail("Expected ValueError when stdout arg supplied.")
00113         self.assertIn('stdout', c.exception.args[0])

Here is the call graph for this function:

Definition at line 386 of file test_subprocess.py.

00386 
00387     def test_communicate(self):
00388         p = subprocess.Popen([sys.executable, "-c",
00389                               'import sys,os;'
00390                               'sys.stderr.write("pineapple");'
00391                               'sys.stdout.write(sys.stdin.read())'],
00392                              stdin=subprocess.PIPE,
00393                              stdout=subprocess.PIPE,
00394                              stderr=subprocess.PIPE)
00395         self.addCleanup(p.stdout.close)
00396         self.addCleanup(p.stderr.close)
00397         self.addCleanup(p.stdin.close)
00398         (stdout, stderr) = p.communicate(b"banana")
00399         self.assertEqual(stdout, b"banana")
00400         self.assertStderrEqual(stderr, b"pineapple")

Here is the call graph for this function:

Definition at line 711 of file test_subprocess.py.

00711 
00712     def test_communicate_eintr(self):
00713         # Issue #12493: communicate() should handle EINTR
00714         def handler(signum, frame):
00715             pass
00716         old_handler = signal.signal(signal.SIGALRM, handler)
00717         self.addCleanup(signal.signal, signal.SIGALRM, old_handler)
00718 
00719         # the process is running for 2 seconds
00720         args = [sys.executable, "-c", 'import time; time.sleep(2)']
00721         for stream in ('stdout', 'stderr'):
00722             kw = {stream: subprocess.PIPE}
00723             with subprocess.Popen(args, **kw) as process:
00724                 signal.alarm(1)
00725                 # communicate() will be interrupted by SIGALRM
00726                 process.communicate()
00727 
00728 
# context manager

Definition at line 690 of file test_subprocess.py.

00690 
00691     def test_communicate_epipe(self):
00692         # Issue 10963: communicate() should hide EPIPE
00693         p = subprocess.Popen([sys.executable, "-c", 'pass'],
00694                              stdin=subprocess.PIPE,
00695                              stdout=subprocess.PIPE,
00696                              stderr=subprocess.PIPE)
00697         self.addCleanup(p.stdout.close)
00698         self.addCleanup(p.stderr.close)
00699         self.addCleanup(p.stdin.close)
00700         p.communicate(b"x" * 2**20)

Here is the call graph for this function:

Definition at line 701 of file test_subprocess.py.

00701 
00702     def test_communicate_epipe_only_stdin(self):
00703         # Issue 10963: communicate() should hide EPIPE
00704         p = subprocess.Popen([sys.executable, "-c", 'pass'],
00705                              stdin=subprocess.PIPE)
00706         self.addCleanup(p.stdin.close)
00707         time.sleep(2)
00708         p.communicate(b"x" * 2**20)

Here is the call graph for this function:

Definition at line 432 of file test_subprocess.py.

00432 
00433     def test_communicate_pipe_buf(self):
00434         # communicate() with writes larger than pipe_buf
00435         # This test will probably deadlock rather than fail, if
00436         # communicate() does not work properly.
00437         x, y = os.pipe()
00438         if mswindows:
00439             pipe_buf = 512
00440         else:
00441             pipe_buf = os.fpathconf(x, "PC_PIPE_BUF")
00442         os.close(x)
00443         os.close(y)
00444         p = subprocess.Popen([sys.executable, "-c",
00445                               'import sys,os;'
00446                               'sys.stdout.write(sys.stdin.read(47));'
00447                               'sys.stderr.write("xyz"*%d);'
00448                               'sys.stdout.write(sys.stdin.read())' % pipe_buf],
00449                              stdin=subprocess.PIPE,
00450                              stdout=subprocess.PIPE,
00451                              stderr=subprocess.PIPE)
00452         self.addCleanup(p.stdout.close)
00453         self.addCleanup(p.stderr.close)
00454         self.addCleanup(p.stdin.close)
00455         string_to_write = b"abc"*pipe_buf
00456         (stdout, stderr) = p.communicate(string_to_write)
00457         self.assertEqual(stdout, string_to_write)

Here is the call graph for this function:

Definition at line 402 of file test_subprocess.py.

00402 
00403     def test_communicate_pipe_fd_leak(self):
00404         for stdin_pipe in (False, True):
00405             for stdout_pipe in (False, True):
00406                 for stderr_pipe in (False, True):
00407                     options = {}
00408                     if stdin_pipe:
00409                         options['stdin'] = subprocess.PIPE
00410                     if stdout_pipe:
00411                         options['stdout'] = subprocess.PIPE
00412                     if stderr_pipe:
00413                         options['stderr'] = subprocess.PIPE
00414                     if not options:
00415                         continue
00416                     p = subprocess.Popen((sys.executable, "-c", "pass"), **options)
00417                     p.communicate()
00418                     if p.stdin is not None:
00419                         self.assertTrue(p.stdin.closed)
00420                     if p.stdout is not None:
00421                         self.assertTrue(p.stdout.closed)
00422                     if p.stderr is not None:
00423                         self.assertTrue(p.stderr.closed)

Here is the call graph for this function:

Definition at line 424 of file test_subprocess.py.

00424 
00425     def test_communicate_returns(self):
00426         # communicate() should return None if no redirection is active
00427         p = subprocess.Popen([sys.executable, "-c",
00428                               "import sys; sys.exit(47)"])
00429         (stdout, stderr) = p.communicate()
00430         self.assertEqual(stdout, None)
00431         self.assertEqual(stderr, None)

Here is the call graph for this function:

Definition at line 378 of file test_subprocess.py.

00378 
00379     def test_communicate_stderr(self):
00380         p = subprocess.Popen([sys.executable, "-c",
00381                               'import sys; sys.stderr.write("pineapple")'],
00382                              stderr=subprocess.PIPE)
00383         (stdout, stderr) = p.communicate()
00384         self.assertEqual(stdout, None)
00385         self.assertStderrEqual(stderr, b"pineapple")

Here is the call graph for this function:

Reimplemented from test.test_subprocess.ProcessTestCase.

Definition at line 1648 of file test_subprocess.py.

01648 
01649     def test_communicate_stdin(self):
01650         with subprocess.Popen([sys.executable, "-c",
01651                               "import sys;"
01652                               "sys.exit(sys.stdin.read() == 'context')"],
01653                              stdin=subprocess.PIPE) as proc:
01654             proc.communicate(b"context")
01655             self.assertEqual(proc.returncode, 1)

Here is the call graph for this function:

Definition at line 370 of file test_subprocess.py.

00370 
00371     def test_communicate_stdout(self):
00372         p = subprocess.Popen([sys.executable, "-c",
00373                               'import sys; sys.stdout.write("pineapple")'],
00374                              stdout=subprocess.PIPE)
00375         (stdout, stderr) = p.communicate()
00376         self.assertEqual(stdout, b"pineapple")
00377         self.assertEqual(stderr, None)

Here is the call graph for this function:

Definition at line 313 of file test_subprocess.py.

00313 
00314     def test_cwd(self):
00315         tmpdir = tempfile.gettempdir()
00316         # We cannot use os.path.realpath to canonicalize the path,
00317         # since it doesn't expand Tru64 {memb} strings. See bug 1063571.
00318         cwd = os.getcwd()
00319         os.chdir(tmpdir)
00320         tmpdir = os.getcwd()
00321         os.chdir(cwd)
00322         p = subprocess.Popen([sys.executable, "-c",
00323                               'import sys,os;'
00324                               'sys.stdout.write(os.getcwd())'],
00325                              stdout=subprocess.PIPE,
00326                              cwd=tmpdir)
00327         self.addCleanup(p.stdout.close)
00328         normcase = os.path.normcase
00329         self.assertEqual(normcase(p.stdout.read().decode("utf-8")),
00330                          normcase(tmpdir))

Here is the call graph for this function:

Definition at line 349 of file test_subprocess.py.

00349 
00350     def test_empty_env(self):
00351         with subprocess.Popen([sys.executable, "-c",
00352                                'import os; '
00353                                'print(list(os.environ.keys()))'],
00354                               stdout=subprocess.PIPE,
00355                               env={}) as p:
00356             stdout, stderr = p.communicate()
00357             self.assertIn(stdout.strip(),
00358                 (b"[]",
00359                  # Mac OS X adds __CF_USER_TEXT_ENCODING variable to an empty
00360                  # environment
00361                  b"['__CF_USER_TEXT_ENCODING']"))

Here is the call graph for this function:

Definition at line 331 of file test_subprocess.py.

00331 
00332     def test_env(self):
00333         newenv = os.environ.copy()
00334         newenv["FRUIT"] = "orange"
00335         with subprocess.Popen([sys.executable, "-c",
00336                                'import sys,os;'
00337                                'sys.stdout.write(os.getenv("FRUIT"))'],
00338                               stdout=subprocess.PIPE,
00339                               env=newenv) as p:
00340             stdout, stderr = p.communicate()
00341             self.assertEqual(stdout, b"orange")

Here is the call graph for this function:

Definition at line 164 of file test_subprocess.py.

00164 
00165     def test_executable_with_cwd(self):
00166         python_dir = os.path.dirname(os.path.realpath(sys.executable))
00167         p = subprocess.Popen(["somethingyoudonthave", "-c",
00168                               "import sys; sys.exit(47)"],
00169                              executable=sys.executable, cwd=python_dir)
00170         p.wait()
00171         self.assertEqual(p.returncode, 47)

Here is the call graph for this function:

Definition at line 174 of file test_subprocess.py.

00174 
00175     def test_executable_without_cwd(self):
00176         # For a normal installation, it should work without 'cwd'
00177         # argument.  For test runs in the build directory, see #7774.
00178         p = subprocess.Popen(["somethingyoudonthave", "-c",
00179                               "import sys; sys.exit(47)"],
00180                              executable=sys.executable)
00181         p.wait()
00182         self.assertEqual(p.returncode, 47)

Here is the call graph for this function:

Definition at line 670 of file test_subprocess.py.

00670 
00671     def test_handles_closed_on_exception(self):
00672         # If CreateProcess exits with an error, ensure the
00673         # duplicate output handles are released
00674         ifhandle, ifname = mkstemp()
00675         ofhandle, ofname = mkstemp()
00676         efhandle, efname = mkstemp()
00677         try:
00678             subprocess.Popen (["*"], stdin=ifhandle, stdout=ofhandle,
00679               stderr=efhandle)
00680         except OSError:
00681             os.close(ifhandle)
00682             os.remove(ifname)
00683             os.close(ofhandle)
00684             os.remove(ofname)
00685             os.close(efhandle)
00686             os.remove(efname)
00687         self.assertFalse(os.path.exists(ifname))
00688         self.assertFalse(os.path.exists(ofname))
00689         self.assertFalse(os.path.exists(efname))

Here is the call graph for this function:

Reimplemented from test.test_subprocess.ProcessTestCase.

Definition at line 1656 of file test_subprocess.py.

01656 
01657     def test_invalid_args(self):
01658         with self.assertRaises(EnvironmentError) as c:
01659             with subprocess.Popen(['nonexisting_i_hope'],
01660                                   stdout=subprocess.PIPE,
01661                                   stderr=subprocess.PIPE) as proc:
01662                 pass
01663 
01664             if c.exception.errno != errno.ENOENT:  # ignore "no such file"
01665                 raise c.exception
01666 

Here is the call graph for this function:

Definition at line 629 of file test_subprocess.py.

00629 
00630     def test_invalid_bufsize(self):
00631         # an invalid type of the bufsize argument should raise
00632         # TypeError.
00633         with self.assertRaises(TypeError):
00634             subprocess.Popen([sys.executable, "-c", "pass"], "orange")

Here is the call graph for this function:

Definition at line 659 of file test_subprocess.py.

00659 
00660     def test_issue8780(self):
00661         # Ensure that stdout is inherited from the parent
00662         # if stdout=PIPE is not used
00663         code = ';'.join((
00664             'import subprocess, sys',
00665             'retcode = subprocess.call('
00666                 "[sys.executable, '-c', 'print(\"Hello World!\")'])",
00667             'assert retcode == 0'))
00668         output = subprocess.check_output([sys.executable, '-c', code])
00669         self.assertTrue(output.startswith(b'Hello World!'), ascii(output))

Here is the call graph for this function:

Definition at line 643 of file test_subprocess.py.

00643 
00644     def test_leaking_fds_on_error(self):
00645         # see bug #5179: Popen leaks file descriptors to PIPEs if
00646         # the child fails to execute; this will eventually exhaust
00647         # the maximum number of open fds. 1024 seems a very common
00648         # value for that limit, but Windows has 2048, so we loop
00649         # 1024 times (each call leaked two fds).
00650         for i in range(1024):
00651             # Windows raises IOError.  Others raise OSError.
00652             with self.assertRaises(EnvironmentError) as c:
00653                 subprocess.Popen(['nonexisting_i_hope'],
00654                                  stdout=subprocess.PIPE,
00655                                  stderr=subprocess.PIPE)
00656             # ignore errors that indicate the command was not found
00657             if c.exception.errno not in (errno.ENOENT, errno.EACCES):
00658                 raise c.exception

Here is the call graph for this function:

Definition at line 586 of file test_subprocess.py.

00586 
00587     def test_list2cmdline(self):
00588         self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
00589                          '"a b c" d e')
00590         self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
00591                          'ab\\"c \\ d')
00592         self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']),
00593                          'ab\\"c " \\\\" d')
00594         self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
00595                          'a\\\\\\b "de fg" h')
00596         self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
00597                          'a\\\\\\"b c d')
00598         self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
00599                          '"a\\\\b c" d e')
00600         self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
00601                          '"a\\\\b\\ c" d e')
00602         self.assertEqual(subprocess.list2cmdline(['ab', '']),
00603                          'ab ""')
00604 

Here is the call graph for this function:

Definition at line 547 of file test_subprocess.py.

00547 
00548     def test_no_leaking(self):
00549         # Make sure we leak no resources
00550         if not mswindows:
00551             max_handles = 1026 # too much for most UNIX systems
00552         else:
00553             max_handles = 2050 # too much for (at least some) Windows setups
00554         handles = []
00555         tmpdir = tempfile.mkdtemp()
00556         try:
00557             for i in range(max_handles):
00558                 try:
00559                     tmpfile = os.path.join(tmpdir, support.TESTFN)
00560                     handles.append(os.open(tmpfile, os.O_WRONLY|os.O_CREAT))
00561                 except OSError as e:
00562                     if e.errno != errno.EMFILE:
00563                         raise
00564                     break
00565             else:
00566                 self.skipTest("failed to reach the file descriptor limit "
00567                     "(tried %d)" % max_handles)
00568             # Close a couple of them (should be enough for a subprocess)
00569             for i in range(10):
00570                 os.close(handles.pop())
00571             # Loop creating some subprocesses. If one of them leaks some fds,
00572             # the next loop iteration will fail by reaching the max fd limit.
00573             for i in range(15):
00574                 p = subprocess.Popen([sys.executable, "-c",
00575                                       "import sys;"
00576                                       "sys.stdout.write(sys.stdin.read())"],
00577                                      stdin=subprocess.PIPE,
00578                                      stdout=subprocess.PIPE,
00579                                      stderr=subprocess.PIPE)
00580                 data = p.communicate(b"lime")[0]
00581                 self.assertEqual(data, b"lime")
00582         finally:
00583             for h in handles:
00584                 os.close(h)
00585             shutil.rmtree(tmpdir)

Here is the call graph for this function:

Definition at line 1628 of file test_subprocess.py.

01628 
01629     def test_pipe(self):
01630         with subprocess.Popen([sys.executable, "-c",
01631                                "import sys;"
01632                                "sys.stdout.write('stdout');"
01633                                "sys.stderr.write('stderr');"],
01634                               stdout=subprocess.PIPE,
01635                               stderr=subprocess.PIPE) as proc:
01636             self.assertEqual(proc.stdout.read(), b"stdout")
01637             self.assertStderrEqual(proc.stderr.read(), b"stderr")
01638 
01639         self.assertTrue(proc.stdout.closed)
01640         self.assertTrue(proc.stderr.closed)

Here is the call graph for this function:

Definition at line 605 of file test_subprocess.py.

00605 
00606     def test_poll(self):
00607         p = subprocess.Popen([sys.executable,
00608                           "-c", "import time; time.sleep(1)"])
00609         count = 0
00610         while p.poll() is None:
00611             time.sleep(0.1)
00612             count += 1
00613         # We expect that the poll loop probably went around about 10 times,
00614         # but, based on system scheduling we can't control, it's possible
00615         # poll() never returned None.  It "should be" very rare that it
00616         # didn't go around at least twice.
00617         self.assertGreaterEqual(count, 2)
00618         # Subsequent invocations should just return the returncode
00619         self.assertEqual(p.poll(), 0)
00620 

Here is the call graph for this function:

Definition at line 1641 of file test_subprocess.py.

01641 
01642     def test_returncode(self):
01643         with subprocess.Popen([sys.executable, "-c",
01644                                "import sys; sys.exit(100)"]) as proc:
01645             pass
01646         # __exit__ calls wait(), so the returncode should be set
01647         self.assertEqual(proc.returncode, 100)

Here is the call graph for this function:

Definition at line 257 of file test_subprocess.py.

00257 
00258     def test_stderr_filedes(self):
00259         # stderr is set to open file descriptor
00260         tf = tempfile.TemporaryFile()
00261         self.addCleanup(tf.close)
00262         d = tf.fileno()
00263         p = subprocess.Popen([sys.executable, "-c",
00264                           'import sys; sys.stderr.write("strawberry")'],
00265                          stderr=d)
00266         p.wait()
00267         os.lseek(d, 0, 0)
00268         self.assertStderrEqual(os.read(d, 1024), b"strawberry")

Here is the call graph for this function:

Definition at line 269 of file test_subprocess.py.

00269 
00270     def test_stderr_fileobj(self):
00271         # stderr is set to open file object
00272         tf = tempfile.TemporaryFile()
00273         self.addCleanup(tf.close)
00274         p = subprocess.Popen([sys.executable, "-c",
00275                           'import sys; sys.stderr.write("strawberry")'],
00276                          stderr=tf)
00277         p.wait()
00278         tf.seek(0)
00279         self.assertStderrEqual(tf.read(), b"strawberry")

Here is the call graph for this function:

Definition at line 155 of file test_subprocess.py.

00155 
00156     def test_stderr_none(self):
00157         # .stderr is None when not redirected
00158         p = subprocess.Popen([sys.executable, "-c", 'print("banana")'],
00159                          stdin=subprocess.PIPE, stdout=subprocess.PIPE)
00160         self.addCleanup(p.stdout.close)
00161         self.addCleanup(p.stdin.close)
00162         p.wait()
00163         self.assertEqual(p.stderr, None)

Here is the call graph for this function:

Definition at line 249 of file test_subprocess.py.

00249 
00250     def test_stderr_pipe(self):
00251         # stderr redirection
00252         p = subprocess.Popen([sys.executable, "-c",
00253                           'import sys; sys.stderr.write("strawberry")'],
00254                          stderr=subprocess.PIPE)
00255         self.addCleanup(p.stderr.close)
00256         self.assertStderrEqual(p.stderr.read(), b"strawberry")

Here is the call graph for this function:

Definition at line 193 of file test_subprocess.py.

00193 
00194     def test_stdin_filedes(self):
00195         # stdin is set to open file descriptor
00196         tf = tempfile.TemporaryFile()
00197         self.addCleanup(tf.close)
00198         d = tf.fileno()
00199         os.write(d, b"pear")
00200         os.lseek(d, 0, 0)
00201         p = subprocess.Popen([sys.executable, "-c",
00202                          'import sys; sys.exit(sys.stdin.read() == "pear")'],
00203                          stdin=d)
00204         p.wait()
00205         self.assertEqual(p.returncode, 1)

Here is the call graph for this function:

Definition at line 206 of file test_subprocess.py.

00206 
00207     def test_stdin_fileobj(self):
00208         # stdin is set to open file object
00209         tf = tempfile.TemporaryFile()
00210         self.addCleanup(tf.close)
00211         tf.write(b"pear")
00212         tf.seek(0)
00213         p = subprocess.Popen([sys.executable, "-c",
00214                          'import sys; sys.exit(sys.stdin.read() == "pear")'],
00215                          stdin=tf)
00216         p.wait()
00217         self.assertEqual(p.returncode, 1)

Here is the call graph for this function:

Definition at line 134 of file test_subprocess.py.

00134 
00135     def test_stdin_none(self):
00136         # .stdin is None when not redirected
00137         p = subprocess.Popen([sys.executable, "-c", 'print("banana")'],
00138                          stdout=subprocess.PIPE, stderr=subprocess.PIPE)
00139         self.addCleanup(p.stdout.close)
00140         self.addCleanup(p.stderr.close)
00141         p.wait()
00142         self.assertEqual(p.stdin, None)

Here is the call graph for this function:

Definition at line 183 of file test_subprocess.py.

00183 
00184     def test_stdin_pipe(self):
00185         # stdin redirection
00186         p = subprocess.Popen([sys.executable, "-c",
00187                          'import sys; sys.exit(sys.stdin.read() == "pear")'],
00188                         stdin=subprocess.PIPE)
00189         p.stdin.write(b"pear")
00190         p.stdin.close()
00191         p.wait()
00192         self.assertEqual(p.returncode, 1)

Here is the call graph for this function:

Definition at line 226 of file test_subprocess.py.

00226 
00227     def test_stdout_filedes(self):
00228         # stdout is set to open file descriptor
00229         tf = tempfile.TemporaryFile()
00230         self.addCleanup(tf.close)
00231         d = tf.fileno()
00232         p = subprocess.Popen([sys.executable, "-c",
00233                           'import sys; sys.stdout.write("orange")'],
00234                          stdout=d)
00235         p.wait()
00236         os.lseek(d, 0, 0)
00237         self.assertEqual(os.read(d, 1024), b"orange")

Here is the call graph for this function:

Definition at line 307 of file test_subprocess.py.

00307 
00308     def test_stdout_filedes_of_stdout(self):
00309         # stdout is set to 1 (#1531862).
00310         cmd = r"import sys, os; sys.exit(os.write(sys.stdout.fileno(), b'.\n'))"
00311         rc = subprocess.call([sys.executable, "-c", cmd], stdout=1)
00312         self.assertEqual(rc, 2)

Here is the call graph for this function:

Definition at line 238 of file test_subprocess.py.

00238 
00239     def test_stdout_fileobj(self):
00240         # stdout is set to open file object
00241         tf = tempfile.TemporaryFile()
00242         self.addCleanup(tf.close)
00243         p = subprocess.Popen([sys.executable, "-c",
00244                           'import sys; sys.stdout.write("orange")'],
00245                          stdout=tf)
00246         p.wait()
00247         tf.seek(0)
00248         self.assertEqual(tf.read(), b"orange")

Here is the call graph for this function:

Definition at line 143 of file test_subprocess.py.

00143 
00144     def test_stdout_none(self):
00145         # .stdout is None when not redirected
00146         p = subprocess.Popen([sys.executable, "-c",
00147                              'print("    this bit of output is from a '
00148                              'test of stdout in a different '
00149                              'process ...")'],
00150                              stdin=subprocess.PIPE, stderr=subprocess.PIPE)
00151         self.addCleanup(p.stdin.close)
00152         self.addCleanup(p.stderr.close)
00153         p.wait()
00154         self.assertEqual(p.stdout, None)

Here is the call graph for this function:

Definition at line 218 of file test_subprocess.py.

00218 
00219     def test_stdout_pipe(self):
00220         # stdout redirection
00221         p = subprocess.Popen([sys.executable, "-c",
00222                           'import sys; sys.stdout.write("orange")'],
00223                          stdout=subprocess.PIPE)
00224         self.addCleanup(p.stdout.close)
00225         self.assertEqual(p.stdout.read(), b"orange")

Here is the call graph for this function:

Definition at line 292 of file test_subprocess.py.

00292 
00293     def test_stdout_stderr_file(self):
00294         # capture stdout and stderr to the same open file
00295         tf = tempfile.TemporaryFile()
00296         self.addCleanup(tf.close)
00297         p = subprocess.Popen([sys.executable, "-c",
00298                               'import sys;'
00299                               'sys.stdout.write("apple");'
00300                               'sys.stdout.flush();'
00301                               'sys.stderr.write("orange")'],
00302                              stdout=tf,
00303                              stderr=tf)
00304         p.wait()
00305         tf.seek(0)
00306         self.assertStderrEqual(tf.read(), b"appleorange")

Here is the call graph for this function:

Definition at line 280 of file test_subprocess.py.

00280 
00281     def test_stdout_stderr_pipe(self):
00282         # capture stdout and stderr to the same pipe
00283         p = subprocess.Popen([sys.executable, "-c",
00284                               'import sys;'
00285                               'sys.stdout.write("apple");'
00286                               'sys.stdout.flush();'
00287                               'sys.stderr.write("orange")'],
00288                              stdout=subprocess.PIPE,
00289                              stderr=subprocess.STDOUT)
00290         self.addCleanup(p.stdout.close)
00291         self.assertStderrEqual(p.stdout.read(), b"appleorange")

Here is the call graph for this function:

Definition at line 474 of file test_subprocess.py.

00474 
00475     def test_universal_newlines(self):
00476         p = subprocess.Popen([sys.executable, "-c",
00477                               'import sys,os;' + SETBINARY +
00478                               'sys.stdout.write(sys.stdin.readline());'
00479                               'sys.stdout.flush();'
00480                               'sys.stdout.write("line2\\n");'
00481                               'sys.stdout.flush();'
00482                               'sys.stdout.write(sys.stdin.read());'
00483                               'sys.stdout.flush();'
00484                               'sys.stdout.write("line4\\n");'
00485                               'sys.stdout.flush();'
00486                               'sys.stdout.write("line5\\r\\n");'
00487                               'sys.stdout.flush();'
00488                               'sys.stdout.write("line6\\r");'
00489                               'sys.stdout.flush();'
00490                               'sys.stdout.write("\\nline7");'
00491                               'sys.stdout.flush();'
00492                               'sys.stdout.write("\\nline8");'],
00493                              stdin=subprocess.PIPE,
00494                              stdout=subprocess.PIPE,
00495                              universal_newlines=1)
00496         p.stdin.write("line1\n")
00497         self.assertEqual(p.stdout.readline(), "line1\n")
00498         p.stdin.write("line3\n")
00499         p.stdin.close()
00500         self.addCleanup(p.stdout.close)
00501         self.assertEqual(p.stdout.readline(),
00502                          "line2\n")
00503         self.assertEqual(p.stdout.read(6),
00504                          "line3\n")
00505         self.assertEqual(p.stdout.read(),
00506                          "line4\nline5\nline6\nline7\nline8")

Here is the call graph for this function:

Definition at line 507 of file test_subprocess.py.

00507 
00508     def test_universal_newlines_communicate(self):
00509         # universal newlines through communicate()
00510         p = subprocess.Popen([sys.executable, "-c",
00511                               'import sys,os;' + SETBINARY +
00512                               'sys.stdout.write("line2\\n");'
00513                               'sys.stdout.flush();'
00514                               'sys.stdout.write("line4\\n");'
00515                               'sys.stdout.flush();'
00516                               'sys.stdout.write("line5\\r\\n");'
00517                               'sys.stdout.flush();'
00518                               'sys.stdout.write("line6\\r");'
00519                               'sys.stdout.flush();'
00520                               'sys.stdout.write("\\nline7");'
00521                               'sys.stdout.flush();'
00522                               'sys.stdout.write("\\nline8");'],
00523                              stderr=subprocess.PIPE,
00524                              stdout=subprocess.PIPE,
00525                              universal_newlines=1)
00526         self.addCleanup(p.stdout.close)
00527         self.addCleanup(p.stderr.close)
00528         # BUG: can't give a non-empty stdin because it breaks both the
00529         # select- and poll-based communicate() implementations.
00530         (stdout, stderr) = p.communicate()
00531         self.assertEqual(stdout,
00532                          "line2\nline4\nline5\nline6\nline7\nline8")

Here is the call graph for this function:

Definition at line 533 of file test_subprocess.py.

00533 
00534     def test_universal_newlines_communicate_stdin(self):
00535         # universal newlines through communicate(), with only stdin
00536         p = subprocess.Popen([sys.executable, "-c",
00537                               'import sys,os;' + SETBINARY + '''\nif True:
00538                                   s = sys.stdin.readline()
00539                                   assert s == "line1\\n", repr(s)
00540                                   s = sys.stdin.read()
00541                                   assert s == "line3\\n", repr(s)
00542                               '''],
00543                              stdin=subprocess.PIPE,
00544                              universal_newlines=1)
00545         (stdout, stderr) = p.communicate("line1\nline3\n")
00546         self.assertEqual(p.returncode, 0)

Here is the call graph for this function:

Definition at line 621 of file test_subprocess.py.

00621 
00622     def test_wait(self):
00623         p = subprocess.Popen([sys.executable,
00624                           "-c", "import time; time.sleep(2)"])
00625         self.assertEqual(p.wait(), 0)
00626         # Subsequent invocations should just return the returncode
00627         self.assertEqual(p.wait(), 0)
00628 

Here is the call graph for this function:

Definition at line 458 of file test_subprocess.py.

00458 
00459     def test_writes_before_communicate(self):
00460         # stdin.write before communicate()
00461         p = subprocess.Popen([sys.executable, "-c",
00462                               'import sys,os;'
00463                               'sys.stdout.write(sys.stdin.read())'],
00464                              stdin=subprocess.PIPE,
00465                              stdout=subprocess.PIPE,
00466                              stderr=subprocess.PIPE)
00467         self.addCleanup(p.stdout.close)
00468         self.addCleanup(p.stderr.close)
00469         self.addCleanup(p.stdin.close)
00470         p.stdin.write(b"banana")
00471         (stdout, stderr) = p.communicate(b"split")
00472         self.assertEqual(stdout, b"bananasplit")
00473         self.assertStderrEqual(stderr, b"")

Here is the call graph for this function:


The documentation for this class was generated from the following file: