From 29158b6dbbf0b78679ddb3e33a42f76b8ca4bcb2 Mon Sep 17 00:00:00 2001 From: Yannick Jadoul Date: Thu, 31 Oct 2019 18:42:23 +0100 Subject: [PATCH] Fixing SystemError when nb_bool/nb_nonzero sets a Python exception in type_caster::load --- include/pybind11/cast.h | 3 +++ tests/test_builtin_casters.py | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 90407eb984..50f6b607cf 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -1173,6 +1173,9 @@ template <> class type_caster { 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 73cc465f5b..abbfcec493 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():