paleolimbot commented on a change in pull request #11919:
URL: https://github.com/apache/arrow/pull/11919#discussion_r766058487



##########
File path: r/tests/testthat/test-python.R
##########
@@ -143,3 +143,70 @@ test_that("RecordBatchReader from python", {
   expect_r6_class(rt_table, "Table")
   expect_identical(as.data.frame(rt_table), example_data)
 })
+
+test_that("Pointer wrapper accepts external pointers", {
+  ptr <- allocate_arrow_schema()
+  exportable <- int32()
+  exportable$export_to_c(ptr)
+
+  # make sure exportable is released and deleted
+  expect_equal(DataType$import_from_c(ptr), int32())
+  delete_arrow_schema(ptr)
+})
+
+test_that("Pointer wrapper accepts double-casted pointers", {
+  ptr <- allocate_arrow_schema()
+  exportable <- int32()
+  exportable$export_to_c(external_pointer_addr_double(ptr))
+
+  # make sure exportable is released and deleted
+  expect_equal(DataType$import_from_c(ptr), int32())

Review comment:
       They do! It's the `Pointer<>` wrapper that's automatically invoked for 
all import/export methods (but I added the test for the import method for 
DataType also because it was easy). I could also test more than just 
`DataType`'s import/export but it's just a proxy for the `Pointer<>` here.

##########
File path: r/src/arrow_cpp11.h
##########
@@ -57,13 +57,45 @@ namespace r {
 template <typename T>
 struct Pointer {
   Pointer() : ptr_(new T()) {}
-  explicit Pointer(SEXP x)
-      : ptr_(reinterpret_cast<T*>(static_cast<uintptr_t>(REAL(x)[0]))) {}
+  explicit Pointer(SEXP x) {
+    if (TYPEOF(x) == EXTPTRSXP) {
+      ptr_ = (T*)R_ExternalPtrAddr(x);
+    } else if (TYPEOF(x) == STRSXP && Rf_length(x) == 1) {
+      // User passed an character representation of the pointer address
+      SEXP char0 = STRING_ELT(x, 0);
+      if (char0 == NA_STRING) {
+        cpp11::stop("Can't convert NA_character_ to pointer");
+      }
+
+      const char* input_chars = CHAR(char0);
+      char* endptr;
+      uint64_t ptr_value = strtoll(input_chars, &endptr, 0);

Review comment:
       Done!




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to