30
30
# setting is True
31
31
import argparse
32
32
import cmd
33
+ import contextlib
33
34
import copy
34
35
import functools
35
36
import glob
47
48
namedtuple ,
48
49
)
49
50
from collections .abc import Callable , Iterable , Mapping
50
- from contextlib import (
51
- redirect_stdout ,
52
- suppress ,
53
- )
54
51
from types import (
55
52
FrameType ,
56
53
ModuleType ,
121
118
)
122
119
123
120
# 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 ) :
125
122
from IPython import start_ipython # type: ignore[import]
126
- except ImportError :
127
- pass
128
123
129
124
from .rl_utils import (
130
125
RlType ,
@@ -284,7 +279,7 @@ def remove(self, command_method: CommandFunc) -> None:
284
279
class Cmd (cmd .Cmd ):
285
280
"""An easy but powerful framework for writing line-oriented command interpreters.
286
281
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
288
283
to the out of the box configuration.
289
284
290
285
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:
1088
1083
1089
1084
:param settable: Settable object being added
1090
1085
"""
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 } ' )
1094
1088
self ._settables [settable .name ] = settable
1095
1089
1096
1090
def remove_settable (self , name : str ) -> None :
@@ -1296,7 +1290,7 @@ def ppaged(self, msg: Any, *, end: str = '\n', chop: bool = False) -> None:
1296
1290
# Don't try to use the pager when being run by a continuous integration system like Jenkins + pexpect.
1297
1291
functional_terminal = False
1298
1292
1299
- if self .stdin .isatty () and self .stdout .isatty ():
1293
+ if self .stdin .isatty () and self .stdout .isatty (): # noqa: SIM102
1300
1294
if sys .platform .startswith ('win' ) or os .environ .get ('TERM' ) is not None :
1301
1295
functional_terminal = True
1302
1296
@@ -2725,8 +2719,8 @@ def _redirect_output(self, statement: Statement) -> utils.RedirectionSavedState:
2725
2719
read_fd , write_fd = os .pipe ()
2726
2720
2727
2721
# 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
2730
2724
2731
2725
# Create pipe process in a separate group to isolate our signals from it. If a Ctrl-C event occurs,
2732
2726
# 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:
2756
2750
# like: !ls -l | grep user | wc -l > out.txt. But this makes it difficult to know if the pipe process
2757
2751
# started OK, since the shell itself always starts. Therefore, we will wait a short time and check
2758
2752
# if the pipe process is still running.
2759
- with suppress (subprocess .TimeoutExpired ):
2753
+ with contextlib . suppress (subprocess .TimeoutExpired ):
2760
2754
proc .wait (0.2 )
2761
2755
2762
2756
# Check if the pipe process already exited
@@ -2776,7 +2770,7 @@ def _redirect_output(self, statement: Statement) -> utils.RedirectionSavedState:
2776
2770
mode = 'a' if statement .output == constants .REDIRECTION_APPEND else 'w'
2777
2771
try :
2778
2772
# 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
2780
2774
except OSError as ex :
2781
2775
raise RedirectionError (f'Failed to redirect because: { ex } ' )
2782
2776
@@ -2797,7 +2791,7 @@ def _redirect_output(self, statement: Statement) -> utils.RedirectionSavedState:
2797
2791
# no point opening up the temporary file
2798
2792
current_paste_buffer = get_paste_buffer ()
2799
2793
# 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
2801
2795
redir_saved_state .redirecting = True
2802
2796
sys .stdout = self .stdout = new_stdout
2803
2797
@@ -2823,11 +2817,9 @@ def _restore_output(self, statement: Statement, saved_redir_state: utils.Redirec
2823
2817
self .stdout .seek (0 )
2824
2818
write_to_paste_buffer (self .stdout .read ())
2825
2819
2826
- try :
2820
+ with contextlib . suppress ( BrokenPipeError ) :
2827
2821
# Close the file or pipe that stdout was redirected to
2828
2822
self .stdout .close ()
2829
- except BrokenPipeError :
2830
- pass
2831
2823
2832
2824
# Restore the stdout values
2833
2825
self .stdout = cast (TextIO , saved_redir_state .saved_self_stdout )
@@ -3081,11 +3073,9 @@ def _read_command_line(self, prompt: str) -> str:
3081
3073
"""
3082
3074
try :
3083
3075
# Wrap in try since terminal_lock may not be locked
3084
- try :
3076
+ with contextlib . suppress ( RuntimeError ) :
3085
3077
# Command line is about to be drawn. Allow asynchronous changes to the terminal.
3086
3078
self .terminal_lock .release ()
3087
- except RuntimeError :
3088
- pass
3089
3079
return self .read_input (prompt , completion_mode = utils .CompletionMode .COMMANDS )
3090
3080
except EOFError :
3091
3081
return 'eof'
@@ -3622,7 +3612,7 @@ def _print_topics(self, header: str, cmds: list[str], verbose: bool) -> None:
3622
3612
result = io .StringIO ()
3623
3613
3624
3614
# try to redirect system stdout
3625
- with redirect_stdout (result ):
3615
+ with contextlib . redirect_stdout (result ):
3626
3616
# save our internal stdout
3627
3617
stdout_orig = self .stdout
3628
3618
try :
@@ -3948,7 +3938,7 @@ def _reset_py_display() -> None:
3948
3938
# Delete any prompts that have been set
3949
3939
attributes = ['ps1' , 'ps2' , 'ps3' ]
3950
3940
for cur_attr in attributes :
3951
- with suppress (KeyError ):
3941
+ with contextlib . suppress (KeyError ):
3952
3942
del sys .__dict__ [cur_attr ]
3953
3943
3954
3944
# Reset functions
@@ -4126,7 +4116,7 @@ def py_quit() -> None:
4126
4116
4127
4117
# Check if we are running Python code
4128
4118
if py_code_to_run :
4129
- try :
4119
+ try : # noqa: SIM105
4130
4120
interp .runcode (py_code_to_run ) # type: ignore[arg-type]
4131
4121
except BaseException : # noqa: BLE001, S110
4132
4122
# 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]:
4377
4367
self .last_result = False
4378
4368
4379
4369
# -v must be used alone with no other options
4380
- if args .verbose :
4370
+ if args .verbose : # noqa: SIM102
4381
4371
if args .clear or args .edit or args .output_file or args .run or args .transcript or args .expanded or args .script :
4382
4372
self .poutput ("-v cannot be used with any other options" )
4383
4373
return None
0 commit comments