Skip to content

Commit

Permalink
Merge pull request numpy#8884 from simongibbons/add-python-cython-err…
Browse files Browse the repository at this point in the history
…or-checking

BUG: Check for errors when PyInt_AsLong is called in np.random
  • Loading branch information
jaimefrio authored Apr 3, 2017
2 parents 77eab40 + 30ab8fc commit 6e17c72
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion numpy/random/mtrand/Python.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 15 additions & 8 deletions numpy/random/tests/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -822,19 +822,26 @@ def test_uniform_range_bounds(self):
# 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)
Expand Down

0 comments on commit 6e17c72

Please sign in to comment.