Skip to content

Commit

Permalink
Simplify numpy.bool_ detection (use type name only)
Browse files Browse the repository at this point in the history
  • Loading branch information
aldanor committed Jul 23, 2017
1 parent 31401f8 commit 025ba8a
Showing 1 changed file with 3 additions and 13 deletions.
16 changes: 3 additions & 13 deletions include/pybind11/cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -1053,7 +1053,9 @@ template <> class type_caster<bool> {
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
Expand All @@ -1076,18 +1078,6 @@ template <> class type_caster<bool> {
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<char>() == 'b') {
value = PyObject_IsTrue(src.ptr()) == 1;
return true;
}
}
return false;
}
Expand Down

0 comments on commit 025ba8a

Please sign in to comment.