Skip to content

Commit 025ba8a

Browse files
committed
Simplify numpy.bool_ detection (use type name only)
1 parent 31401f8 commit 025ba8a

File tree

1 file changed

+3
-13
lines changed

1 file changed

+3
-13
lines changed

include/pybind11/cast.h

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,7 +1053,9 @@ template <> class type_caster<bool> {
10531053
if (!src) return false;
10541054
else if (src.ptr() == Py_True) { value = true; return true; }
10551055
else if (src.ptr() == Py_False) { value = false; return true; }
1056-
else if (convert) {
1056+
else if (convert || !strcmp("numpy.bool_", Py_TYPE(src.ptr())->tp_name)) {
1057+
// (allow non-implicit conversion for numpy booleans)
1058+
10571059
Py_ssize_t res = -1;
10581060
if (src.is_none()) {
10591061
res = 0; // None is implicitly converted to False
@@ -1076,18 +1078,6 @@ template <> class type_caster<bool> {
10761078
value = (bool) res;
10771079
return true;
10781080
}
1079-
return false;
1080-
}
1081-
else if (hasattr(src, "dtype")) {
1082-
// Allow non-implicit conversion for numpy booleans
1083-
//
1084-
// Note: this will only run in the first (noconvert) pass;
1085-
// during the second pass, it will be handled by __bool__ logic.
1086-
auto dtype = src.attr("dtype");
1087-
if (hasattr(dtype, "kind") && dtype.attr("kind").cast<char>() == 'b') {
1088-
value = PyObject_IsTrue(src.ptr()) == 1;
1089-
return true;
1090-
}
10911081
}
10921082
return false;
10931083
}

0 commit comments

Comments
 (0)