Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ jobs:
- {python: "3.10", postgres: "14"}
- {python: "3.11", postgres: "15"}
- {python: "3.12", postgres: "16"}
- {python: "3.13-dev", postgres: "17"}
- {python: "3.13", postgres: "17"}

# Opposite extremes of the supported Py/PG range, other architecture
- {python: "3.8", postgres: "17", architecture: "x86"}
- {python: "3.9", postgres: "16", architecture: "x86"}
- {python: "3.10", postgres: "15", architecture: "x86"}
- {python: "3.11", postgres: "14", architecture: "x86"}
- {python: "3.12", postgres: "13", architecture: "x86"}
- {python: "3.13-dev", postgres: "12", architecture: "x86"}
- {python: "3.13", postgres: "12", architecture: "x86"}

env:
PSYCOPG2_TESTDB: postgres
Expand Down
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ What's new in psycopg 2.9.10 (unreleased)

- Add support for Python 3.13.
- Drop support for Python 3.7.
- Fix ``Decimal('Infinity')`` adaptation.


What's new in psycopg 2.9.9
Expand Down
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is not checking that check is NULL (which is in case of error).

It also leaks a refcount, as you overwrite check without a DECREF.

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same problem: no error checking, no decref.

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, I'm not sure this code is still used 😄

* We assume we are here because we didn't find the method. */
PyErr_Clear();
Expand Down
7 changes: 5 additions & 2 deletions tests/test_types_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,15 @@ def testDecimal(self):
self.failUnless(str(s) == "NaN", "wrong decimal quoting: " + str(s))
self.failUnless(type(s) == decimal.Decimal,
"wrong decimal conversion: " + repr(s))

@testutils.skip_before_postgres(14, 0)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SELECT 'Infinity'::numeric AS foo causes

psycopg2.errors.InvalidTextRepresentation: invalid input syntax for type numeric: "Infinity"

in Postgres 12 and 13.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, now that I think about it more we should probably rather check the server version when adapting Decimal('Infinity'), and fall back to NaN'::numeric on Postgres < 14.

Copy link
Member

@dvarrazzo dvarrazzo Oct 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uhm... Thinking a bit harder abut it: is it even a good idea to change the way inf is converted? Yes, converting it to nan wasn't the right thing to do, but maybe there is code that assumes that behaviour.

I am reconsidering the opportunity of making this change.

Note: psycopg 3 doesn't check for version and doesn't convert inf -> nan. Trying to send an inf to a server that doesn't support it results in a server error, which is the right thing to do.

Copy link
Contributor Author

@edgarrmondragon edgarrmondragon Oct 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. Either option (converting it or failing) is a potential breaking change. Happy to close this if you think we should err on preserving current behavior.

def testDecimalInfinity(self):
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