Skip to content

Commit 993756c

Browse files
authored
Use contextlib.suppress instead of try-except-pass (#204)
1 parent d0b6a8c commit 993756c

File tree

2 files changed

+6
-15
lines changed

2 files changed

+6
-15
lines changed

bundled/tool/lsp_jsonrpc.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55

66
import atexit
7+
import contextlib
78
import io
89
import json
910
import pathlib
@@ -99,14 +100,10 @@ def __init__(self, reader: io.TextIOWrapper, writer: io.TextIOWrapper):
99100

100101
def close(self):
101102
"""Closes the underlying streams."""
102-
try:
103+
with contextlib.suppress(Exception):
103104
self._reader.close()
104-
except: # pylint: disable=bare-except
105-
pass
106-
try:
105+
with contextlib.suppress(Exception):
107106
self._writer.close()
108-
except: # pylint: disable=bare-except
109-
pass
110107

111108
def send_data(self, data):
112109
"""Send given data in JSON-RPC format."""
@@ -135,10 +132,8 @@ def __init__(self):
135132
def stop_all_processes(self):
136133
"""Send exit command to all processes and shutdown transport."""
137134
for i in self._rpc.values():
138-
try:
135+
with contextlib.suppress(Exception):
139136
i.send_data({"id": str(uuid.uuid4()), "method": "exit"})
140-
except: # pylint: disable=bare-except
141-
pass
142137
self._thread_pool.shutdown(wait=False)
143138

144139
def start_process(self, workspace: str, args: Sequence[str], cwd: str) -> None:

bundled/tool/lsp_utils.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def _run_module(
114114
str_output = CustomIO("<stdout>", encoding="utf-8")
115115
str_error = CustomIO("<stderr>", encoding="utf-8")
116116

117-
try:
117+
with contextlib.suppress(SystemExit):
118118
with substitute_attr(sys, "argv", argv):
119119
with redirect_io("stdout", str_output):
120120
with redirect_io("stderr", str_error):
@@ -126,8 +126,6 @@ def _run_module(
126126
runpy.run_module(module, run_name="__main__")
127127
else:
128128
runpy.run_module(module, run_name="__main__")
129-
except SystemExit:
130-
pass
131129

132130
return RunResult(str_output.get_value(), str_error.get_value())
133131

@@ -193,7 +191,7 @@ def _run_api(
193191
str_output = CustomIO("<stdout>", encoding="utf-8")
194192
str_error = CustomIO("<stderr>", encoding="utf-8")
195193

196-
try:
194+
with contextlib.suppress(SystemExit):
197195
with substitute_attr(sys, "argv", argv):
198196
with redirect_io("stdout", str_output):
199197
with redirect_io("stderr", str_error):
@@ -205,7 +203,5 @@ def _run_api(
205203
callback(argv, str_output, str_error, str_input)
206204
else:
207205
callback(argv, str_output, str_error)
208-
except SystemExit:
209-
pass
210206

211207
return RunResult(str_output.get_value(), str_error.get_value())

0 commit comments

Comments
 (0)