Skip to content

gh-131178: Use support.captured_stdout and support.captured_stderr #137748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions Lib/test/test_ast/test_ast.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import _ast_unparse
import ast
import builtins
import contextlib
import copy
import dis
import enum
Expand All @@ -14,7 +13,6 @@
import types
import unittest
import weakref
from io import StringIO
from pathlib import Path
from textwrap import dedent
try:
Expand Down Expand Up @@ -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(), '')
Expand Down Expand Up @@ -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: ')

Expand Down
7 changes: 2 additions & 5 deletions Lib/test/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
NAME_MAPPING, REVERSE_NAME_MAPPING)
import builtins
import collections
import contextlib
import io
import pickle
import struct
Expand Down Expand Up @@ -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())

Expand All @@ -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: ')

Expand Down
6 changes: 2 additions & 4 deletions Lib/test/test_tokenize.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import contextlib
import itertools
import os
import re
Expand Down Expand Up @@ -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())

Expand All @@ -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):
Expand Down
Loading