diff --git a/psycopg/pqpath.c b/psycopg/pqpath.c index 0a0de560..e3c14901 100644 --- a/psycopg/pqpath.c +++ b/psycopg/pqpath.c @@ -167,8 +167,10 @@ pq_raise(connectionObject *conn, cursorObject *curs, PGresult **pgres) /* if the connection has somehow beed broken, we mark the connection object as closed but requiring cleanup */ - if (conn->pgconn != NULL && PQstatus(conn->pgconn) == CONNECTION_BAD) + if (conn->pgconn != NULL && PQstatus(conn->pgconn) == CONNECTION_BAD) { conn->closed = 2; + exc = OperationalError; + } if (pgres == NULL && curs != NULL) pgres = &curs->pgres; @@ -202,9 +204,9 @@ pq_raise(connectionObject *conn, cursorObject *curs, PGresult **pgres) if (code != NULL) { exc = exception_from_sqlstate(code); } - else { - /* Fallback if there is no exception code (reported happening e.g. - * when the connection is closed). */ + else if (exc == NULL) { + /* Fallback if there is no exception code (unless we already + determined that the connection was closed). */ exc = DatabaseError; } diff --git a/tests/test_cursor.py b/tests/test_cursor.py index 69430e08..60f74ba5 100755 --- a/tests/test_cursor.py +++ b/tests/test_cursor.py @@ -527,7 +527,7 @@ class CursorTests(ConnectingTestCase): cur.execute('select pg_terminate_backend(%s)', (pid1,)) time.sleep(0.001) - with self.assertRaises(psycopg2.DatabaseError): + with self.assertRaises(psycopg2.OperationalError): with victim_conn.cursor() as cur: cur.execute('select 1') wait_func(victim_conn)