diff options
author | Simon Gibbons <simongibbons@gmail.com> | 2017-04-01 23:46:52 +0100 |
---|---|---|
committer | Simon Gibbons <simongibbons@gmail.com> | 2017-04-02 23:17:33 +0100 |
commit | 30ab8fc16883f111eba3c873eff32323eeb58747 (patch) | |
tree | efed7911b75acd20a62fd6ee3ba006f7cc5ba3e0 /numpy/random | |
parent | 02e4a57d73d093db347e5afbeaf23153a86f4136 (diff) | |
download | python-numpy-30ab8fc16883f111eba3c873eff32323eeb58747.tar.gz python-numpy-30ab8fc16883f111eba3c873eff32323eeb58747.tar.bz2 python-numpy-30ab8fc16883f111eba3c873eff32323eeb58747.zip |
BUG: Check for errors when PyInt_AsLong is called in np.random
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.
Diffstat (limited to 'numpy/random')
-rw-r--r-- | numpy/random/mtrand/Python.pxi | 2 | ||||
-rw-r--r-- | numpy/random/tests/test_random.py | 23 |
2 files changed, 16 insertions, 9 deletions
diff --git a/numpy/random/mtrand/Python.pxi b/numpy/random/mtrand/Python.pxi index 0571831a6..08aadbaa1 100644 --- a/numpy/random/mtrand/Python.pxi +++ b/numpy/random/mtrand/Python.pxi @@ -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 # Memory API void* PyMem_Malloc(size_t n) diff --git a/numpy/random/tests/test_random.py b/numpy/random/tests/test_random.py index 9c098eef0..e10c442a5 100644 --- a/numpy/random/tests/test_random.py +++ b/numpy/random/tests/test_random.py @@ -822,19 +822,26 @@ class TestRandomDist(TestCase): # DBL_MAX by increasing fmin a bit np.random.uniform(low=np.nextafter(fmin, 1), high=fmax / 1e17) - def test_uniform_propogates_exeptions(self): - # Tests that uniform correctly propogates exceptions - # when called with a type which throws when converted to - # a float + def test_scalar_exception_propagation(self): + # Tests that exceptions are correctly propagated in distributions + # when called with objects that throw exceptions when converted to + # scalars. # # Regression test for gh: 8865 - class ThrowableType(np.ndarray): + class ThrowingFloat(np.ndarray): def __float__(self): - raise ValueError + raise TypeError - x = np.array(1.0).view(ThrowableType) - assert_raises(ValueError, np.random.uniform, x, x) + throwing_float = np.array(1.0).view(ThrowingFloat) + assert_raises(TypeError, np.random.uniform, throwing_float, throwing_float) + + class ThrowingInteger(np.ndarray): + def __int__(self): + raise TypeError + + throwing_int = np.array(1).view(ThrowingInteger) + assert_raises(TypeError, np.random.hypergeometric, throwing_int, 1, 1) def test_vonmises(self): np.random.seed(self.seed) |