diff options
author | Daniele Varrazzo <daniele.varrazzo@gmail.com> | 2015-12-16 12:00:52 +0000 |
---|---|---|
committer | Daniele Varrazzo <daniele.varrazzo@gmail.com> | 2015-12-16 12:03:10 +0000 |
commit | 5fd0f6c4eefecb0d6150179c32c43d16c11b173d (patch) | |
tree | 5f4bfd443908229bb5f9be67a9b9ac1db8f72ac7 /lib/errorcodes.py | |
parent | fe4cb0d49353f56328b9981a5140ecda65e972b4 (diff) | |
download | psycopg2-5fd0f6c4eefecb0d6150179c32c43d16c11b173d.tar.gz |
Fixed race condition on import in errorcodes.lookup
Fixes #382.
Diffstat (limited to 'lib/errorcodes.py')
-rw-r--r-- | lib/errorcodes.py | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/lib/errorcodes.py b/lib/errorcodes.py index 12c300f..aa5a723 100644 --- a/lib/errorcodes.py +++ b/lib/errorcodes.py @@ -38,11 +38,17 @@ def lookup(code, _cache={}): return _cache[code] # Generate the lookup map at first usage. + tmp = {} for k, v in globals().iteritems(): if isinstance(v, str) and len(v) in (2, 5): - _cache[v] = k + tmp[v] = k - return lookup(code) + assert tmp + + # Atomic update, to avoid race condition on import (bug #382) + _cache.update(tmp) + + return _cache[code] # autogenerated data: do not edit below this point. |