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: Check for errors when PyInt_AsLong is called in np.random #8884

Merged

Conversation

simongibbons
Copy link
Contributor

After #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.

@@ -16,7 +16,7 @@ cdef extern from "Python.h":

# Float API
double PyFloat_AsDouble(object ob) except? -1.0
long PyInt_AsLong(object ob)
long PyInt_AsLong(object ob) except? -1
Copy link
Contributor Author

Choose a reason for hiding this comment

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

PyInt_AsLong may have thrown an exception if it returns -1
https://docs.python.org/2/c-api/int.html#c.PyInt_AsLong


class ThrowingInteger(np.ndarray):
def __int__(self):
raise TypeError
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for doing this, just a minor nitpick...

Someone seeing this test and the one you wrote for PyFloat_AsDouble may think there's something fundamentally different between both cases since the exceptions are different. So it would probably be good to make both more equal by having both raise the same error.

I also think it would be good to group them both in a test_scalar_exception_propagation method, rather than have them grouped with the random method used for testing, but this may be a matter of personal taste.

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.
@simongibbons simongibbons force-pushed the add-python-cython-error-checking branch from f1875cc to 30ab8fc Compare April 2, 2017 22:17
@simongibbons
Copy link
Contributor Author

@jaimefrio I've updated the tests taking your comments onboard. Note that the change in exception was intentional as there was some checks that all of the integers passed into hypergeometric were positive, raising a ValueError if not. (So an exception would be thrown, just the wrong one). https://github.com/numpy/numpy/blob/master/numpy/random/mtrand/mtrand.pyx#L4239

I agree the tests should be combined though and have done.

@jaimefrio jaimefrio merged commit 6e17c72 into numpy:master Apr 3, 2017
@jaimefrio
Copy link
Member

Looks much better to me now. Thanks, in it goes!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants