-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
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
BUG: Ensure Errors are correctly checked when PyFloat_AsDouble is called #8883
Conversation
@@ -15,7 +15,7 @@ cdef extern from "Python.h": | |||
object PyString_FromStringAndSize(char* c_string, int length) | |||
|
|||
# Float API | |||
double PyFloat_AsDouble(object ob) | |||
double PyFloat_AsDouble(object ob) except? -1.0 |
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 causes cython to always check for raised exceptions when PyFloat_AsDouble
returns -1.0.
This is what is recommended in the python API docs:
https://docs.python.org/2/c-api/float.html#c.PyFloat_AsDouble
adf5733
to
cfe04f0
Compare
…led. There was an error in np.random.uniform where if np.random.uniform were called with a type that throwed exceptions when it was converted to a float this exception wouldn't be raised. This bug was due to an issue where PyFloat_AsDouble was called but no check for PyErr_Occurred was performed after. This PR fixes the issue by ensuring that Cython will always emit a call to PyErr_Occurred if PyFloat_AsDouble returns -1.0 Fixes: numpy#8865
cfe04f0
to
a6c3d0e
Compare
On a skim of |
Thanks a lot, Simon! And nice Cython syntax, didn't know about that one. |
Should |
Yes I just noticed that one as well, looks like there are some cases where the same issue will occur. I'm just writing a failing test for that and will submit another PR with that. |
After numpy#8883 was merged it was noticed that the same problem was occuring with calls to PyInt_AsLong. Namely that PyErr_Occoured wasn't being checked if it returned -1 indicating an exception could have been thrown. This PR adds those checks as well as a regression test.
There was an error in np.random.uniform where if np.random.uniform
were called with a type that throwed exceptions when it was converted
to a float this exception wouldn't be raised.
This bug was due to an issue where PyFloat_AsDouble was called but
no check for PyErr_Occurred was performed after.
This PR fixes the issue by ensuring that Cython will always emit a
call to PyErr_Occurred if PyFloat_AsDouble returns -1.0
Fixes: #8865