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
48
49
namedtuple ,
49
50
)
50
51
from collections .abc import Callable , Iterable , Mapping
51
- from contextlib import (
52
- redirect_stdout ,
53
- suppress ,
54
- )
55
52
from types import (
56
53
FrameType ,
57
54
ModuleType ,
123
120
)
124
121
125
122
# 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 ) :
127
124
from IPython import start_ipython # type: ignore[import]
128
- except ImportError :
129
- pass
130
125
131
126
from .rl_utils import (
132
127
RlType ,
@@ -1098,9 +1093,8 @@ def add_settable(self, settable: Settable) -> None:
1098
1093
1099
1094
:param settable: Settable object being added
1100
1095
"""
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 } ' )
1104
1098
self ._settables [settable .name ] = settable
1105
1099
1106
1100
def remove_settable (self , name : str ) -> None :
@@ -1306,7 +1300,7 @@ def ppaged(self, msg: Any, *, end: str = '\n', chop: bool = False) -> None:
1306
1300
# Don't try to use the pager when being run by a continuous integration system like Jenkins + pexpect.
1307
1301
functional_terminal = False
1308
1302
1309
- if self .stdin .isatty () and self .stdout .isatty ():
1303
+ if self .stdin .isatty () and self .stdout .isatty (): # noqa: SIM102
1310
1304
if sys .platform .startswith ('win' ) or os .environ .get ('TERM' ) is not None :
1311
1305
functional_terminal = True
1312
1306
@@ -2844,8 +2838,8 @@ def _redirect_output(self, statement: Statement) -> utils.RedirectionSavedState:
2844
2838
read_fd , write_fd = os .pipe ()
2845
2839
2846
2840
# 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
2849
2843
2850
2844
# Create pipe process in a separate group to isolate our signals from it. If a Ctrl-C event occurs,
2851
2845
# 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:
2875
2869
# like: !ls -l | grep user | wc -l > out.txt. But this makes it difficult to know if the pipe process
2876
2870
# started OK, since the shell itself always starts. Therefore, we will wait a short time and check
2877
2871
# if the pipe process is still running.
2878
- with suppress (subprocess .TimeoutExpired ):
2872
+ with contextlib . suppress (subprocess .TimeoutExpired ):
2879
2873
proc .wait (0.2 )
2880
2874
2881
2875
# Check if the pipe process already exited
@@ -2894,7 +2888,7 @@ def _redirect_output(self, statement: Statement) -> utils.RedirectionSavedState:
2894
2888
mode = 'a' if statement .output == constants .REDIRECTION_APPEND else 'w'
2895
2889
try :
2896
2890
# 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
2898
2892
except OSError as ex :
2899
2893
raise RedirectionError (f'Failed to redirect because: { ex } ' )
2900
2894
@@ -2915,7 +2909,7 @@ def _redirect_output(self, statement: Statement) -> utils.RedirectionSavedState:
2915
2909
# no point opening up the temporary file
2916
2910
current_paste_buffer = get_paste_buffer ()
2917
2911
# 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
2919
2913
redir_saved_state .redirecting = True
2920
2914
sys .stdout = self .stdout = new_stdout
2921
2915
@@ -2941,11 +2935,9 @@ def _restore_output(self, statement: Statement, saved_redir_state: utils.Redirec
2941
2935
self .stdout .seek (0 )
2942
2936
write_to_paste_buffer (self .stdout .read ())
2943
2937
2944
- try :
2938
+ with contextlib . suppress ( BrokenPipeError ) :
2945
2939
# Close the file or pipe that stdout was redirected to
2946
2940
self .stdout .close ()
2947
- except BrokenPipeError :
2948
- pass
2949
2941
2950
2942
# Restore the stdout values
2951
2943
self .stdout = cast (TextIO , saved_redir_state .saved_self_stdout )
@@ -3199,11 +3191,9 @@ def _read_command_line(self, prompt: str) -> str:
3199
3191
"""
3200
3192
try :
3201
3193
# Wrap in try since terminal_lock may not be locked
3202
- try :
3194
+ with contextlib . suppress ( RuntimeError ) :
3203
3195
# Command line is about to be drawn. Allow asynchronous changes to the terminal.
3204
3196
self .terminal_lock .release ()
3205
- except RuntimeError :
3206
- pass
3207
3197
return self .read_input (prompt , completion_mode = utils .CompletionMode .COMMANDS )
3208
3198
except EOFError :
3209
3199
return 'eof'
@@ -3948,7 +3938,7 @@ def _print_topics(self, header: str, cmds: list[str], verbose: bool) -> None:
3948
3938
result = io .StringIO ()
3949
3939
3950
3940
# try to redirect system stdout
3951
- with redirect_stdout (result ):
3941
+ with contextlib . redirect_stdout (result ):
3952
3942
# save our internal stdout
3953
3943
stdout_orig = self .stdout
3954
3944
try :
@@ -4245,7 +4235,7 @@ def _reset_py_display() -> None:
4245
4235
# Delete any prompts that have been set
4246
4236
attributes = ['ps1' , 'ps2' , 'ps3' ]
4247
4237
for cur_attr in attributes :
4248
- with suppress (KeyError ):
4238
+ with contextlib . suppress (KeyError ):
4249
4239
del sys .__dict__ [cur_attr ]
4250
4240
4251
4241
# Reset functions
@@ -4423,7 +4413,7 @@ def py_quit() -> None:
4423
4413
4424
4414
# Check if we are running Python code
4425
4415
if py_code_to_run :
4426
- try :
4416
+ try : # noqa: SIM105
4427
4417
interp .runcode (py_code_to_run ) # type: ignore[arg-type]
4428
4418
except BaseException : # noqa: BLE001, S110
4429
4419
# 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]:
4646
4636
self .last_result = False
4647
4637
4648
4638
# -v must be used alone with no other options
4649
- if args .verbose :
4639
+ if args .verbose : # noqa: SIM102
4650
4640
if args .clear or args .edit or args .output_file or args .run or args .transcript or args .expanded or args .script :
4651
4641
self .poutput ("-v cannot be used with any other options" )
4652
4642
self .poutput (self .history_parser .format_usage ())
0 commit comments