mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-11 18:32:12 +02:00
Use "with suppress()" to suppress python exceptions
Using
```Python
with suppress(OSError):
os.remove('somefile.tmp')
```
instead of
```Python
try:
os.remove('somefile.tmp')
except OSError:
pass
```
makes the code more compact and more readable IMO.
This pattern was recommended by Raymond Hettinger, a Python Core
Developer in his talk "Transforming Code into Beautiful, Idiomatic Python" at https://www.youtube.com/watch?v=OSGv2VnC0go. The transcript is available at https://github.com/JeffPaine/beautiful_idiomatic_python
This commit is contained in:
@@ -8,6 +8,7 @@ import re
|
||||
import sys
|
||||
import types
|
||||
from functools import partial
|
||||
from contextlib import suppress
|
||||
|
||||
from .cli import emph, parse_args
|
||||
from .cmds import cmap, parse_subcommand_cli
|
||||
@@ -67,10 +68,8 @@ class SocketIO:
|
||||
|
||||
def __exit__(self, *a):
|
||||
import socket
|
||||
try:
|
||||
with suppress(EnvironmentError): # on some OSes such as macOS the socket is already closed at this point
|
||||
self.socket.shutdown(socket.SHUT_RDWR)
|
||||
except EnvironmentError:
|
||||
pass # on some OSes such as macOS the socket is already closed at this point
|
||||
self.socket.close()
|
||||
|
||||
def send(self, data):
|
||||
|
||||
Reference in New Issue
Block a user