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


##########
python/pyarrow/_dlpack.pxi:
##########
@@ -44,3 +44,31 @@ cdef void dlpack_pycapsule_deleter(object dltensor) noexcept:
 
     # Set the error indicator from err_type, err_value, err_traceback
     cpython.PyErr_Restore(err_type, err_value, err_traceback)
+
+
+cdef void dlpack_versioned_pycapsule_deleter(object dltensor) noexcept:
+    cdef DLManagedTensorVersioned* dlm_tensor
+    cdef PyObject* err_type
+    cdef PyObject* err_value
+    cdef PyObject* err_traceback
+
+    # Do nothing if the capsule has been consumed
+    if cpython.PyCapsule_IsValid(dltensor, "used_dltensor_versioned"):
+        return
+
+    # An exception may be in-flight, we must save it in case
+    # we create another one
+    cpython.PyErr_Fetch(&err_type, &err_value, &err_traceback)
+
+    dlm_tensor = <DLManagedTensorVersioned*>cpython.PyCapsule_GetPointer(
+        dltensor, 'dltensor_versioned')
+    if dlm_tensor == NULL:
+        cpython.PyErr_WriteUnraisable(dltensor)

Review Comment:
   I am not sure that is true. Looking at the [Python docs for this 
PyErr](https://docs.python.org/3/c-api/exceptions.html#c.PyErr_WriteUnraisable):
   
   > The function is called with a single argument obj [PyObject *obj] that 
identifies the context in which the unraisable exception occurred. If possible, 
the repr of obj will be printed in the warning message. If obj is NULL, only 
the traceback is printed.
   
   And as `dlm_tensor` is `NULL` and not a `PyObject` we need to use the 
capsule `dltensor` object in this case.
   
   Also, it is used in the same way in the dlpack documentation, towards the 
end of the Implementation section: 
https://dmlc.github.io/dlpack/latest/python_spec.html#implementation.



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