Skip to content

Commit 55ff464

Browse files
YannickJadoulwjakob
authored andcommitted
Fixing SystemError when nb_bool/nb_nonzero sets a Python exception in type_caster<bool>::load (#1976)
1 parent b32b762 commit 55ff464

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

include/pybind11/cast.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,8 @@ template <> class type_caster<bool> {
11721172
if (res == 0 || res == 1) {
11731173
value = (bool) res;
11741174
return true;
1175+
} else {
1176+
PyErr_Clear();
11751177
}
11761178
}
11771179
return false;

tests/test_builtin_casters.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,11 +318,15 @@ def test_numpy_bool():
318318
import numpy as np
319319
convert, noconvert = m.bool_passthrough, m.bool_passthrough_noconvert
320320

321+
def cant_convert(v):
322+
pytest.raises(TypeError, convert, v)
323+
321324
# np.bool_ is not considered implicit
322325
assert convert(np.bool_(True)) is True
323326
assert convert(np.bool_(False)) is False
324327
assert noconvert(np.bool_(True)) is True
325328
assert noconvert(np.bool_(False)) is False
329+
cant_convert(np.zeros(2, dtype='int'))
326330

327331

328332
def test_int_long():

0 commit comments

Comments
 (0)