diff --git a/psycopg/adapter_pdecimal.c b/psycopg/adapter_pdecimal.c index 25a7212d1..15ae13112 100644 --- a/psycopg/adapter_pdecimal.c +++ b/psycopg/adapter_pdecimal.c @@ -47,11 +47,24 @@ pdecimal_getquoted(pdecimalObject *self, PyObject *args) } goto output; } - else if (check) { + + check = PyObject_CallMethod(self->wrapped, "is_nan", NULL); + if (check == Py_True) { res = Bytes_FromString("'NaN'::numeric"); goto end; } + /* If the decimal is not finite and not NaN, it must be infinity, + * so all that is left is to check if it is positive or negative. */ + check = PyObject_CallMethod(self->wrapped, "is_signed", NULL); + if (check == Py_True) { + res = Bytes_FromString("'-Infinity'::numeric"); + goto end; + } else { + res = Bytes_FromString("'Infinity'::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(); diff --git a/tests/test_types_basic.py b/tests/test_types_basic.py index 31f0310d4..c3f615904 100755 --- a/tests/test_types_basic.py +++ b/tests/test_types_basic.py @@ -75,11 +75,11 @@ def testDecimal(self): self.failUnless(type(s) == decimal.Decimal, "wrong decimal conversion: " + repr(s)) s = self.execute("SELECT %s AS foo", (decimal.Decimal("infinity"),)) - self.failUnless(str(s) == "NaN", "wrong decimal quoting: " + str(s)) + self.failUnless(str(s) == "Infinity", "wrong decimal quoting: " + str(s)) self.failUnless(type(s) == decimal.Decimal, "wrong decimal conversion: " + repr(s)) s = self.execute("SELECT %s AS foo", (decimal.Decimal("-infinity"),)) - self.failUnless(str(s) == "NaN", "wrong decimal quoting: " + str(s)) + self.failUnless(str(s) == "-Infinity", "wrong decimal quoting: " + str(s)) self.failUnless(type(s) == decimal.Decimal, "wrong decimal conversion: " + repr(s))