[Mpuls-commits] r5962 - base/trunk/mpulsweb/lib

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Wed May 23 15:17:30 CEST 2012


Author: bh
Date: 2012-05-23 15:17:30 +0200 (Wed, 23 May 2012)
New Revision: 5962

Modified:
   base/trunk/mpulsweb/lib/db.py
Log:
Work around a bug in psycopg related multiple apps in mod_wsgi.
The symptom is ProgrammingError exceptions raised by psycopg when trying
to write Decimal objects into the database. The issue is explained in
more detail in the comments in the code.

Fixes mpuls/issue3023


Modified: base/trunk/mpulsweb/lib/db.py
===================================================================
--- base/trunk/mpulsweb/lib/db.py	2012-05-22 11:46:43 UTC (rev 5961)
+++ base/trunk/mpulsweb/lib/db.py	2012-05-23 13:17:30 UTC (rev 5962)
@@ -30,6 +30,7 @@
 
 import logging
 from contextlib import contextmanager
+import decimal
 
 import psycopg2 as dbapi
 import psycopg2.extensions
@@ -47,6 +48,20 @@
 # implementations.
 psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
 
+# Work around a subtle defect in psycopg2. When running two Python
+# sub-interpreters in the same process the dictionary implementing the
+# adaptation protocol in psycopg is shared between the interpreters.
+# However, each interpreter has its own Decimal class which leads to
+# adaptation failures when trying to use Decimal values in SQL queries.
+# psycopg raises ProgrammingError with a message containing "can't
+# adapt". We work around this problem by always registering the Decimal
+# type of the current interpreter as described in the psycopg ticket for
+# this defect. See also mpuls/issue3023.
+# This defect has been fixed with psycopg 2.4.3, so we can remove this
+# work-around again when it's no longer needed.
+# psycopg ticket:
+# http://psycopg.lighthouseapp.com/projects/62710/tickets/52-cant-adapt-type-decimal
+psycopg2.extensions.register_adapter(decimal.Decimal, psycopg2._psycopg.Decimal)
 
 db = StackedObjectProxy(name="mpuls.db")
 



More information about the Mpuls-commits mailing list