112112Licensed under the MIT license.
113113"""
114114
115+ from contextlib import suppress
115116from queue import Empty , Full , Queue
116117
117118from . 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