Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Decimal('Infinity') adaptation #1735

Closed

Conversation

edgarrmondragon
Copy link
Contributor

Closes #1724

@edgarrmondragon edgarrmondragon marked this pull request as ready for review October 11, 2024 00:15
@@ -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.

@@ -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.

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.

@@ -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
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.

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 😄

@dvarrazzo
Copy link
Member

Thank you very much for this MR, but as discussed it's probably not a good idea to do this in psycopg2.

@dvarrazzo dvarrazzo closed this Oct 30, 2024
@edgarrmondragon edgarrmondragon deleted the 1724-decimal-infinity branch October 30, 2024 14:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect Decimal('Infinity') adaptation
2 participants