Skip to content

Commit 0d289d4

Browse files
committed
Revert changes
1 parent 9f3100d commit 0d289d4

File tree

19 files changed

+124
-84
lines changed

19 files changed

+124
-84
lines changed

Lib/test/test_argparse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class StdStreamTest(unittest.TestCase):
5656
def test_skip_invalid_stderr(self):
5757
parser = argparse.ArgumentParser()
5858
with (
59-
captured_stderr(),
59+
contextlib.redirect_stderr(None),
6060
mock.patch('argparse._sys.exit')
6161
):
6262
parser.exit(status=0, message='foo')

Lib/test/test_compile.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import contextlib
12
import dis
3+
import io
24
import itertools
35
import math
46
import opcode
@@ -965,7 +967,8 @@ class C:
965967
for mode in ["exec", "single"]:
966968
with self.subTest(opt=opt, mode=mode):
967969
code = compile(src, "<test>", mode, optimize=opt)
968-
with support.captured_stdout() as output:
970+
output = io.StringIO()
971+
with contextlib.redirect_stdout(output):
969972
dis.dis(code)
970973
self.assertNotIn('NOP', output.getvalue())
971974

Lib/test/test_compileall.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import contextlib
33
import filecmp
44
import importlib.util
5+
import io
56
import os
67
import py_compile
78
import shutil
@@ -88,7 +89,7 @@ def test_year_2038_mtime_compilation(self):
8889
os.utime(self.source_path, (2**32 - 1, 2**32 - 1))
8990
except (OverflowError, OSError):
9091
self.skipTest("filesystem doesn't support timestamps near 2**32")
91-
with support.captured_stdout():
92+
with contextlib.redirect_stdout(io.StringIO()):
9293
self.assertTrue(compileall.compile_file(self.source_path))
9394

9495
def test_larger_than_32_bit_times(self):
@@ -98,7 +99,7 @@ def test_larger_than_32_bit_times(self):
9899
os.utime(self.source_path, (2**35, 2**35))
99100
except (OverflowError, OSError):
100101
self.skipTest("filesystem doesn't support large timestamps")
101-
with support.captured_stdout():
102+
with contextlib.redirect_stdout(io.StringIO()):
102103
self.assertTrue(compileall.compile_file(self.source_path))
103104

104105
def recreation_check(self, metadata):
@@ -205,7 +206,7 @@ def test_no_pycache_in_non_package(self):
205206
def test_compile_file_encoding_fallback(self):
206207
# Bug 44666 reported that compile_file failed when sys.stdout.encoding is None
207208
self.add_bad_source_file()
208-
with support.captured_stdout():
209+
with contextlib.redirect_stdout(io.StringIO()):
209210
self.assertFalse(compileall.compile_file(self.bad_source_path))
210211

211212

@@ -509,7 +510,8 @@ def tearDown(self):
509510
shutil.rmtree(self.directory)
510511

511512
def test_error(self):
512-
with support.captured_stdout() as buffer:
513+
buffer = io.TextIOWrapper(io.BytesIO(), encoding='ascii')
514+
with contextlib.redirect_stdout(buffer):
513515
compiled = compileall.compile_dir(self.directory)
514516
self.assertFalse(compiled) # should not be successful
515517
buffer.seek(0)

Lib/test/test_concurrent_futures/test_interpreter_pool.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import _thread
22
import asyncio
33
import contextlib
4+
import io
45
import os
56
import subprocess
67
import sys
@@ -198,14 +199,14 @@ def init2():
198199
nonlocal count
199200
count += 1
200201

201-
with support.captured_stderr() as stderr:
202+
with contextlib.redirect_stderr(io.StringIO()) as stderr:
202203
with self.executor_type(initializer=init1) as executor:
203204
fut = executor.submit(lambda: None)
204205
self.assertIn('NotShareableError', stderr.getvalue())
205206
with self.assertRaises(BrokenInterpreterPool):
206207
fut.result()
207208

208-
with support.captured_stderr() as stderr:
209+
with contextlib.redirect_stderr(io.StringIO()) as stderr:
209210
with self.executor_type(initializer=init2) as executor:
210211
fut = executor.submit(lambda: None)
211212
self.assertIn('NotShareableError', stderr.getvalue())
@@ -218,7 +219,7 @@ def initializer(self):
218219
raise NotImplementedError
219220
spam = Spam()
220221

