diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 90407eb98..3d4a759c3 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -1172,6 +1172,8 @@ public: if (res == 0 || res == 1) { value = (bool) res; return true; + } else { + PyErr_Clear(); } } return false; diff --git a/tests/test_builtin_casters.py b/tests/test_builtin_casters.py index 73cc465f5..abbfcec49 100644 --- a/tests/test_builtin_casters.py +++ b/tests/test_builtin_casters.py @@ -318,11 +318,15 @@ def test_numpy_bool(): import numpy as np convert, noconvert = m.bool_passthrough, m.bool_passthrough_noconvert + def cant_convert(v): + pytest.raises(TypeError, convert, v) + # np.bool_ is not considered implicit assert convert(np.bool_(True)) is True assert convert(np.bool_(False)) is False assert noconvert(np.bool_(True)) is True assert noconvert(np.bool_(False)) is False + cant_convert(np.zeros(2, dtype='int')) def test_int_long():