Fix Decimal('Infinity') adaptation

Closes https://github.com/psycopg/psycopg2/issues/1724
This commit is contained in:
Edgar Ramírez-Mondragón 2024-10-10 18:03:48 -06:00
parent eaeeb76944
commit 4dbc04fd82
No known key found for this signature in database
GPG Key ID: 74C40D09C7B42099
2 changed files with 16 additions and 3 deletions

View File

@ -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();

View File

@ -75,11 +75,11 @@ class TypesBasicTests(ConnectingTestCase):
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))