Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warnings are never raised #304

Closed
kwauchope opened this issue Mar 20, 2015 · 2 comments
Closed

Warnings are never raised #304

kwauchope opened this issue Mar 20, 2015 · 2 comments

Comments

@kwauchope
Copy link

Overview:

psycopg2.Warning exceptions are never raised as stated in documentation though notices are correctly set. After a quick search I couldn't find where Warnings were raised in psycopg history. One could argue warnings such as privilege_not_revoked are 'important'.

Reason:

PGRES_NONFATAL_ERROR is not handled in pqpath.pq_fetch() and there is no 'warning' case in pqpath.exception_from_sqlstate() as PGRES_NONFATAL_ERROR is never returned by PQresultStatus. This is as libpq was designed and intended, non fatal errors are sent to the notice processor instead, however Warning Exceptions are not raised in connection_int where the notice processing occurs.

Possible resolutions:

  • Meh who cares.
  • Raise Warning in connection_int.conn_notice_callback() or connection_int.conn_notice_process() (could break current implementations that don't catch warnings). After looking at postgresql warning messages one could argue privilege_not_revoked (01006) for example is at least worthy of a Warning exception.
  • Add a code example somewhere similar to that below for users that want to pull out WARNING notices after each execute statement or block of statements if desired. This deletes notices after so there is no confusion which statement or block of statements caused the warning (there are no timestamps or counters associated with the notices).
def handleWarnings(self):
    for warning in filter(lambda x: x.startswith('WARNING'), self.conn.notices):
        #do something appropriate
    del self.conn.notices[:]

or

def handleWarnings(self):
    warnings = filter(lambda x: x.startswith('WARNING'), self.conn.notices)
    if len(warnings):
        #do something appropriate
    del self.conn.notices[:]
@dvarrazzo
Copy link
Member

Warnings are only passed as notices: I don't think it's a good idea building any policy on top of them at driver level. For a starter, "WARNING" is probably localized so we cannot really trust that string. So if anything I'd improve the docs. Feel free to provide a documentation patch, thank you.

@dvarrazzo
Copy link
Member

Closing the issue. Meaningful doc patches are well accepted but we won't convert postgres warnings into python warnings. The application can easily do that where needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants