Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
t-kalinowski committed Dec 14, 2023
1 parent d1b2c59 commit c909b57
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3674,20 +3674,23 @@ SEXP py_len_impl(PyObjectRef x, SEXP defaultValue = R_NilValue) {

// [[Rcpp::export]]
SEXP py_bool_impl(PyObjectRef x, bool silent = false) {
PyErrorScopeGuard py_error_guard;

// evaluate Python `not not x`
int result = PyObject_IsTrue(x);
int result;
if(silent) {
PyErrorScopeGuard _g;

if(result == -1) {
// Should only happen if the object has a `__bool__` method that
// intentionally throws an exception.
if (silent) {
// evaluate Python `not not x`
result = PyObject_IsTrue(x);
// result==-1 should only happen if the object has a
// __bool__() method that intentionally raises an exception.
if(result == -1)
result = NA_LOGICAL;
} else {
py_error_guard.release();

} else {

result = PyObject_IsTrue(x);
if(result == -1)
throw PythonException(py_fetch_error());
}

}

return Rf_ScalarLogical(result);
Expand Down

0 comments on commit c909b57

Please sign in to comment.