From 025ba8a8b50cc25696ccb97aaa1834dd70b84aa0 Mon Sep 17 00:00:00 2001 From: Ivan Smirnov Date: Sun, 23 Jul 2017 12:15:06 +0100 Subject: [PATCH] Simplify numpy.bool_ detection (use type name only) --- include/pybind11/cast.h | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 3535cf9332..0b41f942bc 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -1053,7 +1053,9 @@ template <> class type_caster { if (!src) return false; else if (src.ptr() == Py_True) { value = true; return true; } else if (src.ptr() == Py_False) { value = false; return true; } - else if (convert) { + else if (convert || !strcmp("numpy.bool_", Py_TYPE(src.ptr())->tp_name)) { + // (allow non-implicit conversion for numpy booleans) + Py_ssize_t res = -1; if (src.is_none()) { res = 0; // None is implicitly converted to False @@ -1076,18 +1078,6 @@ template <> class type_caster { value = (bool) res; return true; } - return false; - } - else if (hasattr(src, "dtype")) { - // Allow non-implicit conversion for numpy booleans - // - // Note: this will only run in the first (noconvert) pass; - // during the second pass, it will be handled by __bool__ logic. - auto dtype = src.attr("dtype"); - if (hasattr(dtype, "kind") && dtype.attr("kind").cast() == 'b') { - value = PyObject_IsTrue(src.ptr()) == 1; - return true; - } } return false; }