|
5 | 5 | TestCase, run_module_suite, assert_, assert_raises, assert_equal, |
6 | 6 | assert_warns, assert_no_warnings, assert_array_equal, |
7 | 7 | assert_array_almost_equal) |
8 | | -from numpy.compat import asbytes |
9 | 8 | import sys |
10 | 9 | import warnings |
11 | 10 | import randomstate as random |
@@ -394,7 +393,7 @@ def test_choice_return_shape(self): |
394 | 393 | def test_bytes(self): |
395 | 394 | mt19937.seed(self.seed) |
396 | 395 | actual = mt19937.bytes(10) |
397 | | - desired = asbytes('\x82Ui\x9e\xff\x97+Wf\xa5') |
| 396 | + desired = b'\x82Ui\x9e\xff\x97+Wf\xa5' |
398 | 397 | assert_equal(actual, desired) |
399 | 398 |
|
400 | 399 | def test_shuffle(self): |
@@ -824,6 +823,27 @@ def test_uniform_range_bounds(self): |
824 | 823 | # DBL_MAX by increasing fmin a bit |
825 | 824 | mt19937.uniform(low=np.nextafter(fmin, 1), high=fmax / 1e17) |
826 | 825 |
|
| 826 | + def test_scalar_exception_propagation(self): |
| 827 | + # Tests that exceptions are correctly propagated in distributions |
| 828 | + # when called with objects that throw exceptions when converted to |
| 829 | + # scalars. |
| 830 | + # |
| 831 | + # Regression test for gh: 8865 |
| 832 | + |
| 833 | + class ThrowingFloat(np.ndarray): |
| 834 | + def __float__(self): |
| 835 | + raise TypeError |
| 836 | + |
| 837 | + throwing_float = np.array(1.0).view(ThrowingFloat) |
| 838 | + assert_raises(TypeError, mt19937.uniform, throwing_float, throwing_float) |
| 839 | + |
| 840 | + class ThrowingInteger(np.ndarray): |
| 841 | + def __int__(self): |
| 842 | + raise TypeError |
| 843 | + |
| 844 | + throwing_int = np.array(1).view(ThrowingInteger) |
| 845 | + assert_raises(TypeError, mt19937.hypergeometric, throwing_int, 1, 1) |
| 846 | + |
827 | 847 | def test_vonmises(self): |
828 | 848 | mt19937.seed(self.seed) |
829 | 849 | actual = mt19937.vonmises(mu=1.23, kappa=1.54, size=(3, 2)) |
|
0 commit comments