Sat, 05 Nov 2022 13:01:29 +0100
Replaced a custom function by use of standard functions from contextlib.
src/eric7/CodeFormatting/IsortFormattingDialog.py | file | annotate | diff | comparison | revisions | |
src/eric7/CodeFormatting/IsortUtilities.py | file | annotate | diff | comparison | revisions |
--- a/src/eric7/CodeFormatting/IsortFormattingDialog.py Sat Nov 05 13:00:17 2022 +0100 +++ b/src/eric7/CodeFormatting/IsortFormattingDialog.py Sat Nov 05 13:01:29 2022 +0100 @@ -7,9 +7,11 @@ Module implementing a dialog showing the isort code formatting progress and the results. """ +import contextlib import copy import io import multiprocessing +import os import pathlib from dataclasses import dataclass @@ -30,7 +32,6 @@ from .FormattingDiffWidget import FormattingDiffWidget from .IsortFormattingAction import IsortFormattingAction -from .IsortUtilities import suppressStderr from .Ui_IsortFormattingDialog import Ui_IsortFormattingDialog @@ -550,8 +551,13 @@ @rtype IsortResult """ diffIO = io.StringIO() if withDiff else False - with suppressStderr(): - ok = check_file(filename, show_diff=diffIO, config=isortConfig) + with open(os.devnull, "w") as devnull: + with contextlib.redirect_stderr(devnull): + ok = check_file( + filename, + show_diff=diffIO, + config=isortConfig, + ) if withDiff: data = "" if ok else diffIO.getvalue() diffIO.close() @@ -574,14 +580,15 @@ @return result object @rtype IsortResult """ - with suppressStderr(): - ok = sort_file( - filename, - config=isortConfig, - ask_to_apply=False, - write_to_stdout=False, - show_diff=False, - ) + with open(os.devnull, "w") as devnull: + with contextlib.redirect_stderr(devnull): + ok = sort_file( + filename, + config=isortConfig, + ask_to_apply=False, + write_to_stdout=False, + show_diff=False, + ) status = "changed" if ok else "unchanged"
--- a/src/eric7/CodeFormatting/IsortUtilities.py Sat Nov 05 13:00:17 2022 +0100 +++ b/src/eric7/CodeFormatting/IsortUtilities.py Sat Nov 05 13:01:29 2022 +0100 @@ -8,10 +8,6 @@ tool. """ -import contextlib -import os -import sys - import isort from PyQt6.QtCore import QCoreApplication, pyqtSlot @@ -35,15 +31,3 @@ """ type.</p>""", ).format(isort.__version__), ) - - -@contextlib.contextmanager -def suppressStderr(): - """ - Function to suppress output to stderr. - """ - stderr = sys.stderr - with open(os.devnull, "w") as devnull: - sys.stderr = devnull - yield - sys.stderr = stderr