Skip to content

Commit d8edcd7

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 d8edcd7

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

include/pybind11/cast.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,9 @@ template <> class type_caster<bool> {
11731173
value = (bool) res;
11741174
return true;
11751175
}
1176+
else {
1177+
PyErr_Clear();
1178+
}
11761179
}
11771180
return false;
11781181
}

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)