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

feat(python): Support the PyCapsule protocol #318

Merged
merged 3 commits into from
Nov 17, 2023

Conversation

jorisvandenbossche
Copy link
Member

First commit with just importing for now. Will do exporting next.

@jorisvandenbossche jorisvandenbossche changed the title Support the PyCapsule protocol feat(python): Support the PyCapsule protocol Nov 16, 2023
@codecov-commenter
Copy link

codecov-commenter commented Nov 16, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (032cdd9) 87.01% compared to head (2f2e106) 85.29%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #318      +/-   ##
==========================================
- Coverage   87.01%   85.29%   -1.73%     
==========================================
  Files          70        3      -67     
  Lines       10574      374   -10200     
==========================================
- Hits         9201      319    -8882     
+ Misses       1373       55    -1318     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@paleolimbot paleolimbot left a comment

Choose a reason for hiding this comment

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

Thank you for working on this! I think you probably want to use the Schema, Array, and ArrayStream constructors with the capsule as the base instead of "moving" into the "holder" helpers. We might be able to get rid of the holders entirely now that the capsules are defined (once there's code for allocating them).

if hasattr(obj, "__arrow_c_schema__"):
return Schema._import_from_c_capsule(obj.__arrow_c_schema__())

# for pyarrow < 14.0
Copy link
Member

Choose a reason for hiding this comment

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

At some point we should maybe remove this logic (or do stricter check...maybe using type().__name__ or something). A name-based check would support older pyarrow, anyway...I think it would be reasonable for any other package that were using _export_to_c() to update and use the new dunder method to be compatible here.

return self.schema.__arrow_c_schema__()


class ArrayWrapper:
Copy link
Member

Choose a reason for hiding this comment

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

Array(Stream)Wrapper should also implement __arrow_c_schema__, right? Even if not used here, it might be good for future readers who end up here to see the example.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, "Will do exporting next."


c_schema = <ArrowSchema*> PyCapsule_GetPointer(schema_capsule, 'arrow_schema')

out = Schema.allocate()
Copy link
Member

@paleolimbot paleolimbot Nov 16, 2023

Choose a reason for hiding this comment

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

I wonder if what you want here is more like:

cdef Schema schema = Schema(schema_capsule, <uintptr_t>PyCapsule_GetPointer(schema_capsule, 'arrow_schema'))

(i.e., no need to "move" here...base can be a capsule, which does basically the same thing as the SchemaHolder)

@jorisvandenbossche
Copy link
Member Author

I think you probably want to use the Schema, Array, and ArrayStream constructors with the capsule as the base instead of "moving" into the "holder" helpers.

That's fine for me as well, no strong opinion here. I actually started that way, but thought that moving the struct as consumer so we own the memory would be considered good practice (nanoarrow is of course a bit of a peculiar consumer). But actually for nested ones that still only moves the top-level one, so it's not that this guarantees anything ..

@jorisvandenbossche
Copy link
Member Author

Indeed, the PyCapsule also ensures proper deallocation like the Holder class, so will try a variant with keeping the capsule around as the base.

@paleolimbot
Copy link
Member

Moving the struct is definitely good practice, but the Capsule interface forces that on the producer (whereas with the _export_to_c() there was no guarantee of cleanup so moving was sort of required).

@jorisvandenbossche
Copy link
Member Author

For exporting (the dunder methods), do we want to move in that case? I think right now we don't really expose the functionality to create a Schema/Array .. object from "scratch", owning the memory, but in theory nanoarrow C supports that, so we could eventually have that. For such a case, we should probably create a new capsule with a moved struct?

Or even in general, if you want that a nanoarrow.Array can be consumed multiple times, we need to export a new struct, otherwise the one of the consumers could already mark it as released.

@paleolimbot
Copy link
Member

For exporting (the dunder methods), do we want to move in that case?

That is a very complicated question, although I think the renaming of classes will help here. For the current Array (probably to be renamed CArray or UnsafeArray to properly communicate the low-level/only-use-if-you-know-what-you-are-doingness of it), I think you can implement __arrow_c_XXX__ and move to a capsule. For the current ArrayView (probably future Array), you will want to make a shallow copy. Shallow copying isn't implemented here and is complicated (I do it in R)...I would punt and not do that right now.

For Schema, you can use ArrowSchemaDeepCopy(), which is probably a safer default than a move.

Streams I think you can just move...they are already stateful and so I think there is less opportunity for somebody to misuse them in that way.

Copy link
Member

@paleolimbot paleolimbot left a comment

Choose a reason for hiding this comment

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

Just a few details!

from cpython cimport Py_buffer
from nanoarrow_c cimport *
from libc.string cimport memcpy
Copy link
Member

Choose a reason for hiding this comment

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

I think this can be removed now?

@@ -428,6 +447,33 @@ cdef class Array:
self._ptr = <ArrowArray*>addr
self._schema = schema

@staticmethod
def _import_from_c_capsule(schema_capsule, array_capsule):
Copy link
Member

Choose a reason for hiding this comment

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

I think the ordering everywhere else is array, schema?

@paleolimbot paleolimbot merged commit 2d28306 into apache:main Nov 17, 2023
@jorisvandenbossche jorisvandenbossche deleted the capsules branch November 22, 2023 15:26
jorisvandenbossche added a commit that referenced this pull request Nov 24, 2023
Follow-up on #318

Exports the different objects using different mechanism:

- ArrowSchema -> deep copy
- ArrowArray -> shallow copy of the struct, update private_data/release
to ref count the original's base
- ArrowArrayStream -> move the struct (turning the source
nanoarrow.ArrayStream object as released, but that's fine)
@paleolimbot paleolimbot added this to the nanoarrow 0.4.0 milestone Jan 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants