BUG: do a boolean check on casting bool to other types

avoids some issues of booleans coming from external buffers that may not
only contain 0 or 1.
closes #3801.
Merge pull request #3811 from charris/backport-1.8-3802

Backport 1.8 3802