Skip to content

Commit

Permalink
Fix Decimal('Infinity') adaptation
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarrmondragon committed Oct 11, 2024
1 parent eaeeb76 commit 4dbc04f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
15 changes: 14 additions & 1 deletion psycopg/adapter_pdecimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions tests/test_types_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down

0 comments on commit 4dbc04f

Please sign in to comment.