Fixing SystemError when nb_bool/nb_nonzero sets a Python exception in type_caster<bool>::load (#1976)
diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h
index 90407eb..3d4a759 100644
--- a/include/pybind11/cast.h
+++ b/include/pybind11/cast.h
@@ -1172,6 +1172,8 @@
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 73cc465..abbfcec 100644
--- a/tests/test_builtin_casters.py
+++ b/tests/test_builtin_casters.py
@@ -318,11 +318,15 @@
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():