in ticket <https://github.com/psycopg/psycopg/issues/1188> an user showed that, upon a failed connection attempt because of a bad password, the server sends a SQLSTATE 28P01 message. However psycopg cannot raise the expected `InvalidPassword` exception, because the only way to get the SQLSTATE seems to be from a PGresult via PQresultErrorField and I fail to see any API call to get the info from a PGconn.
Have you seen the *pgpassfileWarning* function (src/interfaces/libpq/fe-connect.c)? Does this sound like what you're looking for?