Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-39942: [Python] Make capsule name check more lenient #39977

Merged
merged 7 commits into from
Feb 9, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions python/pyarrow/types.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@
# specific language governing permissions and limitations
# under the License.

from cpython.pycapsule cimport PyCapsule_CheckExact, PyCapsule_GetPointer, PyCapsule_New, PyCapsule_IsValid
from cpython.pycapsule cimport (
PyCapsule_CheckExact,
PyCapsule_GetPointer,
PyCapsule_GetName,
PyCapsule_New,
PyCapsule_IsValid
)

from libc.string cimport strcmp
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be removed now?


import atexit
from collections.abc import Mapping
Expand Down Expand Up @@ -105,6 +113,7 @@ cdef void* _as_c_pointer(v, allow_null=False) except *:
(the latter for compatibility with raw pointers exported by reticulate)
"""
cdef void* c_ptr
cdef const char* capsule_name
if isinstance(v, int):
c_ptr = <void*> <uintptr_t > v
elif isinstance(v, float):
Expand All @@ -114,7 +123,14 @@ cdef void* _as_c_pointer(v, allow_null=False) except *:
"Arrow library", UserWarning, stacklevel=2)
c_ptr = <void*> <uintptr_t > v
elif PyCapsule_CheckExact(v):
c_ptr = PyCapsule_GetPointer(v, NULL)
capsule_name = PyCapsule_GetName(v)
if capsule_name == NULL or strcmp(capsule_name, "r_extptr") == 0:
paleolimbot marked this conversation as resolved.
Show resolved Hide resolved
c_ptr = PyCapsule_GetPointer(v, capsule_name)
else:
capsule_name_str = capsule_name.decode("UTF-8")
raise ValueError(
f"Can't convert PyCapsule with name '{capsule_name_str}' to pointer address"
)
paleolimbot marked this conversation as resolved.
Show resolved Hide resolved
else:
raise TypeError(f"Expected a pointer value, got {type(v)!r}")
if not allow_null and c_ptr == NULL:
Expand Down
Loading