Kaydet (Commit) 3761e8dd authored tarafından Tim Peters's avatar Tim Peters

New helper remove_stderr_debug_decorations(). This test passes in a

debug build on Windows now.  More applications of the helper may be needed
on non-Windows platforms.
üst 29b6b4f7
...@@ -6,6 +6,7 @@ import signal ...@@ -6,6 +6,7 @@ import signal
import os import os
import tempfile import tempfile
import time import time
import re
mswindows = (sys.platform == "win32") mswindows = (sys.platform == "win32")
...@@ -19,6 +20,14 @@ if mswindows: ...@@ -19,6 +20,14 @@ if mswindows:
else: else:
SETBINARY = '' SETBINARY = ''
# 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):
if __debug__:
stderr = re.sub(r"\[\d+ refs\]\r?\n?$", "", stderr)
return stderr
class ProcessTestCase(unittest.TestCase): class ProcessTestCase(unittest.TestCase):
def mkstemp(self): def mkstemp(self):
"""wrapper for mkstemp, calling mktemp if mkstemp is not available""" """wrapper for mkstemp, calling mktemp if mkstemp is not available"""
...@@ -144,7 +153,8 @@ class ProcessTestCase(unittest.TestCase): ...@@ -144,7 +153,8 @@ class ProcessTestCase(unittest.TestCase):
p = subprocess.Popen([sys.executable, "-c", p = subprocess.Popen([sys.executable, "-c",
'import sys; sys.stderr.write("strawberry")'], 'import sys; sys.stderr.write("strawberry")'],
stderr=subprocess.PIPE) stderr=subprocess.PIPE)
self.assertEqual(p.stderr.read(), "strawberry") self.assertEqual(remove_stderr_debug_decorations(p.stderr.read()),
"strawberry")
def test_stderr_filedes(self): def test_stderr_filedes(self):
# stderr is set to open file descriptor # stderr is set to open file descriptor
...@@ -155,7 +165,8 @@ class ProcessTestCase(unittest.TestCase): ...@@ -155,7 +165,8 @@ class ProcessTestCase(unittest.TestCase):
stderr=d) stderr=d)
p.wait() p.wait()
os.lseek(d, 0, 0) os.lseek(d, 0, 0)
self.assertEqual(os.read(d, 1024), "strawberry") self.assertEqual(remove_stderr_debug_decorations(os.read(d, 1024)),
"strawberry")
def test_stderr_fileobj(self): def test_stderr_fileobj(self):
# stderr is set to open file object # stderr is set to open file object
...@@ -165,7 +176,8 @@ class ProcessTestCase(unittest.TestCase): ...@@ -165,7 +176,8 @@ class ProcessTestCase(unittest.TestCase):
stderr=tf) stderr=tf)
p.wait() p.wait()
tf.seek(0) tf.seek(0)
self.assertEqual(tf.read(), "strawberry") self.assertEqual(remove_stderr_debug_decorations(tf.read()),
"strawberry")
def test_stdout_stderr_pipe(self): def test_stdout_stderr_pipe(self):
# capture stdout and stderr to the same pipe # capture stdout and stderr to the same pipe
...@@ -176,7 +188,9 @@ class ProcessTestCase(unittest.TestCase): ...@@ -176,7 +188,9 @@ class ProcessTestCase(unittest.TestCase):
'sys.stderr.write("orange")'], 'sys.stderr.write("orange")'],
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT) stderr=subprocess.STDOUT)
self.assertEqual(p.stdout.read(), "appleorange") output = p.stdout.read()
stripped = remove_stderr_debug_decorations(output)
self.assertEqual(stripped, "appleorange")
def test_stdout_stderr_file(self): def test_stdout_stderr_file(self):
# capture stdout and stderr to the same open file # capture stdout and stderr to the same open file
...@@ -190,7 +204,9 @@ class ProcessTestCase(unittest.TestCase): ...@@ -190,7 +204,9 @@ class ProcessTestCase(unittest.TestCase):
stderr=tf) stderr=tf)
p.wait() p.wait()
tf.seek(0) tf.seek(0)
self.assertEqual(tf.read(), "appleorange") output = tf.read()
stripped = remove_stderr_debug_decorations(output)
self.assertEqual(stripped, "appleorange")
def test_cwd(self): def test_cwd(self):
tmpdir = os.getenv("TEMP", "/tmp") tmpdir = os.getenv("TEMP", "/tmp")
...@@ -222,7 +238,8 @@ class ProcessTestCase(unittest.TestCase): ...@@ -222,7 +238,8 @@ class ProcessTestCase(unittest.TestCase):
stderr=subprocess.PIPE) stderr=subprocess.PIPE)
(stdout, stderr) = p.communicate("banana") (stdout, stderr) = p.communicate("banana")
self.assertEqual(stdout, "banana") self.assertEqual(stdout, "banana")
self.assertEqual(stderr, "pineapple") self.assertEqual(remove_stderr_debug_decorations(stderr),
"pineapple")
def test_communicate_returns(self): def test_communicate_returns(self):
# communicate() should return None if no redirection is active # communicate() should return None if no redirection is active
...@@ -266,7 +283,7 @@ class ProcessTestCase(unittest.TestCase): ...@@ -266,7 +283,7 @@ class ProcessTestCase(unittest.TestCase):
p.stdin.write("banana") p.stdin.write("banana")
(stdout, stderr) = p.communicate("split") (stdout, stderr) = p.communicate("split")
self.assertEqual(stdout, "bananasplit") self.assertEqual(stdout, "bananasplit")
self.assertEqual(stderr, "") self.assertEqual(remove_stderr_debug_decorations(stderr), "")
def test_universal_newlines(self): def test_universal_newlines(self):
p = subprocess.Popen([sys.executable, "-c", p = subprocess.Popen([sys.executable, "-c",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment