3030# setting is True
3131import argparse
3232import cmd
33+ import contextlib
3334import copy
3435import functools
3536import glob
4748 namedtuple ,
4849)
4950from collections .abc import Callable , Iterable , Mapping
50- from contextlib import (
51- redirect_stdout ,
52- suppress ,
53- )
5451from types import (
5552 FrameType ,
5653 ModuleType ,
121118)
122119
123120# NOTE: When using gnureadline with Python 3.13, start_ipython needs to be imported before any readline-related stuff
124- try :
121+ with contextlib . suppress ( ImportError ) :
125122 from IPython import start_ipython # type: ignore[import]
126- except ImportError :
127- pass
128123
129124from .rl_utils import (
130125 RlType ,
@@ -284,7 +279,7 @@ def remove(self, command_method: CommandFunc) -> None:
284279class Cmd (cmd .Cmd ):
285280 """An easy but powerful framework for writing line-oriented command interpreters.
286281
287- Extends the Python Standard Library’ s cmd package by adding a lot of useful features
282+ Extends the Python Standard Library' s cmd package by adding a lot of useful features
288283 to the out of the box configuration.
289284
290285 Line-oriented command interpreters are often useful for test harnesses, internal tools, and rapid prototypes.
@@ -1088,9 +1083,8 @@ def add_settable(self, settable: Settable) -> None:
10881083
10891084 :param settable: Settable object being added
10901085 """
1091- if not self .always_prefix_settables :
1092- if settable .name in self .settables and settable .name not in self ._settables :
1093- raise KeyError (f'Duplicate settable: { settable .name } ' )
1086+ if not self .always_prefix_settables and settable .name in self .settables and settable .name not in self ._settables :
1087+ raise KeyError (f'Duplicate settable: { settable .name } ' )
10941088 self ._settables [settable .name ] = settable
10951089
10961090 def remove_settable (self , name : str ) -> None :
@@ -1296,7 +1290,7 @@ def ppaged(self, msg: Any, *, end: str = '\n', chop: bool = False) -> None:
12961290 # Don't try to use the pager when being run by a continuous integration system like Jenkins + pexpect.
12971291 functional_terminal = False
12981292
1299- if self .stdin .isatty () and self .stdout .isatty ():
1293+ if self .stdin .isatty () and self .stdout .isatty (): # noqa: SIM102
13001294 if sys .platform .startswith ('win' ) or os .environ .get ('TERM' ) is not None :
13011295 functional_terminal = True
13021296
@@ -2725,8 +2719,8 @@ def _redirect_output(self, statement: Statement) -> utils.RedirectionSavedState:
27252719 read_fd , write_fd = os .pipe ()
27262720
27272721 # Open each side of the pipe
2728- subproc_stdin = open (read_fd )
2729- new_stdout : TextIO = cast (TextIO , open (write_fd , 'w' ))
2722+ subproc_stdin = open (read_fd ) # noqa: SIM115
2723+ new_stdout : TextIO = cast (TextIO , open (write_fd , 'w' )) # noqa: SIM115
27302724
27312725 # Create pipe process in a separate group to isolate our signals from it. If a Ctrl-C event occurs,
27322726 # our sigint handler will forward it only to the most recent pipe process. This makes sure pipe
@@ -2756,7 +2750,7 @@ def _redirect_output(self, statement: Statement) -> utils.RedirectionSavedState:
27562750 # like: !ls -l | grep user | wc -l > out.txt. But this makes it difficult to know if the pipe process
27572751 # started OK, since the shell itself always starts. Therefore, we will wait a short time and check
27582752 # if the pipe process is still running.
2759- with suppress (subprocess .TimeoutExpired ):
2753+ with contextlib . suppress (subprocess .TimeoutExpired ):
27602754 proc .wait (0.2 )
27612755
27622756 # Check if the pipe process already exited
@@ -2776,7 +2770,7 @@ def _redirect_output(self, statement: Statement) -> utils.RedirectionSavedState:
27762770 mode = 'a' if statement .output == constants .REDIRECTION_APPEND else 'w'
27772771 try :
27782772 # Use line buffering
2779- new_stdout = cast (TextIO , open (utils .strip_quotes (statement .output_to ), mode = mode , buffering = 1 ))
2773+ new_stdout = cast (TextIO , open (utils .strip_quotes (statement .output_to ), mode = mode , buffering = 1 )) # noqa: SIM115
27802774 except OSError as ex :
27812775 raise RedirectionError (f'Failed to redirect because: { ex } ' )
27822776
@@ -2797,7 +2791,7 @@ def _redirect_output(self, statement: Statement) -> utils.RedirectionSavedState:
27972791 # no point opening up the temporary file
27982792 current_paste_buffer = get_paste_buffer ()
27992793 # create a temporary file to store output
2800- new_stdout = cast (TextIO , tempfile .TemporaryFile (mode = "w+" ))
2794+ new_stdout = cast (TextIO , tempfile .TemporaryFile (mode = "w+" )) # noqa: SIM115
28012795 redir_saved_state .redirecting = True
28022796 sys .stdout = self .stdout = new_stdout
28032797
@@ -2823,11 +2817,9 @@ def _restore_output(self, statement: Statement, saved_redir_state: utils.Redirec
28232817 self .stdout .seek (0 )
28242818 write_to_paste_buffer (self .stdout .read ())
28252819
2826- try :
2820+ with contextlib . suppress ( BrokenPipeError ) :
28272821 # Close the file or pipe that stdout was redirected to
28282822 self .stdout .close ()
2829- except BrokenPipeError :
2830- pass
28312823
28322824 # Restore the stdout values
28332825 self .stdout = cast (TextIO , saved_redir_state .saved_self_stdout )
@@ -3081,11 +3073,9 @@ def _read_command_line(self, prompt: str) -> str:
30813073 """
30823074 try :
30833075 # Wrap in try since terminal_lock may not be locked
3084- try :
3076+ with contextlib . suppress ( RuntimeError ) :
30853077 # Command line is about to be drawn. Allow asynchronous changes to the terminal.
30863078 self .terminal_lock .release ()
3087- except RuntimeError :
3088- pass
30893079 return self .read_input (prompt , completion_mode = utils .CompletionMode .COMMANDS )
30903080 except EOFError :
30913081 return 'eof'
@@ -3622,7 +3612,7 @@ def _print_topics(self, header: str, cmds: list[str], verbose: bool) -> None:
36223612 result = io .StringIO ()
36233613
36243614 # try to redirect system stdout
3625- with redirect_stdout (result ):
3615+ with contextlib . redirect_stdout (result ):
36263616 # save our internal stdout
36273617 stdout_orig = self .stdout
36283618 try :
@@ -3948,7 +3938,7 @@ def _reset_py_display() -> None:
39483938 # Delete any prompts that have been set
39493939 attributes = ['ps1' , 'ps2' , 'ps3' ]
39503940 for cur_attr in attributes :
3951- with suppress (KeyError ):
3941+ with contextlib . suppress (KeyError ):
39523942 del sys .__dict__ [cur_attr ]
39533943
39543944 # Reset functions
@@ -4126,7 +4116,7 @@ def py_quit() -> None:
41264116
41274117 # Check if we are running Python code
41284118 if py_code_to_run :
4129- try :
4119+ try : # noqa: SIM105
41304120 interp .runcode (py_code_to_run ) # type: ignore[arg-type]
41314121 except BaseException : # noqa: BLE001, S110
41324122 # We don't care about any exception that happened in the Python code
@@ -4377,7 +4367,7 @@ def do_history(self, args: argparse.Namespace) -> Optional[bool]:
43774367 self .last_result = False
43784368
43794369 # -v must be used alone with no other options
4380- if args .verbose :
4370+ if args .verbose : # noqa: SIM102
43814371 if args .clear or args .edit or args .output_file or args .run or args .transcript or args .expanded or args .script :
43824372 self .poutput ("-v cannot be used with any other options" )
43834373 return None
0 commit comments