FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-131178: Use `support.captured_stdout` and `support.captured_stderr` by donbarbos · Pull Request #137748 · python/cpython · GitHub

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

Filter by extension

Filter by extension .py  (3) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
11 changes: 3 additions & 8 deletions Lib/test/test_ast/test_ast.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL