unittest.result.TestResult does not create its buffers until they're used. It uses StringIO not cStringIO. Issue 8333.

This commit is contained in:
Michael Foord 2010-04-07 23:04:22 +00:00
parent 9a39eccfe1
commit f6ff26c486
2 changed files with 11 additions and 6 deletions

View File

@ -4,7 +4,7 @@ import os
import sys import sys
import traceback import traceback
from cStringIO import StringIO from StringIO import StringIO
from . import util from . import util
from functools import wraps from functools import wraps
@ -46,8 +46,8 @@ class TestResult(object):
self.unexpectedSuccesses = [] self.unexpectedSuccesses = []
self.shouldStop = False self.shouldStop = False
self.buffer = False self.buffer = False
self._stdout_buffer = StringIO() self._stdout_buffer = None
self._stderr_buffer = StringIO() self._stderr_buffer = None
self._original_stdout = sys.stdout self._original_stdout = sys.stdout
self._original_stderr = sys.stderr self._original_stderr = sys.stderr
self._mirrorOutput = False self._mirrorOutput = False
@ -60,6 +60,9 @@ class TestResult(object):
self.testsRun += 1 self.testsRun += 1
self._mirrorOutput = False self._mirrorOutput = False
if self.buffer: if self.buffer:
if self._stderr_buffer is None:
self._stderr_buffer = StringIO()
self._stdout_buffer = StringIO()
sys.stdout = self._stdout_buffer sys.stdout = self._stdout_buffer
sys.stderr = self._stderr_buffer sys.stderr = self._stderr_buffer

View File

@ -1,6 +1,6 @@
import sys import sys
import textwrap import textwrap
from cStringIO import StringIO, OutputType from StringIO import StringIO
from test import test_support from test import test_support
import unittest import unittest
@ -24,6 +24,8 @@ class Test_TestResult(unittest.TestCase):
self.assertEqual(len(result.failures), 0) self.assertEqual(len(result.failures), 0)
self.assertEqual(result.testsRun, 0) self.assertEqual(result.testsRun, 0)
self.assertEqual(result.shouldStop, False) self.assertEqual(result.shouldStop, False)
self.assertIsNone(result._stdout_buffer)
self.assertIsNone(result._stderr_buffer)
# "This method can be called to signal that the set of tests being # "This method can be called to signal that the set of tests being
@ -400,8 +402,8 @@ class TestOutputBuffering(unittest.TestCase):
self.assertIsNot(real_out, sys.stdout) self.assertIsNot(real_out, sys.stdout)
self.assertIsNot(real_err, sys.stderr) self.assertIsNot(real_err, sys.stderr)
self.assertIsInstance(sys.stdout, OutputType) self.assertIsInstance(sys.stdout, StringIO)
self.assertIsInstance(sys.stderr, OutputType) self.assertIsInstance(sys.stderr, StringIO)
self.assertIsNot(sys.stdout, sys.stderr) self.assertIsNot(sys.stdout, sys.stderr)
out_stream = sys.stdout out_stream = sys.stdout