diff -r 65eb12898d56 Lib/test/_test_multiprocessing.py --- a/Lib/test/_test_multiprocessing.py Fri Dec 12 12:27:27 2014 +0100 +++ b/Lib/test/_test_multiprocessing.py Sat Dec 13 18:53:47 2014 +0200 @@ -3356,6 +3356,7 @@ class TestFlags(unittest.TestCase): 'TestFlags.run_in_child()') data = subprocess.check_output( [sys.executable, '-E', '-S', '-O', '-c', prog]) + data = test.support.strip_python_stdout(data) child_flags, grandchild_flags = json.loads(data.decode('ascii')) self.assertEqual(child_flags, grandchild_flags) diff -r 65eb12898d56 Lib/test/script_helper.py --- a/Lib/test/script_helper.py Fri Dec 12 12:27:27 2014 +0100 +++ b/Lib/test/script_helper.py Sat Dec 13 18:53:47 2014 +0200 @@ -13,7 +13,8 @@ import shutil import zipfile from importlib.util import source_from_cache -from test.support import make_legacy_pyc, strip_python_stderr, temp_dir +from test.support import (make_legacy_pyc, temp_dir, + strip_python_stderr, strip_python_stdout) # Executing the interpreter in a subprocess def _assert_python(expected_success, *args, **env_vars): @@ -48,6 +49,7 @@ def _assert_python(expected_success, *ar p.stdout.close() p.stderr.close() rc = p.returncode + out = strip_python_stdout(out) err = strip_python_stderr(err) if (rc and expected_success) or (not rc and not expected_success): raise AssertionError( diff -r 65eb12898d56 Lib/test/support/__init__.py --- a/Lib/test/support/__init__.py Fri Dec 12 12:27:27 2014 +0100 +++ b/Lib/test/support/__init__.py Sat Dec 13 18:53:47 2014 +0200 @@ -2002,6 +2002,28 @@ def strip_python_stderr(stderr): stderr = re.sub(br"\[\d+ refs, \d+ blocks\]\r?\n?", b"", stderr).strip() return stderr +def strip_python_stdout(stdout): + """Strip the stdout of a Python process from potential debug output + emitted by the interpreter. + + This will typically be run on the result of the communicate() method + of a subprocess.Popen object. + """ + if hasattr(sys, 'getcounts'): + stdout = re.sub(br"([A-Za-z0-9_. -]+ alloc'd: " + br"\d+, freed: \d+, max in use: \d+" + br'|fast tuple allocs: \d+, empty: \d+' + br'|fast int allocs: pos: \d+, neg: \d+' + br'|null strings: \d+, 1-strings: \d+' + br')$\r?\n?', b'', stdout, 0, re.MULTILINE) + return stdout + +requires_type_collecting = unittest.skipIf(hasattr(sys, 'getcounts'), + 'types are immortal if COUNT_ALLOCS is used') + +requires_clean_stdout = unittest.skipIf(hasattr(sys, 'getcounts'), + 'stdout is polluted by debug output if COUNT_ALLOCS is used') + def args_from_interpreter_flags(): """Return a list of command-line arguments reproducing the current settings in sys.flags and sys.warnoptions.""" diff -r 65eb12898d56 Lib/test/test_asyncio/test_events.py --- a/Lib/test/test_asyncio/test_events.py Fri Dec 12 12:27:27 2014 +0100 +++ b/Lib/test/test_asyncio/test_events.py Sat Dec 13 18:53:47 2014 +0200 @@ -1610,6 +1610,7 @@ class SubprocessTestsMixin: self.loop.run_until_complete(proto.completed) self.assertEqual(-signal.SIGHUP, proto.returncode) + @support.requires_clean_stdout def test_subprocess_stderr(self): prog = os.path.join(os.path.dirname(__file__), 'echo2.py') diff -r 65eb12898d56 Lib/test/test_asyncio/test_subprocess.py --- a/Lib/test/test_asyncio/test_subprocess.py Fri Dec 12 12:27:27 2014 +0100 +++ b/Lib/test/test_asyncio/test_subprocess.py Sat Dec 13 18:53:47 2014 +0200 @@ -21,6 +21,7 @@ PROGRAM_CAT = [ class SubprocessMixin: + @support.requires_clean_stdout def test_stdin_stdout(self): args = PROGRAM_CAT @@ -48,6 +49,7 @@ class SubprocessMixin: self.assertEqual(exitcode, 0) self.assertEqual(stdout, b'some data') + @support.requires_clean_stdout def test_communicate(self): args = PROGRAM_CAT @@ -162,6 +164,7 @@ class SubprocessMixin: self.loop.run_until_complete(proc.communicate(large_data)) self.loop.run_until_complete(proc.wait()) + @support.requires_clean_stdout def test_pause_reading(self): limit = 10 size = (limit * 2 + 1) @@ -198,6 +201,7 @@ class SubprocessMixin: self.assertTrue(transport.pause_reading.called) self.assertTrue(transport.resume_reading.called) + @support.requires_clean_stdout def test_stdin_not_inheritable(self): # Tulip issue #209: stdin must not be inheritable, otherwise # the Process.communicate() hangs diff -r 65eb12898d56 Lib/test/test_base64.py --- a/Lib/test/test_base64.py Fri Dec 12 12:27:27 2014 +0100 +++ b/Lib/test/test_base64.py Sat Dec 13 18:53:47 2014 +0200 @@ -630,7 +630,9 @@ class TestMain(unittest.TestCase): def get_output(self, *args, **options): args = (sys.executable, '-m', 'base64') + args - return subprocess.check_output(args, **options) + out = subprocess.check_output(args, **options) + out = support.strip_python_stdout(out) + return out def test_encode_decode(self): output = self.get_output('-t') diff -r 65eb12898d56 Lib/test/test_capi.py --- a/Lib/test/test_capi.py Fri Dec 12 12:27:27 2014 +0100 +++ b/Lib/test/test_capi.py Sat Dec 13 18:53:47 2014 +0200 @@ -310,6 +310,7 @@ class EmbeddingTests(unittest.TestCase): self.assertEqual(p.returncode, 0, "bad returncode %d, stderr is %r" % (p.returncode, err)) + out = os.fsdecode(support.strip_python_stdout(os.fsencode(out))) return out, err def test_subinterps(self): diff -r 65eb12898d56 Lib/test/test_cmd_line.py --- a/Lib/test/test_cmd_line.py Fri Dec 12 12:27:27 2014 +0100 +++ b/Lib/test/test_cmd_line.py Sat Dec 13 18:53:47 2014 +0200 @@ -86,6 +86,7 @@ class CmdLineTest(unittest.TestCase): p.stderr.close() rc = p.returncode self.assertEqual(rc, 0) + out = test.support.strip_python_stdout(out) return rc, out, err code = 'import sys; print(sys._xoptions)' # normally the refcount is hidden @@ -193,6 +194,7 @@ class CmdLineTest(unittest.TestCase): stdout=subprocess.PIPE, env=env) stdout, stderr = p.communicate() + stdout = test.support.strip_python_stdout(stdout) self.assertEqual(stdout, expected) self.assertEqual(p.returncode, 0) @@ -296,6 +298,7 @@ class CmdLineTest(unittest.TestCase): (sys.executable, "-c", code), stdin=stdin, stdout=subprocess.PIPE) as proc: stdout, stderr = proc.communicate() + stdout = test.support.strip_python_stdout(stdout) self.assertEqual(stdout.rstrip(), expected) def test_stdin_readline(self): @@ -463,6 +466,7 @@ class CmdLineTest(unittest.TestCase): stderr=subprocess.DEVNULL) out = subprocess.check_output([sys.executable, "-I", main], cwd=tmpdir) + out = test.support.strip_python_stdout(out) self.assertEqual(out.strip(), b"ok") def test_main(): diff -r 65eb12898d56 Lib/test/test_faulthandler.py --- a/Lib/test/test_faulthandler.py Fri Dec 12 12:27:27 2014 +0100 +++ b/Lib/test/test_faulthandler.py Sat Dec 13 18:53:47 2014 +0200 @@ -57,7 +57,8 @@ class FaultHandlerTests(unittest.TestCas process = script_helper.spawn_python('-c', code) stdout, stderr = process.communicate() exitcode = process.wait() - output = support.strip_python_stderr(stdout) + output = support.strip_python_stdout(stdout) + output = support.strip_python_stderr(output) output = output.decode('ascii', 'backslashreplace') if filename: self.assertEqual(output, '') @@ -263,6 +264,7 @@ class FaultHandlerTests(unittest.TestCas args = (sys.executable, '-E', '-c', code) # don't use assert_python_ok() because it always enable faulthandler output = subprocess.check_output(args) + output = support.strip_python_stdout(output) self.assertEqual(output.rstrip(), b"False") def test_sys_xoptions(self): @@ -271,6 +273,7 @@ class FaultHandlerTests(unittest.TestCas args = (sys.executable, "-E", "-X", "faulthandler", "-c", code) # don't use assert_python_ok() because it always enable faulthandler output = subprocess.check_output(args) + output = support.strip_python_stdout(output) self.assertEqual(output.rstrip(), b"True") def test_env_var(self): @@ -281,12 +284,14 @@ class FaultHandlerTests(unittest.TestCas env['PYTHONFAULTHANDLER'] = '' # don't use assert_python_ok() because it always enable faulthandler output = subprocess.check_output(args, env=env) + output = support.strip_python_stdout(output) self.assertEqual(output.rstrip(), b"False") # non-empty env var env = os.environ.copy() env['PYTHONFAULTHANDLER'] = '1' output = subprocess.check_output(args, env=env) + output = support.strip_python_stdout(output) self.assertEqual(output.rstrip(), b"True") def check_dump_traceback(self, filename): diff -r 65eb12898d56 Lib/test/test_gc.py --- a/Lib/test/test_gc.py Fri Dec 12 12:27:27 2014 +0100 +++ b/Lib/test/test_gc.py Sat Dec 13 18:53:47 2014 +0200 @@ -1,6 +1,7 @@ import unittest from test.support import (verbose, refcount_test, run_unittest, - strip_python_stderr, cpython_only) + strip_python_stderr, cpython_only, + requires_type_collecting) from test.script_helper import assert_python_ok, make_script, temp_dir import sys @@ -135,10 +136,16 @@ class GCTests(unittest.TestCase): del a self.assertNotEqual(gc.collect(), 0) del B, C - self.assertNotEqual(gc.collect(), 0) + if hasattr(sys, 'getcounts'): + self.assertEqual(gc.collect(), 0) + else: + self.assertNotEqual(gc.collect(), 0) A.a = A() del A - self.assertNotEqual(gc.collect(), 0) + if hasattr(sys, 'getcounts'): + self.assertEqual(gc.collect(), 0) + else: + self.assertNotEqual(gc.collect(), 0) self.assertEqual(gc.collect(), 0) def test_method(self): @@ -613,6 +620,7 @@ class GCTests(unittest.TestCase): gc.collect() # this blows up (bad C pointer) when it fails @cpython_only + @requires_type_collecting def test_garbage_at_shutdown(self): import subprocess code = """if 1: @@ -661,6 +669,7 @@ class GCTests(unittest.TestCase): stderr = run_command(code % "gc.DEBUG_SAVEALL") self.assertNotIn(b"uncollectable objects at shutdown", stderr) + @requires_type_collecting def test_gc_main_module_at_shutdown(self): # Create a reference cycle through the __main__ module and check # it gets collected at interpreter shutdown. @@ -675,6 +684,7 @@ class GCTests(unittest.TestCase): rc, out, err = assert_python_ok('-c', code) self.assertEqual(out.strip(), b'__del__ called') + @requires_type_collecting def test_gc_ordinary_module_at_shutdown(self): # Same as above, but with a non-__main__ module. with temp_dir() as script_dir: diff -r 65eb12898d56 Lib/test/test_httpservers.py --- a/Lib/test/test_httpservers.py Fri Dec 12 12:27:27 2014 +0100 +++ b/Lib/test/test_httpservers.py Sat Dec 13 18:53:47 2014 +0200 @@ -481,6 +481,7 @@ class CGIHTTPServerTestCase(BaseTestCase msg='path = %r\nGot: %r\nWanted: %r' % (path, actual, expected)) + @support.requires_clean_stdout def test_headers_and_content(self): res = self.request('/cgi-bin/file1.py') self.assertEqual((b'Hello World' + self.linesep, 'text/html', 200), @@ -490,6 +491,7 @@ class CGIHTTPServerTestCase(BaseTestCase res = self.request('///////////nocgi.py/../cgi-bin/nothere.sh') self.assertEqual(res.status, 404) + @support.requires_clean_stdout def test_post(self): params = urllib.parse.urlencode( {'spam' : 1, 'eggs' : 'python', 'bacon' : 123456}) @@ -503,6 +505,7 @@ class CGIHTTPServerTestCase(BaseTestCase res.read() self.assertEqual(res.status, 404) + @support.requires_clean_stdout def test_authorization(self): headers = {b'Authorization' : b'Basic ' + base64.b64encode(b'username:pass')} @@ -510,12 +513,14 @@ class CGIHTTPServerTestCase(BaseTestCase self.assertEqual((b'Hello World' + self.linesep, 'text/html', 200), (res.read(), res.getheader('Content-type'), res.status)) + @support.requires_clean_stdout def test_no_leading_slash(self): # http://bugs.python.org/issue2254 res = self.request('cgi-bin/file1.py') self.assertEqual((b'Hello World' + self.linesep, 'text/html', 200), (res.read(), res.getheader('Content-type'), res.status)) + @support.requires_clean_stdout def test_os_environ_is_not_altered(self): signature = "Test CGI Server" os.environ['SERVER_SOFTWARE'] = signature @@ -524,11 +529,13 @@ class CGIHTTPServerTestCase(BaseTestCase (res.read(), res.getheader('Content-type'), res.status)) self.assertEqual(os.environ['SERVER_SOFTWARE'], signature) + @support.requires_clean_stdout def test_urlquote_decoding_in_cgi_check(self): res = self.request('/cgi-bin%2ffile1.py') self.assertEqual((b'Hello World' + self.linesep, 'text/html', 200), (res.read(), res.getheader('Content-type'), res.status)) + @support.requires_clean_stdout def test_nested_cgi_path_issue21323(self): res = self.request('/cgi-bin/child-dir/file3.py') self.assertEqual((b'Hello World' + self.linesep, 'text/html', 200), diff -r 65eb12898d56 Lib/test/test_io.py --- a/Lib/test/test_io.py Fri Dec 12 12:27:27 2014 +0100 +++ b/Lib/test/test_io.py Sat Dec 13 18:53:47 2014 +0200 @@ -2830,6 +2830,7 @@ class TextIOWrapperTest(unittest.TestCas """.format(iomod=iomod, kwargs=kwargs) return assert_python_ok("-c", code) + @support.requires_type_collecting def test_create_at_shutdown_without_encoding(self): rc, out, err = self._check_create_at_shutdown() if err: @@ -2839,6 +2840,7 @@ class TextIOWrapperTest(unittest.TestCas else: self.assertEqual("ok", out.decode().strip()) + @support.requires_type_collecting def test_create_at_shutdown_with_encoding(self): rc, out, err = self._check_create_at_shutdown(encoding='utf-8', errors='strict') diff -r 65eb12898d56 Lib/test/test_json/test_tool.py --- a/Lib/test/test_json/test_tool.py Fri Dec 12 12:27:27 2014 +0100 +++ b/Lib/test/test_json/test_tool.py Sat Dec 13 18:53:47 2014 +0200 @@ -65,6 +65,7 @@ class TestTool(unittest.TestCase): (sys.executable, '-m', 'json.tool'), stdin=subprocess.PIPE, stdout=subprocess.PIPE) as proc: out, err = proc.communicate(self.data.encode()) + out = support.strip_python_stdout(out) self.assertEqual(out.splitlines(), self.expect.encode().splitlines()) self.assertEqual(err, None) diff -r 65eb12898d56 Lib/test/test_logging.py --- a/Lib/test/test_logging.py Fri Dec 12 12:27:27 2014 +0100 +++ b/Lib/test/test_logging.py Sat Dec 13 18:53:47 2014 +0200 @@ -3373,6 +3373,7 @@ class ModuleLevelMiscTest(BaseTest): logging.setLoggerClass(logging.Logger) self.assertEqual(logging.getLoggerClass(), logging.Logger) + @support.requires_type_collecting def test_logging_at_shutdown(self): # Issue #20037 code = """if 1: diff -r 65eb12898d56 Lib/test/test_module.py --- a/Lib/test/test_module.py Fri Dec 12 12:27:27 2014 +0100 +++ b/Lib/test/test_module.py Sat Dec 13 18:53:47 2014 +0200 @@ -1,7 +1,7 @@ # Test the module type import unittest import weakref -from test.support import run_unittest, gc_collect +from test.support import run_unittest, gc_collect, requires_type_collecting from test.script_helper import assert_python_ok import sys @@ -101,6 +101,7 @@ class ModuleTests(unittest.TestCase): gc_collect() self.assertEqual(f().__dict__["bar"], 4) + @requires_type_collecting def test_clear_dict_in_ref_cycle(self): destroyed = [] m = ModuleType("foo") @@ -116,6 +117,7 @@ a = A(destroyed)""" gc_collect() self.assertEqual(destroyed, [1]) + @requires_type_collecting def test_weakref(self): m = ModuleType("foo") wr = weakref.ref(m) @@ -214,6 +216,7 @@ a = A(destroyed)""" self.assertEqual(r[-len(ends_with):], ends_with, '{!r} does not end with {!r}'.format(r, ends_with)) + @requires_type_collecting def test_module_finalization_at_shutdown(self): # Module globals and builtins should still be available during shutdown rc, out, err = assert_python_ok("-c", "from test import final_a") diff -r 65eb12898d56 Lib/test/test_os.py --- a/Lib/test/test_os.py Fri Dec 12 12:27:27 2014 +0100 +++ b/Lib/test/test_os.py Sat Dec 13 18:53:47 2014 +0200 @@ -1081,6 +1081,7 @@ class URandomTests(unittest.TestCase): data2 = os.urandom(16) self.assertNotEqual(data1, data2) + @support.requires_clean_stdout def get_urandom_subprocess(self, count): code = '\n'.join(( 'import os, sys', @@ -1132,6 +1133,7 @@ class URandomTests(unittest.TestCase): """ rc, out, err = assert_python_ok('-Sc', code) + @support.requires_clean_stdout def test_urandom_fd_reopened(self): # Issue #21207: urandom() should detect its fd to /dev/urandom # changed to something else, and reopen it. @@ -1934,6 +1936,7 @@ class PidTests(unittest.TestCase): 'import os; print(os.getppid())'], stdout=subprocess.PIPE) stdout, _ = p.communicate() + stdout = support.strip_python_stdout(stdout) # We are the parent of our subprocess self.assertEqual(int(stdout), os.getpid()) diff -r 65eb12898d56 Lib/test/test_pdb.py --- a/Lib/test/test_pdb.py Fri Dec 12 12:27:27 2014 +0100 +++ b/Lib/test/test_pdb.py Sat Dec 13 18:53:47 2014 +0200 @@ -1036,6 +1036,7 @@ class PdbTestCase(unittest.TestCase): ) self.addCleanup(proc.stdout.close) stdout, stderr = proc.communicate(b'cont\n') + stdout = support.strip_python_stdout(stdout) self.assertNotIn('Error', stdout.decode(), "Got an error running test script under PDB") diff -r 65eb12898d56 Lib/test/test_platform.py --- a/Lib/test/test_platform.py Fri Dec 12 12:27:27 2014 +0100 +++ b/Lib/test/test_platform.py Sat Dec 13 18:53:47 2014 +0200 @@ -25,7 +25,9 @@ class PlatformTest(unittest.TestCase): cmd = [python, '-c', 'import platform; print(platform.architecture())'] p = subprocess.Popen(cmd, stdout=subprocess.PIPE) - return p.communicate() + out, err = p.communicate() + out = support.strip_python_stdout(out) + return out, err real = os.path.realpath(sys.executable) link = os.path.abspath(support.TESTFN) os.symlink(real, link) @@ -275,9 +277,10 @@ class PlatformTest(unittest.TestCase): with warnings.catch_warnings(): warnings.simplefilter("ignore", DeprecationWarning) with platform.popen(command) as stdout: - hello = stdout.read().strip() + hello = stdout.read() + hello = os.fsdecode(support.strip_python_stdout(os.fsencode(hello))) stdout.close() - self.assertEqual(hello, "Hello") + self.assertEqual(hello.strip(), "Hello") data = 'plop' if mswindows: diff -r 65eb12898d56 Lib/test/test_popen.py --- a/Lib/test/test_popen.py Fri Dec 12 12:27:27 2014 +0100 +++ b/Lib/test/test_popen.py Sat Dec 13 18:53:47 2014 +0200 @@ -23,6 +23,7 @@ class PopenTest(unittest.TestCase): cmd = cmd % (python, cmdline) with os.popen(cmd) as p: data = p.read() + data = os.fsdecode(support.strip_python_stdout(os.fsencode(data))) got = eval(data)[1:] # strip off argv[0] self.assertEqual(got, expected) diff -r 65eb12898d56 Lib/test/test_quopri.py --- a/Lib/test/test_quopri.py Fri Dec 12 12:27:27 2014 +0100 +++ b/Lib/test/test_quopri.py Sat Dec 13 18:53:47 2014 +0200 @@ -180,6 +180,7 @@ zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz stdin=subprocess.PIPE, stdout=subprocess.PIPE) self.addCleanup(process.stdout.close) cout, cerr = process.communicate(p) + cout = support.strip_python_stdout(cout) # On Windows, Python will output the result to stdout using # CRLF, as the mode of stdout is text mode. To compare this # with the expected result, we need to do a line-by-line comparison. @@ -196,6 +197,7 @@ zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz stdin=subprocess.PIPE, stdout=subprocess.PIPE) self.addCleanup(process.stdout.close) cout, cerr = process.communicate(e) + cout = support.strip_python_stdout(cout) cout = cout.decode('latin-1') p = p.decode('latin-1') self.assertEqual(cout.splitlines(), p.splitlines()) diff -r 65eb12898d56 Lib/test/test_site.py --- a/Lib/test/test_site.py Fri Dec 12 12:27:27 2014 +0100 +++ b/Lib/test/test_site.py Sat Dec 13 18:53:47 2014 +0200 @@ -438,6 +438,7 @@ class StartupImportTests(unittest.TestCa stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = popen.communicate() + stdout = test.support.strip_python_stdout(stdout) stdout = stdout.decode('utf-8') stderr = stderr.decode('utf-8') modules = eval(stdout) diff -r 65eb12898d56 Lib/test/test_subprocess.py --- a/Lib/test/test_subprocess.py Fri Dec 12 12:27:27 2014 +0100 +++ b/Lib/test/test_subprocess.py Sat Dec 13 18:53:47 2014 +0200 @@ -266,6 +266,7 @@ class ProcessTestCase(BaseTestCase): self.addCleanup(p.stdout.close) self.addCleanup(p.stderr.close) out, err = p.communicate() + out = support.strip_python_stdout(out) self.assertEqual(p.returncode, 0, err) self.assertEqual(out.rstrip(), b'test_stdout_none') @@ -330,6 +331,7 @@ class ProcessTestCase(BaseTestCase): return os.path.split(python_path) # For use in the test_cwd* tests below. + @support.requires_clean_stdout def _assert_cwd(self, expected_cwd, python_arg, **kwargs): # Invoke Python via Popen, and assert that (1) the call succeeds, # and that (2) the current working directory of the child process @@ -455,6 +457,7 @@ class ProcessTestCase(BaseTestCase): p.wait() self.assertEqual(p.returncode, 1) + @support.requires_clean_stdout def test_stdout_pipe(self): # stdout redirection p = subprocess.Popen([sys.executable, "-c", @@ -463,6 +466,7 @@ class ProcessTestCase(BaseTestCase): self.addCleanup(p.stdout.close) self.assertEqual(p.stdout.read(), b"orange") + @support.requires_clean_stdout def test_stdout_filedes(self): # stdout is set to open file descriptor tf = tempfile.TemporaryFile() @@ -475,6 +479,7 @@ class ProcessTestCase(BaseTestCase): os.lseek(d, 0, 0) self.assertEqual(os.read(d, 1024), b"orange") + @support.requires_clean_stdout def test_stdout_fileobj(self): # stdout is set to open file object tf = tempfile.TemporaryFile() @@ -517,6 +522,7 @@ class ProcessTestCase(BaseTestCase): tf.seek(0) self.assertStderrEqual(tf.read(), b"strawberry") + @support.requires_clean_stdout def test_stdout_stderr_pipe(self): # capture stdout and stderr to the same pipe p = subprocess.Popen([sys.executable, "-c", @@ -529,6 +535,7 @@ class ProcessTestCase(BaseTestCase): self.addCleanup(p.stdout.close) self.assertStderrEqual(p.stdout.read(), b"appleorange") + @support.requires_clean_stdout def test_stdout_stderr_file(self): # capture stdout and stderr to the same open file tf = tempfile.TemporaryFile() @@ -544,6 +551,7 @@ class ProcessTestCase(BaseTestCase): tf.seek(0) self.assertStderrEqual(tf.read(), b"appleorange") + @support.requires_clean_stdout def test_stdout_filedes_of_stdout(self): # stdout is set to 1 (#1531862). # To avoid printing the text on stdout, we do something similar to @@ -588,6 +596,7 @@ class ProcessTestCase(BaseTestCase): p.wait() self.assertEqual(p.stdin, None) + @support.requires_clean_stdout def test_env(self): newenv = os.environ.copy() newenv["FRUIT"] = "orange" @@ -603,6 +612,7 @@ class ProcessTestCase(BaseTestCase): # Python @unittest.skipIf(sys.platform == 'win32', 'cannot test an empty env on Windows') + @support.requires_clean_stdout @unittest.skipIf(sysconfig.get_config_var('Py_ENABLE_SHARED') is not None, 'the python library cannot be loaded ' 'with an empty environment') @@ -627,6 +637,7 @@ class ProcessTestCase(BaseTestCase): p.communicate(b"pear") self.assertEqual(p.returncode, 1) + @support.requires_clean_stdout def test_communicate_stdout(self): p = subprocess.Popen([sys.executable, "-c", 'import sys; sys.stdout.write("pineapple")'], @@ -643,6 +654,7 @@ class ProcessTestCase(BaseTestCase): self.assertEqual(stdout, None) self.assertStderrEqual(stderr, b"pineapple") + @support.requires_clean_stdout def test_communicate(self): p = subprocess.Popen([sys.executable, "-c", 'import sys,os;' @@ -658,6 +670,7 @@ class ProcessTestCase(BaseTestCase): self.assertEqual(stdout, b"banana") self.assertStderrEqual(stderr, b"pineapple") + @support.requires_clean_stdout def test_communicate_timeout(self): p = subprocess.Popen([sys.executable, "-c", 'import sys,os,time;' @@ -677,6 +690,7 @@ class ProcessTestCase(BaseTestCase): self.assertEqual(stdout, "banana") self.assertStderrEqual(stderr.encode(), b"pineapple\npear\n") + @support.requires_clean_stdout def test_communicate_timeout_large_ouput(self): # Test an expiring timeout while the child is outputting lots of data. p = subprocess.Popen([sys.executable, "-c", @@ -724,6 +738,7 @@ class ProcessTestCase(BaseTestCase): self.assertEqual(stdout, None) self.assertEqual(stderr, None) + @support.requires_clean_stdout def test_communicate_pipe_buf(self): # communicate() with writes larger than pipe_buf # This test will probably deadlock rather than fail, if @@ -747,6 +762,7 @@ class ProcessTestCase(BaseTestCase): (stdout, stderr) = p.communicate(string_to_write) self.assertEqual(stdout, string_to_write) + @support.requires_clean_stdout def test_writes_before_communicate(self): # stdin.write before communicate() p = subprocess.Popen([sys.executable, "-c", @@ -763,6 +779,7 @@ class ProcessTestCase(BaseTestCase): self.assertEqual(stdout, b"bananasplit") self.assertStderrEqual(stderr, b"") + @support.requires_clean_stdout def test_universal_newlines(self): p = subprocess.Popen([sys.executable, "-c", 'import sys,os;' + SETBINARY + @@ -798,6 +815,7 @@ class ProcessTestCase(BaseTestCase): self.assertEqual(p.stdout.read(), "line4\nline5\nline6\nline7\nline8") + @support.requires_clean_stdout def test_universal_newlines_communicate(self): # universal newlines through communicate() p = subprocess.Popen([sys.executable, "-c", @@ -849,6 +867,7 @@ class ProcessTestCase(BaseTestCase): p.communicate() self.assertEqual(p.returncode, 0) + @support.requires_clean_stdout def test_universal_newlines_communicate_stdin_stdout_stderr(self): # universal newlines through communicate(), with stdin, stdout, stderr p = subprocess.Popen([sys.executable, "-c", @@ -878,6 +897,7 @@ class ProcessTestCase(BaseTestCase): # Don't use assertStderrEqual because it strips CR and LF from output. self.assertTrue(stderr.startswith("eline2\neline6\neline7\n")) + @support.requires_clean_stdout def test_universal_newlines_communicate_encodings(self): # Check that universal newlines mode works for various encodings, # in particular for encodings in the UTF-16 and UTF-32 families. @@ -910,6 +930,7 @@ class ProcessTestCase(BaseTestCase): _bootlocale.getpreferredencoding = old_getpreferredencoding self.assertEqual(stdout, '1\n2\n3\n4') + @support.requires_clean_stdout def test_no_leaking(self): # Make sure we leak no resources if not mswindows: @@ -1008,6 +1029,7 @@ class ProcessTestCase(BaseTestCase): p = subprocess.Popen([sys.executable, "-c", "pass"], bufsize=None) self.assertEqual(p.wait(), 0) + @support.requires_clean_stdout def _test_bufsize_equal_one(self, line, expected, universal_newlines): # subprocess may deadlock with bufsize=1, see issue #21332 with subprocess.Popen([sys.executable, "-c", "import sys;" @@ -1310,6 +1332,7 @@ class POSIXProcessTestCase(BaseTestCase) [sys.executable, "-c", "import os; print(os.getpgid(os.getpid()))"], start_new_session=True) + output = support.strip_python_stdout(output) except OSError as e: if e.errno != errno.EPERM: raise @@ -1326,6 +1349,7 @@ class POSIXProcessTestCase(BaseTestCase) p.wait() self.assertEqual(-p.returncode, signal.SIGABRT) + @support.requires_clean_stdout def test_preexec(self): # DISCLAIMER: Setting environment variables is *not* a good use # of a preexec_fn. This is merely a test. @@ -1598,6 +1622,7 @@ class POSIXProcessTestCase(BaseTestCase) os.dup2(saved, fd, inheritable=inheritable) os.close(saved) + @support.requires_clean_stdout def check_close_std_fds(self, fds): # Issue #9905: test that subprocess pipes still work properly with # some standard fds closed @@ -1667,6 +1692,7 @@ class POSIXProcessTestCase(BaseTestCase) os.close(new_stdin) os.close(new_stdout) + @support.requires_clean_stdout def test_remapping_std_fds(self): # open up some temporary files temps = [mkstemp() for i in range(3)] @@ -1712,6 +1738,7 @@ class POSIXProcessTestCase(BaseTestCase) for fd in temp_fds: os.close(fd) + @support.requires_clean_stdout def check_swap_fds(self, stdin_no, stdout_no, stderr_no): # open up some temporary files temps = [mkstemp() for i in range(3)] @@ -1808,6 +1835,7 @@ class POSIXProcessTestCase(BaseTestCase) stdout = subprocess.check_output( [sys.executable, "-c", script], env=env) + stdout = support.strip_python_stdout(stdout) stdout = stdout.rstrip(b'\n\r') self.assertEqual(stdout.decode('ascii'), ascii(decoded_value)) @@ -1819,6 +1847,7 @@ class POSIXProcessTestCase(BaseTestCase) stdout = subprocess.check_output( [sys.executable, "-c", script], env=env) + stdout = support.strip_python_stdout(stdout) stdout = stdout.rstrip(b'\n\r') self.assertEqual(stdout.decode('ascii'), ascii(encoded_value)) @@ -1862,6 +1891,7 @@ class POSIXProcessTestCase(BaseTestCase) stdout=subprocess.PIPE, close_fds=False) output, error = p2.communicate() + output = support.strip_python_stdout(output) result_fds = set(map(int, output.split(b','))) unwanted_fds = set([p1.stdin.fileno(), p1.stdout.fileno(), p1.stderr.fileno()]) @@ -1907,7 +1937,9 @@ class POSIXProcessTestCase(BaseTestCase) readfiles, ignored1, ignored2 = select.select([p2.stdout], [], [], 10) self.assertTrue(readfiles, "The child hung") - self.assertEqual(p2.stdout.read(), data) + out = p2.stdout.read() + out = support.strip_python_stdout(out) + self.assertEqual(out, data) p1.stdout.close() p2.stdout.close() @@ -1932,6 +1964,7 @@ class POSIXProcessTestCase(BaseTestCase) p = subprocess.Popen([sys.executable, fd_status], stdout=subprocess.PIPE, close_fds=False) output, ignored = p.communicate() + output = support.strip_python_stdout(output) remaining_fds = set(map(int, output.split(b','))) self.assertEqual(remaining_fds & open_fds, open_fds, @@ -1940,6 +1973,7 @@ class POSIXProcessTestCase(BaseTestCase) p = subprocess.Popen([sys.executable, fd_status], stdout=subprocess.PIPE, close_fds=True) output, ignored = p.communicate() + output = support.strip_python_stdout(output) remaining_fds = set(map(int, output.split(b','))) self.assertFalse(remaining_fds & open_fds, @@ -1953,6 +1987,7 @@ class POSIXProcessTestCase(BaseTestCase) stdout=subprocess.PIPE, close_fds=True, pass_fds=()) output, ignored = p.communicate() + output = support.strip_python_stdout(output) remaining_fds = set(map(int, output.split(b','))) self.assertFalse(remaining_fds & fds_to_keep & open_fds, @@ -2029,6 +2064,7 @@ class POSIXProcessTestCase(BaseTestCase) ''' % fd_status)], stdout=subprocess.PIPE) output, unused_stderr = p.communicate() + output = support.strip_python_stdout(output) output_lines = output.splitlines() self.assertEqual(len(output_lines), 2, msg="expected exactly two lines of output:\n%r" % output) @@ -2062,6 +2098,7 @@ class POSIXProcessTestCase(BaseTestCase) stdout=subprocess.PIPE, close_fds=True, pass_fds=(fd, )) output, ignored = p.communicate() + output = support.strip_python_stdout(output) remaining_fds = set(map(int, output.split(b','))) to_be_closed = open_fds - {fd} @@ -2093,6 +2130,7 @@ class POSIXProcessTestCase(BaseTestCase) stdout=subprocess.PIPE, close_fds=True, pass_fds=pass_fds) output, ignored = p.communicate() + output = support.strip_python_stdout(output) fds = set(map(int, output.split(b','))) # the inheritable file descriptor must be inherited, so its inheritable @@ -2211,6 +2249,7 @@ class POSIXProcessTestCase(BaseTestCase) stdout=subprocess.PIPE, close_fds=True, preexec_fn=lambda: os.dup2(1, fd)) output, ignored = p.communicate() + output = support.strip_python_stdout(output) remaining_fds = set(map(int, output.split(b','))) @@ -2486,6 +2525,7 @@ class CommandsWithSpaces (BaseTestCase): class ContextManagerTests(BaseTestCase): + @support.requires_clean_stdout def test_pipe(self): with subprocess.Popen([sys.executable, "-c", "import sys;" diff -r 65eb12898d56 Lib/test/test_sys.py --- a/Lib/test/test_sys.py Fri Dec 12 12:27:27 2014 +0100 +++ b/Lib/test/test_sys.py Sat Dec 13 18:53:47 2014 +0200 @@ -551,21 +551,24 @@ class SysModuleTest(unittest.TestCase): env["PYTHONIOENCODING"] = "cp424" p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'], stdout = subprocess.PIPE, env=env) - out = p.communicate()[0].strip() + out = p.communicate()[0] + out = test.support.strip_python_stdout(out) expected = ("\xa2" + os.linesep).encode("cp424") - self.assertEqual(out, expected) + self.assertEqual(out.strip(), expected) env["PYTHONIOENCODING"] = "ascii:replace" p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'], stdout = subprocess.PIPE, env=env) - out = p.communicate()[0].strip() - self.assertEqual(out, b'?') + out = p.communicate()[0] + out = test.support.strip_python_stdout(out) + self.assertEqual(out.strip(), b'?') env["PYTHONIOENCODING"] = "ascii" p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) out, err = p.communicate() + out = test.support.strip_python_stdout(out) self.assertEqual(out, b'') self.assertIn(b'UnicodeEncodeError:', err) self.assertIn(rb"'\xa2'", err) @@ -575,6 +578,7 @@ class SysModuleTest(unittest.TestCase): stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) out, err = p.communicate() + out = test.support.strip_python_stdout(out) self.assertEqual(out, b'') self.assertIn(b'UnicodeEncodeError:', err) self.assertIn(rb"'\xa2'", err) @@ -582,8 +586,9 @@ class SysModuleTest(unittest.TestCase): env["PYTHONIOENCODING"] = ":surrogateescape" p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xdcbd))'], stdout=subprocess.PIPE, env=env) - out = p.communicate()[0].strip() - self.assertEqual(out, b'\xbd') + out = p.communicate()[0] + out = test.support.strip_python_stdout(out) + self.assertEqual(out.strip(), b'\xbd') @unittest.skipUnless(test.support.FS_NONASCII, 'requires OS support of non-ASCII encodings') @@ -594,8 +599,9 @@ class SysModuleTest(unittest.TestCase): p = subprocess.Popen([sys.executable, "-c", 'print(%a)' % test.support.FS_NONASCII], stdout=subprocess.PIPE, env=env) - out = p.communicate()[0].strip() - self.assertEqual(out, os.fsencode(test.support.FS_NONASCII)) + out = p.communicate()[0] + out = test.support.strip_python_stdout(out) + self.assertEqual(out.strip(), os.fsencode(test.support.FS_NONASCII)) @unittest.skipIf(sys.base_prefix != sys.prefix, 'Test is not venv-compatible') @@ -615,6 +621,7 @@ class SysModuleTest(unittest.TestCase): 'import sys; print(sys.executable.encode("ascii", "backslashreplace"))'], executable=sys.executable, stdout=subprocess.PIPE, cwd=python_dir) stdout = p.communicate()[0] + stdout = test.support.strip_python_stdout(stdout) executable = stdout.strip().decode("ASCII") p.wait() self.assertIn(executable, ["b''", repr(sys.executable.encode("ascii", "backslashreplace"))]) @@ -659,6 +666,7 @@ class SysModuleTest(unittest.TestCase): env=env, universal_newlines=True) stdout, stderr = p.communicate() + stdout = os.fsdecode(test.support.strip_python_stdout(os.fsencode(stdout))) return stdout def test_c_locale_surrogateescape(self): @@ -744,6 +752,7 @@ class SysModuleTest(unittest.TestCase): c = sys.getallocatedblocks() self.assertIn(c, range(b - 50, b + 50)) + @test.support.requires_type_collecting def test_is_finalizing(self): self.assertIs(sys.is_finalizing(), False) # Don't use the atexit module because _Py_Finalizing is only set @@ -1020,12 +1029,14 @@ class SizeofTest(unittest.TestCase): check((), vsize('')) check((1,2,3), vsize('') + 3*self.P) # type + alloc_counts = '3n2P' if hasattr(sys, 'getcounts') else '' # static type: PyTypeObject - s = vsize('P2n15Pl4Pn9Pn11PIP') + s = vsize('P2n15Pl4Pn9Pn11PIP' + alloc_counts) check(int, s) # (PyTypeObject + PyNumberMethods + PyMappingMethods + # PySequenceMethods + PyBufferProcs + 4P) - s = vsize('P2n17Pl4Pn9Pn11PIP') + struct.calcsize('34P 3P 10P 2P 4P') + s = vsize('P2n17Pl4Pn9Pn11PIP' + alloc_counts) + s += struct.calcsize('34P 3P 10P 2P 4P') # Separate block for PyDictKeysObject with 4 entries s += struct.calcsize("2nPn") + 4*struct.calcsize("n2P") # class diff -r 65eb12898d56 Lib/test/test_sysconfig.py --- a/Lib/test/test_sysconfig.py Fri Dec 12 12:27:27 2014 +0100 +++ b/Lib/test/test_sysconfig.py Sat Dec 13 18:53:47 2014 +0200 @@ -6,7 +6,8 @@ import shutil from copy import copy from test.support import (run_unittest, TESTFN, unlink, check_warnings, - captured_stdout, skip_unless_symlink) + captured_stdout, skip_unless_symlink, + strip_python_stdout) import sysconfig from sysconfig import (get_paths, get_platform, get_config_vars, @@ -251,7 +252,9 @@ class TestSysConfig(unittest.TestCase): cmd = [python, '-c', 'import sysconfig; print(sysconfig.get_platform())'] p = subprocess.Popen(cmd, stdout=subprocess.PIPE, env=os.environ) - return p.communicate() + out, err = p.communicate() + out = strip_python_stdout(out) + return out, err real = os.path.realpath(sys.executable) link = os.path.abspath(TESTFN) os.symlink(real, link) diff -r 65eb12898d56 Lib/test/test_threading.py --- a/Lib/test/test_threading.py Fri Dec 12 12:27:27 2014 +0100 +++ b/Lib/test/test_threading.py Sat Dec 13 18:53:47 2014 +0200 @@ -988,6 +988,7 @@ class ThreadingExceptionTests(BaseTestCa self.assertIn("ZeroDivisionError", err) self.assertNotIn("Unhandled exception", err) + @test.support.requires_type_collecting def test_print_exception_stderr_is_none_1(self): script = r"""if True: import sys diff -r 65eb12898d56 Lib/test/test_tools/test_pindent.py --- a/Lib/test/test_tools/test_pindent.py Fri Dec 12 12:27:27 2014 +0100 +++ b/Lib/test/test_tools/test_pindent.py Sat Dec 13 18:53:47 2014 +0200 @@ -26,6 +26,7 @@ class PindentTests(unittest.TestCase): stdin=subprocess.PIPE, stdout=subprocess.PIPE, universal_newlines=True) as proc: out, err = proc.communicate(source) + out = os.fsdecode(support.strip_python_stdout(os.fsencode(out))) self.assertIsNone(err) return out diff -r 65eb12898d56 Lib/test/test_traceback.py --- a/Lib/test/test_traceback.py Fri Dec 12 12:27:27 2014 +0100 +++ b/Lib/test/test_traceback.py Sat Dec 13 18:53:47 2014 +0200 @@ -4,6 +4,7 @@ from io import StringIO import sys import unittest import re +from test import support from test.support import run_unittest, Error, captured_output from test.support import TESTFN, unlink, cpython_only from test.script_helper import assert_python_ok @@ -171,6 +172,7 @@ class SyntaxTracebackCases(unittest.Test # Issue #18960: coding spec should has no effect do_test("0\n# coding: GBK\n", "h\xe9 ho", 'utf-8', 5) + @support.requires_type_collecting def test_print_traceback_at_exit(self): # Issue #22599: Ensure that it is possible to use the traceback module # to display an exception at Python exit diff -r 65eb12898d56 Lib/test/test_venv.py --- a/Lib/test/test_venv.py Fri Dec 12 12:27:27 2014 +0100 +++ b/Lib/test/test_venv.py Sat Dec 13 18:53:47 2014 +0200 @@ -13,7 +13,8 @@ import subprocess import sys import tempfile from test.support import (captured_stdout, captured_stderr, run_unittest, - can_symlink, EnvironmentVarGuard, rmtree) + can_symlink, EnvironmentVarGuard, rmtree, + strip_python_stdout) import textwrap import unittest import venv @@ -133,6 +134,7 @@ class BasicTest(BaseTest): p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = p.communicate() + out = strip_python_stdout(out) self.assertEqual(out.strip(), expected.encode()) if sys.platform == 'win32': @@ -260,6 +262,7 @@ class BasicTest(BaseTest): p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = p.communicate() + out = strip_python_stdout(out) self.assertEqual(out.strip(), envpy.encode()) @unittest.skipUnless(can_symlink(), 'Needs symlinks') @@ -275,6 +278,7 @@ class BasicTest(BaseTest): p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = p.communicate() + out = strip_python_stdout(out) self.assertEqual(out.strip(), envpy.encode()) @@ -293,6 +297,7 @@ class EnsurePipTest(BaseTest): # if we get unexpected results err = err.decode("latin-1") # Force to text, prevent decoding errors self.assertEqual(err, "") + out = strip_python_stdout(out) out = out.decode("latin-1") # Force to text, prevent decoding errors self.assertEqual(out.strip(), "OK") diff -r 65eb12898d56 Lib/test/test_warnings.py --- a/Lib/test/test_warnings.py Fri Dec 12 12:27:27 2014 +0100 +++ b/Lib/test/test_warnings.py Sat Dec 13 18:53:47 2014 +0200 @@ -900,6 +900,7 @@ class BootstrapTest(unittest.TestCase): assert_python_ok('-c', 'pass', '-W', 'always', PYTHONPATH=cwd) class FinalizationTest(unittest.TestCase): + @support.requires_type_collecting def test_finalization(self): # Issue #19421: warnings.warn() should not crash # during Python finalization diff -r 65eb12898d56 Lib/test/test_weakref.py --- a/Lib/test/test_weakref.py Fri Dec 12 12:27:27 2014 +0100 +++ b/Lib/test/test_weakref.py Sat Dec 13 18:53:47 2014 +0200 @@ -584,6 +584,7 @@ class ReferencesTestCase(TestBase): del c1, c2, C, D gc.collect() + @support.requires_type_collecting def test_callback_in_cycle_resurrection(self): import gc diff -r 65eb12898d56 Lib/unittest/test/test_runner.py --- a/Lib/unittest/test/test_runner.py Fri Dec 12 12:27:27 2014 +0100 +++ b/Lib/unittest/test/test_runner.py Sat Dec 13 18:53:47 2014 +0200 @@ -9,6 +9,7 @@ from unittest.case import _Outcome from unittest.test.support import (LoggingResult, ResultWithNoStartTestRunStopTestRun) +import test.support class TestCleanUp(unittest.TestCase): @@ -278,7 +279,9 @@ class Test_TextTestRunner(unittest.TestC # see #10535 and the _test_warnings file for more information def get_parse_out_err(p): - return [b.splitlines() for b in p.communicate()] + out, err = p.communicate() + out = test.support.strip_python_stdout(out) + return [out.splitlines(), err.splitlines()] opts = dict(stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=os.path.dirname(__file__)) ae_msg = b'Please use assertEqual instead.'