Decimal adapter registration moved from C to Python

Fixes Decimal adaptation in sub-interpreter, where the Decimal class has
a different identity from the one in the main interpreter.

Closes ticket #52.
This commit is contained in:
Daniele Varrazzo 2011-10-14 22:13:10 +01:00
parent 6da39e3a37
commit dde4c0de3d
3 changed files with 14 additions and 4 deletions

2
NEWS
View File

@ -6,6 +6,8 @@ What's new in psycopg 2.4.3
- Added support for arrays of hstores and composite types (ticket #66).
- Fixed segfault in case of transaction started with connection lost
(and possibly other events).
- Fixed adaptation of Decimal type in sub-interpreters, such as in
certain mod_wsgi configurations (ticket #52).
- Rollback connections in transaction or in error before putting them
back into a pool. Also discard broken connections (ticket #62).
- Lazy import of the slow uuid module, thanks to Marko Kreen.

View File

@ -85,5 +85,17 @@ import psycopg2.extensions as _ext
_ext.register_adapter(tuple, _ext.SQL_IN)
_ext.register_adapter(type(None), _ext.NoneAdapter)
# Register the Decimal adapter here instead of in the C layer.
# This way a new class is registered for each sub-interpreter.
# See ticket #52
try:
from decimal import Decimal
except ImportError:
pass
else:
from psycopg2._psycopg import Decimal as Adapter
_ext.register_adapter(Decimal, Adapter)
del Decimal, Adapter
__all__ = filter(lambda k: not k.startswith('_'), locals().keys())

View File

@ -322,7 +322,6 @@ static void
psyco_adapters_init(PyObject *mod)
{
PyObject *call;
PyTypeObject *type;
microprotocols_add(&PyFloat_Type, NULL, (PyObject*)&pfloatType);
#if PY_MAJOR_VERSION < 3
@ -353,9 +352,6 @@ psyco_adapters_init(PyObject *mod)
microprotocols_add(&PyList_Type, NULL, (PyObject*)&listType);
if ((type = (PyTypeObject*)psyco_GetDecimalType()) != NULL)
microprotocols_add(type, NULL, (PyObject*)&pdecimalType);
/* the module has already been initialized, so we can obtain the callable
objects directly from its dictionary :) */
call = PyMapping_GetItemString(mod, "DateFromPy");