diff --git a/r/src/arrow_cpp11.h b/r/src/arrow_cpp11.h index d28ad0feded..43296238513 100644 --- a/r/src/arrow_cpp11.h +++ b/r/src/arrow_cpp11.h @@ -208,14 +208,26 @@ Pointer r6_to_pointer(SEXP self) { cpp11::stop("Invalid R object for %s, must be an ArrowObject", type_name.c_str()); } -#if R_VERSION >= R_Version(4, 5, 0) +// R_UnboundValue and Rf_findVarInFrame are non-API as of R 4.6 +#if R_VERSION >= R_Version(4, 6, 0) + SEXP xp = R_NilValue; + if (R_existsVarInFrame(self, arrow::r::symbols::xp)) { + xp = R_getVar(arrow::r::symbols::xp, self, FALSE); + } + if (xp == R_NilValue) { + cpp11::stop("Invalid: self$`.:xp:.` is NULL"); + } +#elif R_VERSION >= R_Version(4, 5, 0) SEXP xp = R_getVarEx(arrow::r::symbols::xp, self, FALSE, R_UnboundValue); + if (xp == R_UnboundValue || xp == R_NilValue) { + cpp11::stop("Invalid: self$`.:xp:.` is NULL"); + } #else SEXP xp = Rf_findVarInFrame(self, arrow::r::symbols::xp); -#endif if (xp == R_UnboundValue || xp == R_NilValue) { cpp11::stop("Invalid: self$`.:xp:.` is NULL"); } +#endif void* p = R_ExternalPtrAddr(xp); if (p == nullptr) { @@ -227,7 +239,13 @@ Pointer r6_to_pointer(SEXP self) { template void r6_reset_pointer(SEXP r6) { -#if R_VERSION >= R_Version(4, 5, 0) +// R_UnboundValue and Rf_findVarInFrame are non-API as of R 4.6 +#if R_VERSION >= R_Version(4, 6, 0) + if (!R_existsVarInFrame(r6, arrow::r::symbols::xp)) { + return; + } + SEXP xp = R_getVar(arrow::r::symbols::xp, r6, FALSE); +#elif R_VERSION >= R_Version(4, 5, 0) SEXP xp = R_getVarEx(arrow::r::symbols::xp, r6, FALSE, R_UnboundValue); #else SEXP xp = Rf_findVarInFrame(r6, arrow::r::symbols::xp);