221-
with support.captured_stderr() as stderr:
222+
with contextlib.redirect_stderr(io.StringIO()) as stderr:
222223
with self.executor_type(initializer=spam.initializer) as executor:
223224
fut = executor.submit(lambda: None)
224225
self.assertIn('NotShareableError', stderr.getvalue())
@@ -229,7 +230,7 @@ def initializer(self):
229230
def test_init_exception_in_script(self):
230231
executor = self.executor_type(initializer='raise Exception("spam")')
231232
with executor:
232-
with support.captured_stderr() as stderr:
233+
with contextlib.redirect_stderr(io.StringIO()) as stderr:
233234
fut = executor.submit('pass')
234235
with self.assertRaises(BrokenInterpreterPool):
235236
fut.result()
@@ -243,7 +244,7 @@ def test_init_exception_in_func(self):
243244
executor = self.executor_type(initializer=fail,
244245
initargs=(Exception, 'spam'))
245246
with executor:
246-
with support.captured_stderr() as stderr:
247+
with contextlib.redirect_stderr(io.StringIO()) as stderr:
247248
fut = executor.submit(noop)
248249
with self.assertRaises(BrokenInterpreterPool):
249250
fut.result()

Lib/test/test_descr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1295,7 +1295,7 @@ def __init__(self):
12951295
def __del__(self_):
12961296
self.assertEqual(self_.a, 1)
12971297
self.assertEqual(self_.b, 2)
1298-
with support.captured_stderr() as s:
1298+
with support.captured_output('stderr') as s:
12991299
h = H()
13001300
del h
13011301
self.assertEqual(s.getvalue(), '')

Lib/test/test_dis.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Minimal tests for dis module
22

33
import ast
4+
import contextlib
45
import dis
56
import functools
67
import io
@@ -12,9 +13,9 @@
1213
import textwrap
1314
import types
1415
import unittest
15-
from test.support import (captured_stderr, captured_stdout,
16-
requires_debug_ranges, requires_specialization,
17-
cpython_only, os_helper, import_helper, reset_code)
16+
from test.support import (captured_stdout, requires_debug_ranges,
17+
requires_specialization, cpython_only,
18+
os_helper, import_helper, reset_code)
1819
from test.support.bytecode_helper import BytecodeTestCase
1920

2021

@@ -970,7 +971,8 @@ class DisTests(DisTestBase):
970971

971972
def get_disassembly(self, func, lasti=-1, wrapper=True, **kwargs):
972973
# We want to test the default printing behaviour, not the file arg
973-
with captured_stdout() as output:
974+
output = io.StringIO()
975+
with contextlib.redirect_stdout(output):
974976
if wrapper:
975977
dis.dis(func, **kwargs)
976978
else:
@@ -986,7 +988,8 @@ def do_disassembly_test(self, func, expected, **kwargs):
986988
self.do_disassembly_compare(got, expected)
987989
# Add checks for dis.disco
988990
if hasattr(func, '__code__'):
989-
with captured_stdout() as got_disco:
991+
got_disco = io.StringIO()
992+
with contextlib.redirect_stdout(got_disco):
990993
dis.disco(func.__code__, **kwargs)
991994
self.do_disassembly_compare(got_disco.getvalue(), expected)
992995

@@ -1706,7 +1709,8 @@ def _stringify_instruction(instr):
17061709
return base + "),"
17071710

17081711
def _prepare_test_cases():
1709-
with captured_stdout():
1712+
ignore = io.StringIO()
1713+
with contextlib.redirect_stdout(ignore):
17101714
f = outer()
17111715
inner = f()
17121716
_instructions_outer = dis.get_instructions(outer, first_line=expected_outer_line)
@@ -2424,7 +2428,8 @@ def setUp(self) -> None:
24242428
return super().setUp()
24252429

24262430
def get_disassembly(self, tb):
2427-
with captured_stdout() as output:
2431+
output = io.StringIO()
2432+
with contextlib.redirect_stdout(output):
24282433
dis.distb(tb)
24292434
return output.getvalue()
24302435

