fixed crash in pdecimal_str with a few Python 2.5.x releases.

is_finite() is not available in 2.5.1, it is in 2.5.5 but is officially
supported only since 2.6.
This commit is contained in:
Daniele Varrazzo 2010-11-09 00:05:13 +00:00
parent 0d318179a9
commit d601ab8239
3 changed files with 34 additions and 18 deletions

View File

@ -7,6 +7,9 @@
* psycopg/microprotocols.c: use the adapter of an object superclass if * psycopg/microprotocols.c: use the adapter of an object superclass if
available. available.
* psycopg/adapter_pdecimal.c: fixed crash in pdecimal_str with Python
2.5.x releases in which is_finite() is not available.
2010-11-06 Daniele Varrazzo <daniele.varrazzo@gmail.com> 2010-11-06 Daniele Varrazzo <daniele.varrazzo@gmail.com>
* lib/extras.py: added NamedTupleCursor. * lib/extras.py: added NamedTupleCursor.

View File

@ -25,6 +25,7 @@ psycopg 2.3 aims to expose some new features introduced in PostgreSQL 9.0.
- Fixed use of `PQfreemem` instead of `free` in binary typecaster. - Fixed use of `PQfreemem` instead of `free` in binary typecaster.
- Fixed access to freed memory in `conn_get_isolation_level()`. - Fixed access to freed memory in `conn_get_isolation_level()`.
- Fixed crash during Decimal adaptation with a few 2.5.x Python versions.
What's new in psycopg 2.2.2 What's new in psycopg 2.2.2

View File

@ -43,29 +43,41 @@ static PyObject *
pdecimal_str(pdecimalObject *self) pdecimal_str(pdecimalObject *self)
{ {
PyObject *check, *res = 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); check = PyObject_CallMethod(self->wrapped, "is_finite", NULL);
if (check == Py_True) if (check == Py_True) {
res = PyObject_Str(self->wrapped); res = PyObject_Str(self->wrapped);
else goto end;
}
else if (check) {
res = PyString_FromString("'NaN'::numeric"); res = PyString_FromString("'NaN'::numeric");
#endif goto end;
}
/* is_finite() was introduced 2.5.1 < somewhere <= 2.5.4.
* We assume we are here because we didn't find the method. */
PyErr_Clear();
if (!(check = PyObject_CallMethod(self->wrapped, "_isnan", NULL))) {
goto end;
}
if (PyObject_IsTrue(check)) {
res = PyString_FromString("'NaN'::numeric");
goto end;
}
Py_DECREF(check);
if (!(check = PyObject_CallMethod(self->wrapped, "_isinfinity", NULL))) {
goto end;
}
if (PyObject_IsTrue(check)) {
res = PyString_FromString("'NaN'::numeric");
goto end;
}
res = PyObject_Str(self->wrapped);
end: end:
Py_DECREF(check); Py_XDECREF(check);
return res; return res;
} }