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

BUG: Ensure Errors are correctly checked when PyFloat_AsDouble is called #8883

Merged
merged 2 commits into from
Apr 1, 2017

Conversation

simongibbons
Copy link
Contributor

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

@@ -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
Copy link
Contributor Author

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

…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
@simongibbons
Copy link
Contributor Author

simongibbons commented Apr 1, 2017

On a skim of mtrand.pyx it looks like PyFloat_AsDouble was being called in many places without this check being performed. This fix will work for all of those locations.

@jaimefrio jaimefrio merged commit 02e4a57 into numpy:master Apr 1, 2017
@jaimefrio
Copy link
Member

Thanks a lot, Simon!

And nice Cython syntax, didn't know about that one.

@simongibbons simongibbons deleted the fix-uniform-error branch April 1, 2017 22:19
@bashtage
Copy link
Contributor

bashtage commented Apr 1, 2017

Should long PyInt_AsLong(object ob) also be regularly checked?

@simongibbons
Copy link
Contributor Author

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.

simongibbons added a commit to simongibbons/numpy that referenced this pull request Apr 2, 2017
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.
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.

np.random.uniform behaves strangely with subclasses that raise errors
3 participants