@@ -2450,7 +2455,8 @@ def test_distb_explicit_arg(self):
24502455
class TestDisTracebackWithFile(TestDisTraceback):
24512456
# Run the `distb` tests again, using the file arg instead of print
24522457
def get_disassembly(self, tb):
2453-
with captured_stdout() as output:
2458+
output = io.StringIO()
2459+
with contextlib.redirect_stdout(output):
24542460
dis.distb(tb, file=output)
24552461
return output.getvalue()
24562462

@@ -2500,7 +2506,8 @@ def set_source(self, content):
25002506
fp.write(self.text_normalize(content))
25012507

25022508
def invoke_dis(self, *flags):
2503-
with captured_stdout() as output:
2509+
output = io.StringIO()
2510+
with contextlib.redirect_stdout(output):
25042511
dis.main(args=[*flags, self.filename])
25052512
return self.text_normalize(output.getvalue())
25062513

@@ -2534,7 +2541,7 @@ def f():
25342541

25352542
with self.assertRaises(SystemExit):
25362543
# suppress argparse error message
2537-
with captured_stderr():
2544+
with contextlib.redirect_stderr(io.StringIO()):
25382545
_ = self.invoke_dis('--unknown')
25392546

25402547
def test_show_cache(self):

Lib/test/test_future_stmt/test_future_multiple_features.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def test_unicode_literals(self):
1212
self.assertIsInstance("", str)
1313

1414
def test_print_function(self):
15-
with support.captured_stderr() as s:
15+
with support.captured_output("stderr") as s:
1616
print("foo", file=sys.stderr)
1717
self.assertEqual(s.getvalue(), "foo\n")
1818

Lib/test/test_itertools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ def __next__(self):
879879
def run(r1, r2):
880880
result = []
881881
for i, j in zip_longest(r1, r2, fillvalue=0):
882-
with support.captured_stdout():
882+
with support.captured_output('stdout'):
883883
print((i, j))
884884
result.append((i, j))
885885
return result

Lib/test/test_pdb.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import zipfile
1818

1919
from asyncio.events import _set_event_loop_policy
20-
from contextlib import ExitStack
20+
from contextlib import ExitStack, redirect_stdout
2121
from io import StringIO
2222
from test import support
2323
from test.support import has_socket_support, os_helper
@@ -4571,7 +4571,7 @@ def test_checkline_is_not_executable(self):
45714571
with open(os_helper.TESTFN, "w") as f:
45724572
f.write(s)
45734573
num_lines = len(s.splitlines()) + 2 # Test for EOF
4574-
with support.captured_stdout():
4574+
with redirect_stdout(StringIO()):
45754575
db = pdb.Pdb()
45764576
for lineno in range(num_lines):
45774577
self.assertFalse(db.checkline(os_helper.TESTFN, lineno))

Lib/test/test_platform.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import contextlib
12
import copy
3+
import io
24
import itertools
35
import os
46
import pickle
@@ -755,15 +757,17 @@ def setUp(self):
755757
self.addCleanup(platform.invalidate_caches)
756758

757759
def invoke_platform(self, *flags):
758-
with support.captured_stdout() as output:
760+
output = io.StringIO()
761+
with contextlib.redirect_stdout(output):
759762
platform._main(args=flags)
760763
return output.getvalue()
761764

762765
@support.force_not_colorized
763766
def test_unknown_flag(self):
767+
output = io.StringIO()
764768
with self.assertRaises(SystemExit):
765769
# suppress argparse error message
766-
with support.captured_stderr() as output:
770+
with contextlib.redirect_stderr(output):
767771
_ = self.invoke_platform('--unknown')
768772
self.assertStartsWith(output.getvalue(), "usage: ")
769773

@@ -800,9 +804,12 @@ def test_arg_parsing(self):
800804

801805
@support.force_not_colorized
802806
def test_help(self):
807+
output = io.StringIO()
808+
803809
with self.assertRaises(SystemExit):
804-
with support.captured_stdout() as output:
810+
with contextlib.redirect_stdout(output):
805811
platform._main(args=["--help"])
812+
806813
self.assertStartsWith(output.getvalue(), "usage:")
807814

808815

0 commit comments

Comments
 (0)