8000 BUG: Ensure Errors are correctly checked when PyFloat_AsDouble is cal… · juliantaylor/numpy@a6c3d0e · GitHub
[go: up one dir, main page]

Skip to content

Commit a6c3d0e

Browse files
committed
BUG: Ensure Errors are correctly checked when PyFloat_AsDouble is called.
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
1 parent c93201a commit a6c3d0e

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

numpy/random/mtrand/Python.pxi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ cdef extern from "Python.h":
1515
object PyString_FromStringAndSize(char* c_string, int length)
1616

1717
# Float API
18-
double PyFloat_AsDouble(object ob)
18+
double PyFloat_AsDouble(object ob) except? -1.0
1919
long PyInt_AsLong(object ob)
2020

2121
# Memory API

numpy/random/tests/test_random.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,20 @@ def test_uniform_range_bounds(self):
822822
# DBL_MAX by increasing fmin a bit
823823
np.random.uniform(low=np.nextafter(fmin, 1), high=fmax / 1e17)
824824

825+
def test_uniform_propogates_exeptions(self):
826+
# Tests that uniform correctly propogates exceptions
827+
# when called with a type which throws when converted to
828+
# a float
829+
#
830+
# Regression test for gh: 8865
831+
832+
class ThrowableType(np.ndarray):
833+
def __float__(self):
834+
raise ValueError
835+
836+
x = np.array(1.0).view(ThrowableType)
837+
assert_raises(ValueError, np.uniform, x, x)
838+
825839
def test_vonmises(self):
826840
np.random.seed(self.seed)
827841
actual = np.random.vonmises(mu=1.23, kappa=1.54, size=(3, 2))

0 commit comments

Comments
 (0)
0