diff --git a/ChangeLog b/ChangeLog index 8ef45992..b3fdfcd4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2010-02-28 Federico Di Gregorio + + * psycopg/adapt_decimal.c: Python 2.4 decimal type does not support + .isfinite() and two different calls to ._isinfinity() and ._isnan() are + required. This fixes both a test failure and a segfault. + + 2010-02-15 Federico Di Gregorio * Added new Decimal adapter that correctly converts NaN and infinity diff --git a/psycopg/adapter_pdecimal.c b/psycopg/adapter_pdecimal.c index 11d25950..6bcccb58 100644 --- a/psycopg/adapter_pdecimal.c +++ b/psycopg/adapter_pdecimal.c @@ -42,14 +42,29 @@ static PyObject * pdecimal_str(pdecimalObject *self) { - PyObject *res = NULL; - PyObject *check = PyObject_CallMethod(self->wrapped, "is_finite", NULL); - + PyObject *check, *res = NULL; +#if PY_VERSION_HEX < 0x02050000 + check = PyObject_CallMethod(self->wrapped, "_isnan", NULL); + if (PyInt_AsLong(check) == 1) { + res = PyString_FromString("'NaN'::numeric"); + goto end; + } + Py_DECREF(check); + check = PyObject_CallMethod(self->wrapped, "_isinfinity", NULL); + if (abs(PyInt_AsLong(check)) == 1) { + res = PyString_FromString("'NaN'::numeric"); + goto end; + } + res = PyObject_Str(self->wrapped); +#else + check = PyObject_CallMethod(self->wrapped, "is_finite", NULL); if (check == Py_True) res = PyObject_Str(self->wrapped); else res = PyString_FromString("'NaN'::numeric"); +#endif + end: Py_DECREF(check); return res; }