summaryrefslogtreecommitdiff
path: root/psycopg/adapter_pdecimal.c
diff options
context:
space:
mode:
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>2010-11-09 00:05:13 +0000
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>2010-11-09 00:14:22 +0000
commitd601ab82390f0dd731602a0a16f5184cf782ba2a (patch)
tree304413c3aa19c335813f1a528c5302565f90d534 /psycopg/adapter_pdecimal.c
parent0d318179a95b4021ec44dcd75b9c00f78753f45a (diff)
downloadpsycopg2-d601ab82390f0dd731602a0a16f5184cf782ba2a.tar.gz
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.
Diffstat (limited to 'psycopg/adapter_pdecimal.c')
-rw-r--r--psycopg/adapter_pdecimal.c40
1 files changed, 26 insertions, 14 deletions
diff --git a/psycopg/adapter_pdecimal.c b/psycopg/adapter_pdecimal.c
index 3112b8f..c11e753 100644
--- a/psycopg/adapter_pdecimal.c
+++ b/psycopg/adapter_pdecimal.c
@@ -43,29 +43,41 @@ static PyObject *
pdecimal_str(pdecimalObject *self)
{
PyObject *check, *res = NULL;
-#if PY_VERSION_HEX < 0x02050000
- check = PyObject_CallMethod(self->wrapped, "_isnan", NULL);
- if (PyInt_AsLong(check) == 1) {
+ check = PyObject_CallMethod(self->wrapped, "is_finite", NULL);
+ if (check == Py_True) {
+ res = PyObject_Str(self->wrapped);
+ goto end;
+ }
+ else if (check) {
+ res = PyString_FromString("'NaN'::numeric");
+ 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);
- check = PyObject_CallMethod(self->wrapped, "_isinfinity", NULL);
- if (abs(PyInt_AsLong(check)) == 1) {
+ 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);
-#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);
+end:
+ Py_XDECREF(check);
return res;
}