(merge 3.2) Issue #12400: runtest() reuses the same io.StringIO instance for

all calls

 * Don't force verbose to True with option -W
 * Rename rerun_failed variable to output_on_failure
This commit is contained in:
Victor Stinner 2011-06-29 15:25:40 +02:00
commit d71bd9a9a4
1 changed files with 37 additions and 27 deletions

View File

@ -162,23 +162,24 @@ option '-uall,-gui'.
""" """
import builtins import builtins
import errno
import faulthandler import faulthandler
import getopt import getopt
import io
import json import json
import logging
import os import os
import platform
import random import random
import re import re
import sys import sys
import time
import errno
import traceback
import warnings
import unittest
from inspect import isabstract
import tempfile
import platform
import sysconfig import sysconfig
import logging import tempfile
import time
import traceback
import unittest
import warnings
from inspect import isabstract
# Some times __path__ and __file__ are not absolute (e.g. while running from # Some times __path__ and __file__ are not absolute (e.g. while running from
@ -579,7 +580,8 @@ def main(tests=None, testdir=None, verbose=0, quiet=False,
args_tuple = ( args_tuple = (
(test, verbose, quiet), (test, verbose, quiet),
dict(huntrleaks=huntrleaks, use_resources=use_resources, dict(huntrleaks=huntrleaks, use_resources=use_resources,
debug=debug, rerun_failed=verbose3, timeout=timeout) debug=debug, output_on_failure=verbose3,
timeout=timeout)
) )
yield (test, args_tuple) yield (test, args_tuple)
pending = tests_and_args() pending = tests_and_args()
@ -664,7 +666,7 @@ def main(tests=None, testdir=None, verbose=0, quiet=False,
else: else:
try: try:
result = runtest(test, verbose, quiet, huntrleaks, debug, result = runtest(test, verbose, quiet, huntrleaks, debug,
rerun_failed=verbose3, timeout=timeout) output_on_failure=verbose3, timeout=timeout)
accumulate_result(test, result) accumulate_result(test, result)
except KeyboardInterrupt: except KeyboardInterrupt:
interrupted = True interrupted = True
@ -808,7 +810,7 @@ def replace_stdout():
def runtest(test, verbose, quiet, def runtest(test, verbose, quiet,
huntrleaks=False, debug=False, use_resources=None, huntrleaks=False, debug=False, use_resources=None,
rerun_failed=False, timeout=None): output_on_failure=False, timeout=None):
"""Run a single test. """Run a single test.
test -- the name of the test test -- the name of the test
@ -817,7 +819,7 @@ def runtest(test, verbose, quiet,
test_times -- a list of (time, test_name) pairs test_times -- a list of (time, test_name) pairs
huntrleaks -- run multiple times to test for leaks; requires a debug huntrleaks -- run multiple times to test for leaks; requires a debug
build; a triple corresponding to -R's three arguments build; a triple corresponding to -R's three arguments
rerun_failed -- if true, re-run in verbose mode when failed output_on_failure -- if true, display test output on failure
timeout -- dump the traceback and exit if a test takes more than timeout -- dump the traceback and exit if a test takes more than
timeout seconds timeout seconds
@ -836,22 +838,29 @@ def runtest(test, verbose, quiet,
if use_timeout: if use_timeout:
faulthandler.dump_tracebacks_later(timeout, exit=True) faulthandler.dump_tracebacks_later(timeout, exit=True)
try: try:
if rerun_failed: support.verbose = verbose # Tell tests to be moderately quiet
support.verbose = True if output_on_failure:
if runtest.stringio is None:
# Reuse the same instance to all calls to runtest(). Some
# tests keep a reference to sys.stdout or sys.stderr
# (eg. test_argparse).
runtest.stringio = io.StringIO()
orig_stdout = sys.stdout
orig_stderr = sys.stderr orig_stderr = sys.stderr
with support.captured_stdout() as stream: try:
try: sys.stdout = runtest.stringio
sys.stderr = stream sys.stderr = runtest.stringio
result = runtest_inner(test, verbose, quiet, huntrleaks, result = runtest_inner(test, verbose, quiet, huntrleaks,
debug, display_failure=False) debug, display_failure=False)
if result[0] == FAILED: if result[0] == FAILED:
output = stream.getvalue() output = stringio.getvalue()
orig_stderr.write(output) orig_stderr.write(output)
orig_stderr.flush() orig_stderr.flush()
finally: finally:
sys.stderr = orig_stderr sys.stdout = orig_stdout
sys.stderr = orig_stderr
else: else:
support.verbose = verbose # Tell tests to be moderately quiet
result = runtest_inner(test, verbose, quiet, huntrleaks, debug, result = runtest_inner(test, verbose, quiet, huntrleaks, debug,
display_failure=not verbose) display_failure=not verbose)
return result return result
@ -859,6 +868,7 @@ def runtest(test, verbose, quiet,
if use_timeout: if use_timeout:
faulthandler.cancel_dump_tracebacks_later() faulthandler.cancel_dump_tracebacks_later()
cleanup_test_droppings(test, verbose) cleanup_test_droppings(test, verbose)
runtest.stringio = None
# Unit tests are supposed to leave the execution environment unchanged # Unit tests are supposed to leave the execution environment unchanged
# once they complete. But sometimes tests have bugs, especially when # once they complete. But sometimes tests have bugs, especially when