Skip to content

Commit 13fffe3

Browse files
committed
Simplify and add checks for simple code
1 parent 236b581 commit 13fffe3

File tree

7 files changed

+90
-122
lines changed

7 files changed

+90
-122
lines changed

dbutils/pooled_db.py

+16-17
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@
146146
Licensed under the MIT license.
147147
"""
148148

149+
from contextlib import suppress
149150
from functools import total_ordering
150151
from threading import Condition
151152

@@ -349,11 +350,10 @@ def unshare(self, con):
349350
with self._lock:
350351
con.unshare()
351352
shared = con.shared
352-
if not shared: # connection is idle,
353-
try: # so try to remove it
354-
self._shared_cache.remove(con) # from shared cache
355-
except ValueError:
356-
pass # pool has already been closed
353+
if not shared: # connection is idle
354+
# try to remove it from shared cache
355+
with suppress(ValueError): # if pool has already been closed
356+
self._shared_cache.remove(con)
357357
if not shared: # connection has become idle,
358358
self.cache(con.con) # so add it to the idle cache
359359

@@ -374,25 +374,22 @@ def close(self):
374374
with self._lock:
375375
while self._idle_cache: # close all idle connections
376376
con = self._idle_cache.pop(0)
377-
try:
377+
with suppress(Exception):
378378
con.close()
379-
except Exception:
380-
pass
381379
if self._maxshared: # close all shared connections
382380
while self._shared_cache:
383381
con = self._shared_cache.pop(0).con
384-
try:
382+
with suppress(Exception):
385383
con.close()
386-
except Exception:
387-
pass
388384
self._connections -= 1
389385
self._lock.notify_all()
390386

391387
def __del__(self):
392388
"""Delete the pool."""
393-
try:
389+
# builtins (including Exceptions) might not exist anymore
390+
try: # noqa: SIM105
394391
self.close()
395-
except: # noqa: E722 - builtin Exceptions might not exist anymore
392+
except: # noqa: E722, S110
396393
pass
397394

398395
def _wait_lock(self):
@@ -437,9 +434,10 @@ def __getattr__(self, name):
437434

438435
def __del__(self):
439436
"""Delete the pooled connection."""
440-
try:
437+
# builtins (including Exceptions) might not exist anymore
438+
try: # noqa: SIM105
441439
self.close()
442-
except: # noqa: E722 - builtin Exceptions might not exist anymore
440+
except: # noqa: E722, S110
443441
pass
444442

445443
def __enter__(self):
@@ -518,9 +516,10 @@ def __getattr__(self, name):
518516

519517
def __del__(self):
520518
"""Delete the pooled connection."""
521-
try:
519+
# builtins (including Exceptions) might not exist anymore
520+
try: # noqa: SIM105
522521
self.close()
523-
except: # noqa: E722 - builtin Exceptions might not exist anymore
522+
except: # noqa: E722, S110
524523
pass
525524

526525
def __enter__(self):

dbutils/pooled_pg.py

+13-16
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
Licensed under the MIT license.
113113
"""
114114

115+
from contextlib import suppress
115116
from queue import Empty, Full, Queue
116117

117118
from . import __version__
@@ -186,9 +187,8 @@ def __init__(
186187
maxcached = 0
187188
if maxconnections is None:
188189
maxconnections = 0
189-
if maxcached:
190-
if maxcached < mincached:
191-
maxcached = mincached
190+
if maxcached and maxcached < mincached:
191+
maxcached = mincached
192192
if maxconnections:
193193
if maxconnections < maxcached:
194194
maxconnections = maxcached
@@ -211,9 +211,8 @@ def steady_connection(self):
211211

212212
def connection(self):
213213
"""Get a steady, cached PostgreSQL connection from the pool."""
214-
if self._connections:
215-
if not self._connections.acquire(self._blocking):
216-
raise TooManyConnectionsError
214+
if self._connections and not self._connections.acquire(self._blocking):
215+
raise TooManyConnectionsError
217216
try:
218217
con = self._cache.get_nowait()
219218
except Empty:
@@ -226,10 +225,8 @@ def cache(self, con):
226225
if self._reset == RESET_COMPLETELY:
227226
con.reset() # reset the connection completely
228227
elif self._reset == RESET_ALWAYS_ROLLBACK or con._transaction:
229-
try:
228+
with suppress(Exception):
230229
con.rollback() # rollback a possible transaction
231-
except Exception:
232-
pass
233230
self._cache.put_nowait(con) # and then put it back into the cache
234231
except Full:
235232
con.close()
@@ -241,20 +238,19 @@ def close(self):
241238
while 1:
242239
try:
243240
con = self._cache.get_nowait()
244-
try:
241+
with suppress(Exception):
245242
con.close()
246-
except Exception:
247-
pass
248243
if self._connections:
249244
self._connections.release()
250245
except Empty:
251246
break
252247

253248
def __del__(self):
254249
"""Delete the pool."""
255-
try:
250+
# builtins (including Exceptions) might not exist anymore
251+
try: # noqa: SIM105
256252
self.close()
257-
except: # noqa: E722 - builtin Exceptions might not exist anymore
253+
except: # noqa: E722, S110
258254
pass
259255

260256

@@ -298,9 +294,10 @@ def __getattr__(self, name):
298294

299295
def __del__(self):
300296
"""Delete the pooled connection."""
301-
try:
297+
# builtins (including Exceptions) might not exist anymore
298+
try: # noqa: SIM105
302299
self.close()
303-
except: # noqa: E722 - builtin Exceptions might not exist anymore
300+
except: # noqa: E722, S110
304301
pass
305302

306303
def __enter__(self):

0 commit comments

Comments
 (0)