diff --git a/ChangeLog b/ChangeLog index 333bc21a..4a196ac9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2010-02-15 Daniele Varrazzo + + * lib/errorcodes.py: Updated to PostgreSQL 8.4; added lookup() function. + 2010-02-12 Daniele Varrazzo * Stop the loop variable used to create __all__ leaking in the module. diff --git a/lib/errorcodes.py b/lib/errorcodes.py index 30110fd4..3720697a 100644 --- a/lib/errorcodes.py +++ b/lib/errorcodes.py @@ -29,6 +29,22 @@ This module contains symbolic names for all PostgreSQL error codes. # http://www.postgresql.org/docs/8.4/static/errcodes-appendix.html # +def lookup(code, _cache={}): + """Lookup a code error and return its symbolic name. + + Raise KeyError if the code is not found. + """ + if _cache: + return _cache[code] + + # Generate the lookup map at first usage. + for k, v in globals().iteritems(): + if isinstance(v, str) and len(v) in (2, 5): + _cache[v] = k + + return lookup(code) + + # autogenerated data: do not edit below this point. # Error classes