2004-10-12 12:26:28 -03:00
|
|
|
import unittest
|
|
|
|
from test import test_support
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import signal
|
|
|
|
import os
|
|
|
|
import tempfile
|
|
|
|
import time
|
2004-10-13 01:07:12 -03:00
|
|
|
import re
|
2004-10-12 12:26:28 -03:00
|
|
|
|
|
|
|
mswindows = (sys.platform == "win32")
|
|
|
|
|
|
|
|
#
|
|
|
|
# Depends on the following external programs: Python
|
|
|
|
#
|
|
|
|
|
|
|
|
if mswindows:
|
2004-10-12 19:19:32 -03:00
|
|
|
SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), '
|
|
|
|
'os.O_BINARY);')
|
2004-10-12 12:26:28 -03:00
|
|
|
else:
|
|
|
|
SETBINARY = ''
|
|
|
|
|
2004-10-13 01:07:12 -03:00
|
|
|
# In a debug build, stuff like "[6580 refs]" is printed to stderr at
|
|
|
|
# shutdown time. That frustrates tests trying to check stderr produced
|
|
|
|
# from a spawned Python process.
|
|
|
|
def remove_stderr_debug_decorations(stderr):
|
2004-10-14 01:16:54 -03:00
|
|
|
return re.sub(r"\[\d+ refs\]\r?\n?$", "", stderr)
|
2004-10-13 01:07:12 -03:00
|
|
|
|
2004-10-12 12:26:28 -03:00
|
|
|
class ProcessTestCase(unittest.TestCase):
|
2006-06-29 01:10:08 -03:00
|
|
|
def setUp(self):
|
2006-06-30 03:18:39 -03:00
|
|
|
# Try to minimize the number of children we have so this test
|
|
|
|
# doesn't crash on some buildbots (Alphas in particular).
|
2006-07-10 17:39:49 -03:00
|
|
|
if hasattr(test_support, "reap_children"):
|
|
|
|
test_support.reap_children()
|
2006-06-29 01:10:08 -03:00
|
|
|
|
|
|
|
def tearDown(self):
|
2006-06-30 03:18:39 -03:00
|
|
|
# Try to minimize the number of children we have so this test
|
|
|
|
# doesn't crash on some buildbots (Alphas in particular).
|
2006-07-10 17:39:49 -03:00
|
|
|
if hasattr(test_support, "reap_children"):
|
|
|
|
test_support.reap_children()
|
2006-06-29 01:10:08 -03:00
|
|
|
|
2004-10-12 12:26:28 -03:00
|
|
|
def mkstemp(self):
|
|
|
|
"""wrapper for mkstemp, calling mktemp if mkstemp is not available"""
|
|
|
|
if hasattr(tempfile, "mkstemp"):
|
|
|
|
return tempfile.mkstemp()
|
|
|
|
else:
|
|
|
|
fname = tempfile.mktemp()
|
|
|
|
return os.open(fname, os.O_RDWR|os.O_CREAT), fname
|
2004-10-12 18:51:32 -03:00
|
|
|
|
2004-10-12 12:26:28 -03:00
|
|
|
#
|
|
|
|
# Generic tests
|
|
|
|
#
|
|
|
|
def test_call_seq(self):
|
2004-10-12 19:29:54 -03:00
|
|
|
# call() function with sequence argument
|
2004-10-12 19:19:32 -03:00
|
|
|
rc = subprocess.call([sys.executable, "-c",
|
|
|
|
"import sys; sys.exit(47)"])
|
2004-10-12 12:26:28 -03:00
|
|
|
self.assertEqual(rc, 47)
|
|
|
|
|
2005-01-01 05:36:35 -04:00
|
|
|
def test_check_call_zero(self):
|
|
|
|
# check_call() function with zero return code
|
|
|
|
rc = subprocess.check_call([sys.executable, "-c",
|
|
|
|
"import sys; sys.exit(0)"])
|
|
|
|
self.assertEqual(rc, 0)
|
|
|
|
|
|
|
|
def test_check_call_nonzero(self):
|
|
|
|
# check_call() function with non-zero return code
|
|
|
|
try:
|
|
|
|
subprocess.check_call([sys.executable, "-c",
|
|
|
|
"import sys; sys.exit(47)"])
|
|
|
|
except subprocess.CalledProcessError, e:
|
2006-07-14 11:04:45 -03:00
|
|
|
self.assertEqual(e.returncode, 47)
|
2005-01-01 05:36:35 -04:00
|
|
|
else:
|
|
|
|
self.fail("Expected CalledProcessError")
|
|
|
|
|
2004-10-12 12:26:28 -03:00
|
|
|
def test_call_kwargs(self):
|
2004-10-12 19:29:54 -03:00
|
|
|
# call() function with keyword args
|
2004-10-12 12:26:28 -03:00
|
|
|
newenv = os.environ.copy()
|
|
|
|
newenv["FRUIT"] = "banana"
|
|
|
|
rc = subprocess.call([sys.executable, "-c",
|
|
|
|
'import sys, os;' \
|
|
|
|
'sys.exit(os.getenv("FRUIT")=="banana")'],
|
|
|
|
env=newenv)
|
|
|
|
self.assertEqual(rc, 1)
|
|
|
|
|
|
|
|
def test_stdin_none(self):
|
2004-10-12 19:29:54 -03:00
|
|
|
# .stdin is None when not redirected
|
2004-10-12 12:26:28 -03:00
|
|
|
p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
|
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
p.wait()
|
|
|
|
self.assertEqual(p.stdin, None)
|
|
|
|
|
|
|
|
def test_stdout_none(self):
|
2004-10-12 19:29:54 -03:00
|
|
|
# .stdout is None when not redirected
|
2004-10-13 00:43:40 -03:00
|
|
|
p = subprocess.Popen([sys.executable, "-c",
|
2004-10-13 00:29:54 -03:00
|
|
|
'print " this bit of output is from a '
|
|
|
|
'test of stdout in a different '
|
|
|
|
'process ..."'],
|
|
|
|
stdin=subprocess.PIPE, stderr=subprocess.PIPE)
|
2004-10-12 12:26:28 -03:00
|
|
|
p.wait()
|
|
|
|
self.assertEqual(p.stdout, None)
|
|
|
|
|
|
|
|
def test_stderr_none(self):
|
2004-10-12 19:29:54 -03:00
|
|
|
# .stderr is None when not redirected
|
2004-10-12 12:26:28 -03:00
|
|
|
p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
|
|
|
|
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
|
|
|
p.wait()
|
|
|
|
self.assertEqual(p.stderr, None)
|
|
|
|
|
|
|
|
def test_executable(self):
|
2004-10-12 19:19:32 -03:00
|
|
|
p = subprocess.Popen(["somethingyoudonthave",
|
|
|
|
"-c", "import sys; sys.exit(47)"],
|
|
|
|
executable=sys.executable)
|
2004-10-12 12:26:28 -03:00
|
|
|
p.wait()
|
|
|
|
self.assertEqual(p.returncode, 47)
|
|
|
|
|
|
|
|
def test_stdin_pipe(self):
|
2004-10-12 19:29:54 -03:00
|
|
|
# stdin redirection
|
2004-10-12 12:26:28 -03:00
|
|
|
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_stdin_filedes(self):
|
2004-10-12 19:29:54 -03:00
|
|
|
# stdin is set to open file descriptor
|
2004-10-12 18:51:32 -03:00
|
|
|
tf = tempfile.TemporaryFile()
|
2004-10-12 12:26:28 -03:00
|
|
|
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 test_stdin_fileobj(self):
|
2004-10-12 19:29:54 -03:00
|
|
|
# stdin is set to open file object
|
2004-10-12 12:26:28 -03:00
|
|
|
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_stdout_pipe(self):
|
2004-10-12 19:29:54 -03:00
|
|
|
# stdout redirection
|
2004-10-12 12:26:28 -03:00
|
|
|
p = subprocess.Popen([sys.executable, "-c",
|
|
|
|
'import sys; sys.stdout.write("orange")'],
|
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
self.assertEqual(p.stdout.read(), "orange")
|
|
|
|
|
|
|
|
def test_stdout_filedes(self):
|
2004-10-12 19:29:54 -03:00
|
|
|
# stdout is set to open file descriptor
|
2004-10-12 18:51:32 -03:00
|
|
|
tf = tempfile.TemporaryFile()
|
2004-10-12 12:26:28 -03:00
|
|
|
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_fileobj(self):
|
2004-10-12 19:29:54 -03:00
|
|
|
# stdout is set to open file object
|
2004-10-12 18:51:32 -03:00
|
|
|
tf = tempfile.TemporaryFile()
|
2004-10-12 12:26:28 -03:00
|
|
|
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_stderr_pipe(self):
|
2004-10-12 19:29:54 -03:00
|
|
|
# stderr redirection
|
2004-10-12 12:26:28 -03:00
|
|
|
p = subprocess.Popen([sys.executable, "-c",
|
|
|
|
'import sys; sys.stderr.write("strawberry")'],
|
|
|
|
stderr=subprocess.PIPE)
|
2004-10-13 01:07:12 -03:00
|
|
|
self.assertEqual(remove_stderr_debug_decorations(p.stderr.read()),
|
|
|
|
"strawberry")
|
2004-10-12 12:26:28 -03:00
|
|
|
|
|
|
|
def test_stderr_filedes(self):
|
2004-10-12 19:29:54 -03:00
|
|
|
# stderr is set to open file descriptor
|
2004-10-12 18:51:32 -03:00
|
|
|
tf = tempfile.TemporaryFile()
|
2004-10-12 12:26:28 -03:00
|
|
|
d = tf.fileno()
|
|
|
|
p = subprocess.Popen([sys.executable, "-c",
|
|
|
|
'import sys; sys.stderr.write("strawberry")'],
|
|
|
|
stderr=d)
|
|
|
|
p.wait()
|
|
|
|
os.lseek(d, 0, 0)
|
2004-10-13 01:07:12 -03:00
|
|
|
self.assertEqual(remove_stderr_debug_decorations(os.read(d, 1024)),
|
|
|
|
"strawberry")
|
2004-10-12 12:26:28 -03:00
|
|
|
|
|
|
|
def test_stderr_fileobj(self):
|
2004-10-12 19:29:54 -03:00
|
|
|
# stderr is set to open file object
|
2004-10-12 18:51:32 -03:00
|
|
|
tf = tempfile.TemporaryFile()
|
2004-10-12 12:26:28 -03:00
|
|
|
p = subprocess.Popen([sys.executable, "-c",
|
|
|
|
'import sys; sys.stderr.write("strawberry")'],
|
|
|
|
stderr=tf)
|
|
|
|
p.wait()
|
|
|
|
tf.seek(0)
|
2004-10-13 01:07:12 -03:00
|
|
|
self.assertEqual(remove_stderr_debug_decorations(tf.read()),
|
|
|
|
"strawberry")
|
2004-10-12 12:26:28 -03:00
|
|
|
|
|
|
|
def test_stdout_stderr_pipe(self):
|
2004-10-12 19:29:54 -03:00
|
|
|
# capture stdout and stderr to the same pipe
|
2004-10-12 12:26:28 -03:00
|
|
|
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)
|
2004-10-13 01:07:12 -03:00
|
|
|
output = p.stdout.read()
|
|
|
|
stripped = remove_stderr_debug_decorations(output)
|
|
|
|
self.assertEqual(stripped, "appleorange")
|
2004-10-12 12:26:28 -03:00
|
|
|
|
|
|
|
def test_stdout_stderr_file(self):
|
2004-10-12 19:29:54 -03:00
|
|
|
# capture stdout and stderr to the same open file
|
2004-10-12 12:26:28 -03:00
|
|
|
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)
|
2004-10-13 01:07:12 -03:00
|
|
|
output = tf.read()
|
|
|
|
stripped = remove_stderr_debug_decorations(output)
|
|
|
|
self.assertEqual(stripped, "appleorange")
|
2004-10-12 12:26:28 -03:00
|
|
|
|
2006-09-06 21:48:33 -03:00
|
|
|
def test_stdout_filedes_of_stdout(self):
|
|
|
|
# stdout is set to 1 (#1531862).
|
|
|
|
cmd = r"import sys, os; sys.exit(os.write(sys.stdout.fileno(), '.\n'))"
|
|
|
|
rc = subprocess.call([sys.executable, "-c", cmd], stdout=1)
|
|
|
|
self.assertEquals(rc, 2)
|
|
|
|
|
2004-10-12 12:26:28 -03:00
|
|
|
def test_cwd(self):
|
2007-12-20 13:28:10 -04:00
|
|
|
tmpdir = tempfile.gettempdir()
|
2004-11-12 11:51:48 -04:00
|
|
|
# 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)
|
2004-10-12 12:26:28 -03:00
|
|
|
p = subprocess.Popen([sys.executable, "-c",
|
|
|
|
'import sys,os;' \
|
|
|
|
'sys.stdout.write(os.getcwd())'],
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
cwd=tmpdir)
|
2004-10-13 03:55:40 -03:00
|
|
|
normcase = os.path.normcase
|
|
|
|
self.assertEqual(normcase(p.stdout.read()), normcase(tmpdir))
|
2004-10-12 12:26:28 -03:00
|
|
|
|
|
|
|
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")
|
|
|
|
|
2005-03-03 16:24:28 -04:00
|
|
|
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)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
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)
|
2008-05-26 17:22:14 -03:00
|
|
|
self.assertEqual(remove_stderr_debug_decorations(stderr), "pineapple")
|
2005-03-03 16:24:28 -04:00
|
|
|
|
2004-10-12 12:26:28 -03:00
|
|
|
def test_communicate(self):
|
|
|
|
p = subprocess.Popen([sys.executable, "-c",
|
2008-05-26 17:22:14 -03:00
|
|
|
'import sys,os;'
|
|
|
|
'sys.stderr.write("pineapple");'
|
2004-10-12 12:26:28 -03:00
|
|
|
'sys.stdout.write(sys.stdin.read())'],
|
2004-10-12 19:19:32 -03:00
|
|
|
stdin=subprocess.PIPE,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE)
|
2004-10-12 12:26:28 -03:00
|
|
|
(stdout, stderr) = p.communicate("banana")
|
|
|
|
self.assertEqual(stdout, "banana")
|
2004-10-13 01:07:12 -03:00
|
|
|
self.assertEqual(remove_stderr_debug_decorations(stderr),
|
|
|
|
"pineapple")
|
2004-10-12 12:26:28 -03:00
|
|
|
|
2008-05-26 17:22:14 -03:00
|
|
|
# This test is Linux specific for simplicity to at least have
|
|
|
|
# some coverage. It is not a platform specific bug.
|
|
|
|
if os.path.isdir('/proc/%d/fd' % os.getpid()):
|
|
|
|
# Test for the fd leak reported in http://bugs.python.org/issue2791.
|
|
|
|
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)
|
|
|
|
|
2004-10-12 12:26:28 -03:00
|
|
|
def test_communicate_returns(self):
|
2004-10-12 19:29:54 -03:00
|
|
|
# communicate() should return None if no redirection is active
|
2004-10-12 19:19:32 -03:00
|
|
|
p = subprocess.Popen([sys.executable, "-c",
|
|
|
|
"import sys; sys.exit(47)"])
|
2004-10-12 12:26:28 -03:00
|
|
|
(stdout, stderr) = p.communicate()
|
|
|
|
self.assertEqual(stdout, None)
|
|
|
|
self.assertEqual(stderr, None)
|
|
|
|
|
|
|
|
def test_communicate_pipe_buf(self):
|
2004-10-12 19:29:54 -03:00
|
|
|
# communicate() with writes larger than pipe_buf
|
2004-10-12 12:26:28 -03:00
|
|
|
# This test will probably deadlock rather than fail, if
|
2004-10-12 18:51:32 -03:00
|
|
|
# communicate() does not work properly.
|
2004-10-12 12:26:28 -03:00
|
|
|
x, y = os.pipe()
|
|
|
|
if mswindows:
|
|
|
|
pipe_buf = 512
|
|
|
|
else:
|
|
|
|
pipe_buf = os.fpathconf(x, "PC_PIPE_BUF")
|
|
|
|
os.close(x)
|
|
|
|
os.close(y)
|
|
|
|
p = subprocess.Popen([sys.executable, "-c",
|
2004-10-12 18:51:32 -03:00
|
|
|
'import sys,os;'
|
2004-10-12 12:26:28 -03:00
|
|
|
'sys.stdout.write(sys.stdin.read(47));' \
|
|
|
|
'sys.stderr.write("xyz"*%d);' \
|
|
|
|
'sys.stdout.write(sys.stdin.read())' % pipe_buf],
|
2004-10-12 19:19:32 -03:00
|
|
|
stdin=subprocess.PIPE,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE)
|
2004-10-12 12:26:28 -03:00
|
|
|
string_to_write = "abc"*pipe_buf
|
|
|
|
(stdout, stderr) = p.communicate(string_to_write)
|
|
|
|
self.assertEqual(stdout, string_to_write)
|
|
|
|
|
|
|
|
def test_writes_before_communicate(self):
|
2004-10-12 19:29:54 -03:00
|
|
|
# stdin.write before communicate()
|
2004-10-12 12:26:28 -03:00
|
|
|
p = subprocess.Popen([sys.executable, "-c",
|
|
|
|
'import sys,os;' \
|
|
|
|
'sys.stdout.write(sys.stdin.read())'],
|
2004-10-12 19:19:32 -03:00
|
|
|
stdin=subprocess.PIPE,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE)
|
2004-10-12 12:26:28 -03:00
|
|
|
p.stdin.write("banana")
|
|
|
|
(stdout, stderr) = p.communicate("split")
|
|
|
|
self.assertEqual(stdout, "bananasplit")
|
2004-10-13 01:07:12 -03:00
|
|
|
self.assertEqual(remove_stderr_debug_decorations(stderr), "")
|
2004-10-12 18:51:32 -03:00
|
|
|
|
2004-10-12 12:26:28 -03:00
|
|
|
def test_universal_newlines(self):
|
|
|
|
p = subprocess.Popen([sys.executable, "-c",
|
2004-10-12 19:19:32 -03:00
|
|
|
'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();'
|
2004-10-12 12:26:28 -03:00
|
|
|
'sys.stdout.write("\\nline5");'
|
2004-10-12 19:19:32 -03:00
|
|
|
'sys.stdout.flush();'
|
2004-10-12 12:26:28 -03:00
|
|
|
'sys.stdout.write("\\nline6");'],
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
universal_newlines=1)
|
|
|
|
stdout = p.stdout.read()
|
2006-05-02 03:23:22 -03:00
|
|
|
if hasattr(file, 'newlines'):
|
2004-10-12 12:26:28 -03:00
|
|
|
# Interpreter with universal newline support
|
2004-10-12 19:19:32 -03:00
|
|
|
self.assertEqual(stdout,
|
|
|
|
"line1\nline2\nline3\nline4\nline5\nline6")
|
2004-10-12 12:26:28 -03:00
|
|
|
else:
|
|
|
|
# Interpreter without universal newline support
|
2004-10-12 19:19:32 -03:00
|
|
|
self.assertEqual(stdout,
|
|
|
|
"line1\nline2\rline3\r\nline4\r\nline5\nline6")
|
2004-10-12 12:26:28 -03:00
|
|
|
|
|
|
|
def test_universal_newlines_communicate(self):
|
2004-10-12 19:29:54 -03:00
|
|
|
# universal newlines through communicate()
|
2004-10-12 12:26:28 -03:00
|
|
|
p = subprocess.Popen([sys.executable, "-c",
|
2004-10-12 19:19:32 -03:00
|
|
|
'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();'
|
2004-10-12 12:26:28 -03:00
|
|
|
'sys.stdout.write("\\nline5");'
|
2004-10-12 19:19:32 -03:00
|
|
|
'sys.stdout.flush();'
|
2004-10-12 12:26:28 -03:00
|
|
|
'sys.stdout.write("\\nline6");'],
|
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
|
|
|
universal_newlines=1)
|
|
|
|
(stdout, stderr) = p.communicate()
|
2006-05-02 03:23:22 -03:00
|
|
|
if hasattr(file, 'newlines'):
|
2004-10-12 12:26:28 -03:00
|
|
|
# Interpreter with universal newline support
|
2004-10-12 19:19:32 -03:00
|
|
|
self.assertEqual(stdout,
|
|
|
|
"line1\nline2\nline3\nline4\nline5\nline6")
|
2004-10-12 12:26:28 -03:00
|
|
|
else:
|
|
|
|
# Interpreter without universal newline support
|
|
|
|
self.assertEqual(stdout, "line1\nline2\rline3\r\nline4\r\nline5\nline6")
|
|
|
|
|
|
|
|
def test_no_leaking(self):
|
2004-10-12 19:29:54 -03:00
|
|
|
# Make sure we leak no resources
|
2006-06-22 17:06:46 -03:00
|
|
|
if not hasattr(test_support, "is_resource_enabled") \
|
|
|
|
or test_support.is_resource_enabled("subprocess") and not mswindows:
|
2005-03-03 16:47:37 -04:00
|
|
|
max_handles = 1026 # too much for most UNIX systems
|
|
|
|
else:
|
2005-03-27 21:08:02 -04:00
|
|
|
max_handles = 65
|
2004-10-13 04:54:54 -03:00
|
|
|
for i in range(max_handles):
|
2004-10-12 19:19:32 -03:00
|
|
|
p = subprocess.Popen([sys.executable, "-c",
|
|
|
|
"import sys;sys.stdout.write(sys.stdin.read())"],
|
|
|
|
stdin=subprocess.PIPE,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE)
|
2004-10-12 12:26:28 -03:00
|
|
|
data = p.communicate("lime")[0]
|
|
|
|
self.assertEqual(data, "lime")
|
|
|
|
|
|
|
|
|
|
|
|
def test_list2cmdline(self):
|
|
|
|
self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
|
|
|
|
'"a b c" d e')
|
|
|
|
self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
|
|
|
|
'ab\\"c \\ d')
|
2008-01-19 16:49:02 -04:00
|
|
|
self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']),
|
|
|
|
'ab\\"c " \\\\" d')
|
2004-10-12 12:26:28 -03:00
|
|
|
self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
|
|
|
|
'a\\\\\\b "de fg" h')
|
|
|
|
self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
|
|
|
|
'a\\\\\\"b c d')
|
|
|
|
self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
|
|
|
|
'"a\\\\b c" d e')
|
|
|
|
self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
|
|
|
|
'"a\\\\b\\ c" d e')
|
2007-01-13 18:35:35 -04:00
|
|
|
self.assertEqual(subprocess.list2cmdline(['ab', '']),
|
|
|
|
'ab ""')
|
2008-01-19 18:49:37 -04:00
|
|
|
self.assertEqual(subprocess.list2cmdline(['echo', 'foo|bar']),
|
|
|
|
'echo "foo|bar"')
|
2004-10-12 12:26:28 -03:00
|
|
|
|
|
|
|
|
|
|
|
def test_poll(self):
|
|
|
|
p = subprocess.Popen([sys.executable,
|
2004-10-13 00:43:40 -03:00
|
|
|
"-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)
|
2004-10-12 12:26:28 -03:00
|
|
|
# Subsequent invocations should just return the returncode
|
|
|
|
self.assertEqual(p.poll(), 0)
|
|
|
|
|
|
|
|
|
|
|
|
def test_wait(self):
|
|
|
|
p = subprocess.Popen([sys.executable,
|
|
|
|
"-c", "import time; time.sleep(2)"])
|
|
|
|
self.assertEqual(p.wait(), 0)
|
|
|
|
# Subsequent invocations should just return the returncode
|
|
|
|
self.assertEqual(p.wait(), 0)
|
2004-10-12 18:51:32 -03:00
|
|
|
|
2004-11-30 17:04:45 -04:00
|
|
|
|
|
|
|
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")
|
|
|
|
|
2004-10-12 12:26:28 -03:00
|
|
|
#
|
|
|
|
# POSIX tests
|
|
|
|
#
|
|
|
|
if not mswindows:
|
|
|
|
def test_exceptions(self):
|
2004-10-12 19:29:54 -03:00
|
|
|
# catched & re-raised exceptions
|
2004-10-12 12:26:28 -03:00
|
|
|
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)
|
|
|
|
else:
|
|
|
|
self.fail("Expected OSError")
|
2004-10-12 18:51:32 -03:00
|
|
|
|
2006-08-01 15:16:15 -03:00
|
|
|
def _suppress_core_files(self):
|
|
|
|
"""Try to prevent core files from being created.
|
|
|
|
Returns previous ulimit if successful, else None.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
import resource
|
|
|
|
old_limit = resource.getrlimit(resource.RLIMIT_CORE)
|
|
|
|
resource.setrlimit(resource.RLIMIT_CORE, (0,0))
|
|
|
|
return old_limit
|
|
|
|
except (ImportError, ValueError, resource.error):
|
|
|
|
return None
|
|
|
|
|
|
|
|
def _unsuppress_core_files(self, old_limit):
|
|
|
|
"""Return core file behavior to default."""
|
|
|
|
if old_limit is None:
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
import resource
|
|
|
|
resource.setrlimit(resource.RLIMIT_CORE, old_limit)
|
|
|
|
except (ImportError, ValueError, resource.error):
|
|
|
|
return
|
2006-08-02 00:27:46 -03:00
|
|
|
|
2004-10-12 12:26:28 -03:00
|
|
|
def test_run_abort(self):
|
2004-10-12 19:29:54 -03:00
|
|
|
# returncode handles signal termination
|
2006-08-01 15:16:15 -03:00
|
|
|
old_limit = self._suppress_core_files()
|
|
|
|
try:
|
|
|
|
p = subprocess.Popen([sys.executable,
|
|
|
|
"-c", "import os; os.abort()"])
|
|
|
|
finally:
|
|
|
|
self._unsuppress_core_files(old_limit)
|
2004-10-12 12:26:28 -03:00
|
|
|
p.wait()
|
|
|
|
self.assertEqual(-p.returncode, signal.SIGABRT)
|
|
|
|
|
|
|
|
def test_preexec(self):
|
2004-10-12 19:29:54 -03:00
|
|
|
# preexec function
|
2004-10-12 12:26:28 -03:00
|
|
|
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_args_string(self):
|
2004-10-12 19:29:54 -03:00
|
|
|
# args is a string
|
2004-10-12 12:26:28 -03:00
|
|
|
f, fname = self.mkstemp()
|
|
|
|
os.write(f, "#!/bin/sh\n")
|
2008-05-17 04:17:34 -03:00
|
|
|
os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" %
|
2004-10-12 19:19:32 -03:00
|
|
|
sys.executable)
|
2004-10-12 12:26:28 -03:00
|
|
|
os.close(f)
|
|
|
|
os.chmod(fname, 0700)
|
|
|
|
p = subprocess.Popen(fname)
|
|
|
|
p.wait()
|
|
|
|
os.remove(fname)
|
2004-11-17 16:06:35 -04:00
|
|
|
self.assertEqual(p.returncode, 47)
|
2004-10-12 12:26:28 -03:00
|
|
|
|
|
|
|
def test_invalid_args(self):
|
2004-10-12 19:29:54 -03:00
|
|
|
# invalid arguments should raise ValueError
|
2004-10-12 12:26:28 -03:00
|
|
|
self.assertRaises(ValueError, subprocess.call,
|
2004-10-12 19:19:32 -03:00
|
|
|
[sys.executable,
|
|
|
|
"-c", "import sys; sys.exit(47)"],
|
2004-10-12 12:26:28 -03:00
|
|
|
startupinfo=47)
|
|
|
|
self.assertRaises(ValueError, subprocess.call,
|
2004-10-12 19:19:32 -03:00
|
|
|
[sys.executable,
|
|
|
|
"-c", "import sys; sys.exit(47)"],
|
2004-10-12 12:26:28 -03:00
|
|
|
creationflags=47)
|
|
|
|
|
|
|
|
def test_shell_sequence(self):
|
2004-10-12 19:29:54 -03:00
|
|
|
# Run command through the shell (sequence)
|
2004-10-12 12:26:28 -03:00
|
|
|
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_shell_string(self):
|
2004-10-12 19:29:54 -03:00
|
|
|
# Run command through the shell (string)
|
2004-10-12 12:26:28 -03:00
|
|
|
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_call_string(self):
|
2004-10-12 19:29:54 -03:00
|
|
|
# call() function with string argument on UNIX
|
2004-10-12 12:26:28 -03:00
|
|
|
f, fname = self.mkstemp()
|
|
|
|
os.write(f, "#!/bin/sh\n")
|
2008-05-17 04:17:34 -03:00
|
|
|
os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" %
|
2004-10-12 19:19:32 -03:00
|
|
|
sys.executable)
|
2004-10-12 12:26:28 -03:00
|
|
|
os.close(f)
|
|
|
|
os.chmod(fname, 0700)
|
|
|
|
rc = subprocess.call(fname)
|
2004-11-17 16:06:35 -04:00
|
|
|
os.remove(fname)
|
2004-10-12 12:26:28 -03:00
|
|
|
self.assertEqual(rc, 47)
|
|
|
|
|
2008-05-06 20:42:58 -03:00
|
|
|
def DISABLED_test_send_signal(self):
|
2008-04-18 23:23:57 -03:00
|
|
|
p = subprocess.Popen([sys.executable,
|
|
|
|
"-c", "input()"])
|
|
|
|
|
|
|
|
self.assert_(p.poll() is None, p.poll())
|
|
|
|
p.send_signal(signal.SIGINT)
|
|
|
|
self.assertNotEqual(p.wait(), 0)
|
|
|
|
|
2008-05-06 20:42:58 -03:00
|
|
|
def DISABLED_test_kill(self):
|
2008-04-18 23:23:57 -03:00
|
|
|
p = subprocess.Popen([sys.executable,
|
|
|
|
"-c", "input()"])
|
|
|
|
|
|
|
|
self.assert_(p.poll() is None, p.poll())
|
|
|
|
p.kill()
|
|
|
|
self.assertEqual(p.wait(), -signal.SIGKILL)
|
|
|
|
|
2008-05-06 20:42:58 -03:00
|
|
|
def DISABLED_test_terminate(self):
|
2008-04-18 23:23:57 -03:00
|
|
|
p = subprocess.Popen([sys.executable,
|
|
|
|
"-c", "input()"])
|
|
|
|
|
|
|
|
self.assert_(p.poll() is None, p.poll())
|
|
|
|
p.terminate()
|
|
|
|
self.assertEqual(p.wait(), -signal.SIGTERM)
|
2004-10-12 18:51:32 -03:00
|
|
|
|
2004-10-12 12:26:28 -03:00
|
|
|
#
|
|
|
|
# Windows tests
|
|
|
|
#
|
|
|
|
if mswindows:
|
|
|
|
def test_startupinfo(self):
|
2004-10-12 19:29:54 -03:00
|
|
|
# startupinfo argument
|
2004-10-12 12:26:28 -03:00
|
|
|
# We uses hardcoded constants, because we do not want to
|
2004-10-12 18:51:32 -03:00
|
|
|
# depend on win32all.
|
2004-10-12 12:26:28 -03:00
|
|
|
STARTF_USESHOWWINDOW = 1
|
|
|
|
SW_MAXIMIZE = 3
|
|
|
|
startupinfo = subprocess.STARTUPINFO()
|
|
|
|
startupinfo.dwFlags = STARTF_USESHOWWINDOW
|
|
|
|
startupinfo.wShowWindow = SW_MAXIMIZE
|
|
|
|
# Since Python is a console process, it won't be affected
|
|
|
|
# by wShowWindow, but the argument should be silently
|
|
|
|
# ignored
|
|
|
|
subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
|
|
|
|
startupinfo=startupinfo)
|
|
|
|
|
|
|
|
def test_creationflags(self):
|
2004-10-12 19:29:54 -03:00
|
|
|
# creationflags argument
|
2004-10-12 12:26:28 -03:00
|
|
|
CREATE_NEW_CONSOLE = 16
|
2004-10-13 00:21:35 -03:00
|
|
|
sys.stderr.write(" a DOS box should flash briefly ...\n")
|
2004-10-12 19:19:32 -03:00
|
|
|
subprocess.call(sys.executable +
|
2004-10-13 00:21:35 -03:00
|
|
|
' -c "import time; time.sleep(0.25)"',
|
2004-10-12 12:26:28 -03:00
|
|
|
creationflags=CREATE_NEW_CONSOLE)
|
|
|
|
|
|
|
|
def test_invalid_args(self):
|
2004-10-12 19:29:54 -03:00
|
|
|
# invalid arguments should raise ValueError
|
2004-10-12 12:26:28 -03:00
|
|
|
self.assertRaises(ValueError, subprocess.call,
|
2004-10-12 19:19:32 -03:00
|
|
|
[sys.executable,
|
|
|
|
"-c", "import sys; sys.exit(47)"],
|
2004-10-12 12:26:28 -03:00
|
|
|
preexec_fn=lambda: 1)
|
|
|
|
self.assertRaises(ValueError, subprocess.call,
|
2004-10-12 19:19:32 -03:00
|
|
|
[sys.executable,
|
|
|
|
"-c", "import sys; sys.exit(47)"],
|
2007-05-26 19:18:20 -03:00
|
|
|
stdout=subprocess.PIPE,
|
2004-10-12 12:26:28 -03:00
|
|
|
close_fds=True)
|
|
|
|
|
2007-05-26 19:18:20 -03:00
|
|
|
def test_close_fds(self):
|
|
|
|
# close file descriptors
|
|
|
|
rc = subprocess.call([sys.executable, "-c",
|
|
|
|
"import sys; sys.exit(47)"],
|
|
|
|
close_fds=True)
|
|
|
|
self.assertEqual(rc, 47)
|
|
|
|
|
2004-10-12 12:26:28 -03:00
|
|
|
def test_shell_sequence(self):
|
2004-10-12 19:29:54 -03:00
|
|
|
# Run command through the shell (sequence)
|
2004-10-12 12:26:28 -03:00
|
|
|
newenv = os.environ.copy()
|
|
|
|
newenv["FRUIT"] = "physalis"
|
|
|
|
p = subprocess.Popen(["set"], shell=1,
|
2004-10-12 18:51:32 -03:00
|
|
|
stdout=subprocess.PIPE,
|
2004-10-12 12:26:28 -03:00
|
|
|
env=newenv)
|
|
|
|
self.assertNotEqual(p.stdout.read().find("physalis"), -1)
|
|
|
|
|
|
|
|
def test_shell_string(self):
|
2004-10-12 19:29:54 -03:00
|
|
|
# Run command through the shell (string)
|
2004-10-12 12:26:28 -03:00
|
|
|
newenv = os.environ.copy()
|
|
|
|
newenv["FRUIT"] = "physalis"
|
|
|
|
p = subprocess.Popen("set", shell=1,
|
2004-10-12 18:51:32 -03:00
|
|
|
stdout=subprocess.PIPE,
|
2004-10-12 12:26:28 -03:00
|
|
|
env=newenv)
|
|
|
|
self.assertNotEqual(p.stdout.read().find("physalis"), -1)
|
|
|
|
|
|
|
|
def test_call_string(self):
|
2004-10-12 19:29:54 -03:00
|
|
|
# call() function with string argument on Windows
|
2004-10-12 19:19:32 -03:00
|
|
|
rc = subprocess.call(sys.executable +
|
|
|
|
' -c "import sys; sys.exit(47)"')
|
2004-10-12 12:26:28 -03:00
|
|
|
self.assertEqual(rc, 47)
|
|
|
|
|
2008-05-06 20:42:58 -03:00
|
|
|
def DISABLED_test_send_signal(self):
|
2008-04-18 23:23:57 -03:00
|
|
|
p = subprocess.Popen([sys.executable,
|
|
|
|
"-c", "input()"])
|
|
|
|
|
|
|
|
self.assert_(p.poll() is None, p.poll())
|
|
|
|
p.send_signal(signal.SIGTERM)
|
|
|
|
self.assertNotEqual(p.wait(), 0)
|
|
|
|
|
2008-05-06 20:42:58 -03:00
|
|
|
def DISABLED_test_kill(self):
|
2008-04-18 23:23:57 -03:00
|
|
|
p = subprocess.Popen([sys.executable,
|
|
|
|
"-c", "input()"])
|
|
|
|
|
|
|
|
self.assert_(p.poll() is None, p.poll())
|
|
|
|
p.kill()
|
|
|
|
self.assertNotEqual(p.wait(), 0)
|
|
|
|
|
2008-05-06 20:42:58 -03:00
|
|
|
def DISABLED_test_terminate(self):
|
2008-04-18 23:23:57 -03:00
|
|
|
p = subprocess.Popen([sys.executable,
|
|
|
|
"-c", "input()"])
|
|
|
|
|
|
|
|
self.assert_(p.poll() is None, p.poll())
|
|
|
|
p.terminate()
|
|
|
|
self.assertNotEqual(p.wait(), 0)
|
2004-10-12 12:26:28 -03:00
|
|
|
|
|
|
|
def test_main():
|
|
|
|
test_support.run_unittest(ProcessTestCase)
|
2006-07-10 17:39:49 -03:00
|
|
|
if hasattr(test_support, "reap_children"):
|
|
|
|
test_support.reap_children()
|
2004-10-12 12:26:28 -03:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|