-
-
Notifications
You must be signed in to change notification settings - Fork 528
Fix Decimal('Infinity') adaptation
#1735
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
in Postgres 12 and 13.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) | ||
|
|
||
|
|
||
There was a problem hiding this comment.
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
checkisNULL(which is in case of error).It also leaks a refcount, as you overwrite
checkwithout a DECREF.