Error handling in context managers

Gregory Ewing greg.ewing at canterbury.ac.nz
Tue Jan 17 00:01:03 EST 2017


Israel Brewster wrote:
> The problem is that, from time to time, I can't get a connection, the result
> being that cursor is None,

That's your problem right there -- you want a better-behaved
version of psql_cursor().

def get_psql_cursor():
    c = psql_cursor()
    if c is None:
       raise CantGetAConnectionError()
    return c

with get_psql_cursor() as c:
    ...

-- 
Greg



More information about the Python-list mailing list