Skip to content

Commit 734790f

Browse files
committed
Update based on feedback
1 parent df88383 commit 734790f

1 file changed

Lines changed: 29 additions & 5 deletions

File tree

r/src/arrow_cpp11.h

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,26 @@ Pointer r6_to_pointer(SEXP self) {
208208
cpp11::stop("Invalid R object for %s, must be an ArrowObject", type_name.c_str());
209209
}
210210

211-
#if R_VERSION >= R_Version(4, 5, 0)
211+
// R_UnboundValue and Rf_findVarInFrame are non-API as of R 4.6
212+
#if R_VERSION >= R_Version(4, 6, 0)
213+
if (!R_existsVarInFrame(self, arrow::r::symbols::xp)) {
214+
cpp11::stop("Invalid: self$`.:xp:.` is NULL");
215+
}
216+
SEXP xp = R_getVar(arrow::r::symbols::xp, self, FALSE);
217+
if (xp == R_NilValue) {
218+
cpp11::stop("Invalid: self$`.:xp:.` is NULL");
219+
}
220+
#elif R_VERSION >= R_Version(4, 5, 0)
212221
SEXP xp = R_getVarEx(arrow::r::symbols::xp, self, FALSE, R_UnboundValue);
222+
if (xp == R_UnboundValue || xp == R_NilValue) {
223+
cpp11::stop("Invalid: self$`.:xp:.` is NULL");
224+
}
213225
#else
214226
SEXP xp = Rf_findVarInFrame(self, arrow::r::symbols::xp);
215-
#endif
216227
if (xp == R_UnboundValue || xp == R_NilValue) {
217228
cpp11::stop("Invalid: self$`.:xp:.` is NULL");
218229
}
230+
#endif
219231

220232
void* p = R_ExternalPtrAddr(xp);
221233
if (p == nullptr) {
@@ -227,10 +239,22 @@ Pointer r6_to_pointer(SEXP self) {
227239

228240
template <typename T>
229241
void r6_reset_pointer(SEXP r6) {
230-
#if R_VERSION >= R_Version(4, 5, 0)
242+
// R_UnboundValue and Rf_findVarInFrame are non-API as of R 4.6
243+
#if R_VERSION >= R_Version(4, 6, 0)
244+
if (!R_existsVarInFrame(r6, arrow::r::symbols::xp)) {
245+
return;
246+
}
247+
SEXP xp = R_getVar(arrow::r::symbols::xp, r6, FALSE);
248+
#elif R_VERSION >= R_Version(4, 5, 0)
231249
SEXP xp = R_getVarEx(arrow::r::symbols::xp, r6, FALSE, R_UnboundValue);
250+
if (xp == R_UnboundValue) {
251+
return;
252+
}
232253
#else
233254
SEXP xp = Rf_findVarInFrame(r6, arrow::r::symbols::xp);
255+
if (xp == R_UnboundValue) {
256+
return;
257+
}
234258
#endif
235259
void* p = R_ExternalPtrAddr(xp);
236260
if (p != nullptr) {
@@ -388,8 +412,8 @@ SEXP to_r6(const std::shared_ptr<T>& ptr, const char* r6_class_name) {
388412
cpp11::external_pointer<std::shared_ptr<T>> xp(new std::shared_ptr<T>(ptr));
389413
SEXP r6_class = Rf_install(r6_class_name);
390414

391-
// R_existsVarInFrame doesn't exist before R 4.2, so we need to fall back to
392-
// Rf_findVarInFrame3 if it is not defined.
415+
// Rf_findVarInFrame3 and R_UnboundValue are non-API as of R 4.6.
416+
// R_existsVarInFrame doesn't exist before R 4.2.
393417
#if R_VERSION >= R_Version(4, 2, 0)
394418
if (!R_existsVarInFrame(arrow::r::ns::arrow, r6_class)) {
395419
cpp11::stop("No arrow R6 class named '%s'", r6_class_name);

0 commit comments

Comments
 (0)