Copilot commented on code in PR #50827:
URL: https://github.com/apache/arrow/pull/50827#discussion_r3730204633


##########
cpp/src/arrow/c/dlpack.cc:
##########
@@ -131,30 +155,34 @@ Result<DLDevice> ExportDevice(const 
std::shared_ptr<Array>& arr) {
   }
 }
 
+namespace {
+
+template <typename DT>
 struct TensorManagerCtx {
   std::shared_ptr<Tensor> t;
   std::vector<int64_t> strides;
   std::vector<int64_t> shape;
-  DLManagedTensor tensor;
+  DT tensor;
 };
 
-Result<DLManagedTensor*> ExportTensor(const std::shared_ptr<Tensor>& t) {
+template <typename DT>
+Result<DT*> ExportTensorImpl(const std::shared_ptr<Tensor>& t) {
   // Define the DLDataType struct
   const DataType& type = *t->type();
   ARROW_ASSIGN_OR_RAISE(auto dlpack_type, GetDLDataType(type));
 
   // Define DLDevice struct
-  ARROW_ASSIGN_OR_RAISE(auto device, ExportDevice(t))
+  ARROW_ASSIGN_OR_RAISE(auto device, ExportDevice(t));
 
   // Create TensorManagerCtx that will serve as the owner of the 
DLManagedTensor
-  auto ctx = std::make_unique<TensorManagerCtx>();
+  auto ctx = std::make_unique<TensorManagerCtx<DT>>();
 
   // Define the data pointer to the DLTensor
   // If tensor is of length 0, data pointer should be NULL
   if (t->size() == 0) {
-    ctx->tensor.dl_tensor.data = NULL;
+    ctx->tensor.dl_tensor.data = nullptr;
   } else {
-    ctx->tensor.dl_tensor.data = t->raw_mutable_data();
+    ctx->tensor.dl_tensor.data = const_cast<uint8_t*>(t->raw_data());
   }

Review Comment:
   `ExportTensorImpl` now exports immutable tensors through the legacy 
`DLManagedTensor` path by `const_cast`ing `raw_data()`. Because the legacy 
DLPack struct has no read-only flag, consumers may legally write through 
`DLTensor::data`, which would violate Arrow’s immutability contract for 
non-mutable tensors/buffers. Consider rejecting immutable tensors for the 
legacy API and requiring the versioned export (which can set 
`DLPACK_FLAG_BITMASK_READ_ONLY`).



-- 
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