diff --git a/Lib/test/test_ast/test_ast.py b/Lib/test/test_ast/test_ast.py index 1e6f60074308e2..cedffbb0a2e346 100644 --- a/Lib/test/test_ast/test_ast.py +++ b/Lib/test/test_ast/test_ast.py @@ -1,7 +1,6 @@ import _ast_unparse import ast import builtins -import contextlib import copy import dis import enum @@ -14,7 +13,6 @@ import types import unittest import weakref -from io import StringIO from pathlib import Path from textwrap import dedent try: @@ -3417,11 +3415,9 @@ def set_source(self, content): Path(self.filename).write_text(self.text_normalize(content)) def invoke_ast(self, *flags): - stderr = StringIO() - stdout = StringIO() with ( - contextlib.redirect_stdout(stdout), - contextlib.redirect_stderr(stderr), + support.captured_stdout() as stdout, + support.captured_stderr() as stderr, ): ast.main(args=[*flags, self.filename]) self.assertEqual(stderr.getvalue(), '') @@ -3462,9 +3458,8 @@ def f(x: int) -> int: def test_help_message(self): for flag in ('-h', '--help', '--unknown'): with self.subTest(flag=flag): - output = StringIO() with self.assertRaises(SystemExit): - with contextlib.redirect_stderr(output): + with support.captured_stderr() as output: ast.main(args=flag) self.assertStartsWith(output.getvalue(), 'usage: ') diff --git a/Lib/test/test_pickle.py b/Lib/test/test_pickle.py index e2384b33345a45..b24d865b1b8b5c 100644 --- a/Lib/test/test_pickle.py +++ b/Lib/test/test_pickle.py @@ -2,7 +2,6 @@ NAME_MAPPING, REVERSE_NAME_MAPPING) import builtins import collections -import contextlib import io import pickle import struct @@ -728,8 +727,7 @@ def set_pickle_data(self, data): pickle.dump(data, f) def invoke_pickle(self, *flags): - output = io.StringIO() - with contextlib.redirect_stdout(output): + with support.captured_stdout() as output: pickle._main(args=[*flags, self.filename]) return self.text_normalize(output.getvalue()) @@ -754,10 +752,9 @@ def test_invocation(self): @support.force_not_colorized def test_unknown_flag(self): - stderr = io.StringIO() with self.assertRaises(SystemExit): # check that the parser help is shown - with contextlib.redirect_stderr(stderr): + with support.captured_stderr() as stderr: _ = self.invoke_pickle('--unknown') self.assertStartsWith(stderr.getvalue(), 'usage: ') diff --git a/Lib/test/test_tokenize.py b/Lib/test/test_tokenize.py index 865e0c5b40ddd3..92a0510ddf0b03 100644 --- a/Lib/test/test_tokenize.py +++ b/Lib/test/test_tokenize.py @@ -1,4 +1,3 @@ -import contextlib import itertools import os import re @@ -3181,8 +3180,7 @@ def set_source(self, content): fp.write(content) def invoke_tokenize(self, *flags): - output = StringIO() - with contextlib.redirect_stdout(output): + with support.captured_stdout() as output: tokenize._main(args=[*flags, self.filename]) return self.text_normalize(output.getvalue()) @@ -3209,7 +3207,7 @@ def f(): with self.assertRaises(SystemExit): # suppress argparse error message - with contextlib.redirect_stderr(StringIO()): + with support.captured_stderr(): _ = self.invoke_tokenize('--unknown') def test_without_flag(self):