3030# setting is True
3131import argparse
3232import cmd
33+ import contextlib
3334import copy
3435import functools
3536import glob
4849 namedtuple ,
4950)
5051from collections .abc import Callable , Iterable , Mapping
51- from contextlib import (
52- redirect_stdout ,
53- suppress ,
54- )
5552from types import (
5653 FrameType ,
5754 ModuleType ,
123120)
124121
125122# NOTE: When using gnureadline with Python 3.13, start_ipython needs to be imported before any readline-related stuff
126- try :
123+ with contextlib . suppress ( ImportError ) :
127124 from IPython import start_ipython # type: ignore[import]
128- except ImportError :
129- pass
130125
131126from .rl_utils import (
132127 RlType ,
@@ -1098,9 +1093,8 @@ def add_settable(self, settable: Settable) -> None:
10981093
10991094 :param settable: Settable object being added
11001095 """
1101- if not self .always_prefix_settables :
1102- if settable .name in self .settables and settable .name not in self ._settables :
1103- raise KeyError (f'Duplicate settable: { settable .name } ' )
1096+ if not self .always_prefix_settables and settable .name in self .settables and settable .name not in self ._settables :
1097+ raise KeyError (f'Duplicate settable: { settable .name } ' )
11041098 self ._settables [settable .name ] = settable
11051099
11061100 def remove_settable (self , name : str ) -> None :
@@ -1306,7 +1300,7 @@ def ppaged(self, msg: Any, *, end: str = '\n', chop: bool = False) -> None:
13061300 # Don't try to use the pager when being run by a continuous integration system like Jenkins + pexpect.
13071301 functional_terminal = False
13081302
1309- if self .stdin .isatty () and self .stdout .isatty ():
1303+ if self .stdin .isatty () and self .stdout .isatty (): # noqa: SIM102
13101304 if sys .platform .startswith ('win' ) or os .environ .get ('TERM' ) is not None :
13111305 functional_terminal = True
13121306
@@ -2844,8 +2838,8 @@ def _redirect_output(self, statement: Statement) -> utils.RedirectionSavedState:
28442838 read_fd , write_fd = os .pipe ()
28452839
28462840 # Open each side of the pipe
2847- subproc_stdin = open (read_fd )
2848- new_stdout : TextIO = cast (TextIO , open (write_fd , 'w' ))
2841+ subproc_stdin = open (read_fd ) # noqa: SIM115
2842+ new_stdout : TextIO = cast (TextIO , open (write_fd , 'w' )) # noqa: SIM115
28492843
28502844 # Create pipe process in a separate group to isolate our signals from it. If a Ctrl-C event occurs,
28512845 # our sigint handler will forward it only to the most recent pipe process. This makes sure pipe
@@ -2875,7 +2869,7 @@ def _redirect_output(self, statement: Statement) -> utils.RedirectionSavedState:
28752869 # like: !ls -l | grep user | wc -l > out.txt. But this makes it difficult to know if the pipe process
28762870 # started OK, since the shell itself always starts. Therefore, we will wait a short time and check
28772871 # if the pipe process is still running.
2878- with suppress (subprocess .TimeoutExpired ):
2872+ with contextlib . suppress (subprocess .TimeoutExpired ):
28792873 proc .wait (0.2 )
28802874
28812875 # Check if the pipe process already exited
@@ -2894,7 +2888,7 @@ def _redirect_output(self, statement: Statement) -> utils.RedirectionSavedState:
28942888 mode = 'a' if statement .output == constants .REDIRECTION_APPEND else 'w'
28952889 try :
28962890 # Use line buffering
2897- new_stdout = cast (TextIO , open (utils .strip_quotes (statement .output_to ), mode = mode , buffering = 1 ))
2891+ new_stdout = cast (TextIO , open (utils .strip_quotes (statement .output_to ), mode = mode , buffering = 1 )) # noqa: SIM115
28982892 except OSError as ex :
28992893 raise RedirectionError (f'Failed to redirect because: { ex } ' )
29002894
@@ -2915,7 +2909,7 @@ def _redirect_output(self, statement: Statement) -> utils.RedirectionSavedState:
29152909 # no point opening up the temporary file
29162910 current_paste_buffer = get_paste_buffer ()
29172911 # create a temporary file to store output
2918- new_stdout = cast (TextIO , tempfile .TemporaryFile (mode = "w+" ))
2912+ new_stdout = cast (TextIO , tempfile .TemporaryFile (mode = "w+" )) # noqa: SIM115
29192913 redir_saved_state .redirecting = True
29202914 sys .stdout = self .stdout = new_stdout
29212915
@@ -2941,11 +2935,9 @@ def _restore_output(self, statement: Statement, saved_redir_state: utils.Redirec
29412935 self .stdout .seek (0 )
29422936 write_to_paste_buffer (self .stdout .read ())
29432937
2944- try :
2938+ with contextlib . suppress ( BrokenPipeError ) :
29452939 # Close the file or pipe that stdout was redirected to
29462940 self .stdout .close ()
2947- except BrokenPipeError :
2948- pass
29492941
29502942 # Restore the stdout values
29512943 self .stdout = cast (TextIO , saved_redir_state .saved_self_stdout )
@@ -3199,11 +3191,9 @@ def _read_command_line(self, prompt: str) -> str:
31993191 """
32003192 try :
32013193 # Wrap in try since terminal_lock may not be locked
3202- try :
3194+ with contextlib . suppress ( RuntimeError ) :
32033195 # Command line is about to be drawn. Allow asynchronous changes to the terminal.
32043196 self .terminal_lock .release ()
3205- except RuntimeError :
3206- pass
32073197 return self .read_input (prompt , completion_mode = utils .CompletionMode .COMMANDS )
32083198 except EOFError :
32093199 return 'eof'
@@ -3948,7 +3938,7 @@ def _print_topics(self, header: str, cmds: list[str], verbose: bool) -> None:
39483938 result = io .StringIO ()
39493939
39503940 # try to redirect system stdout
3951- with redirect_stdout (result ):
3941+ with contextlib . redirect_stdout (result ):
39523942 # save our internal stdout
39533943 stdout_orig = self .stdout
39543944 try :
@@ -4245,7 +4235,7 @@ def _reset_py_display() -> None:
42454235 # Delete any prompts that have been set
42464236 attributes = ['ps1' , 'ps2' , 'ps3' ]
42474237 for cur_attr in attributes :
4248- with suppress (KeyError ):
4238+ with contextlib . suppress (KeyError ):
42494239 del sys .__dict__ [cur_attr ]
42504240
42514241 # Reset functions
@@ -4423,7 +4413,7 @@ def py_quit() -> None:
44234413
44244414 # Check if we are running Python code
44254415 if py_code_to_run :
4426- try :
4416+ try : # noqa: SIM105
44274417 interp .runcode (py_code_to_run ) # type: ignore[arg-type]
44284418 except BaseException : # noqa: BLE001, S110
44294419 # We don't care about any exception that happened in the Python code
@@ -4646,7 +4636,7 @@ def do_history(self, args: argparse.Namespace) -> Optional[bool]:
46464636 self .last_result = False
46474637
46484638 # -v must be used alone with no other options
4649- if args .verbose :
4639+ if args .verbose : # noqa: SIM102
46504640 if args .clear or args .edit or args .output_file or args .run or args .transcript or args .expanded or args .script :
46514641 self .poutput ("-v cannot be used with any other options" )
46524642 self .poutput (self .history_parser .format_usage ())
0 commit comments