Skip to content

improve PyCapsule constructors#5881

Open
Icxolu wants to merge 1 commit intoPyO3:mainfrom
Icxolu:fix/5692
Open

improve PyCapsule constructors#5881
Icxolu wants to merge 1 commit intoPyO3:mainfrom
Icxolu:fix/5692

Conversation

@Icxolu
Copy link
Member

@Icxolu Icxolu commented Mar 15, 2026

Closes #5692

Copy link
Contributor

@Tpt Tpt left a comment

Choose a reason for hiding this comment

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

I am not an expert on Capsules, so an other approval might be nice

/// Constructs a new capsule whose contents are `value`, associated with `name`.
///
/// Also provides a destructor: when the `PyCapsule` is destroyed, it will be passed the original object,
/// as well as a `*mut c_void` which will point to the capsule's context, if any.
Copy link
Contributor

@Tpt Tpt Mar 16, 2026

Choose a reason for hiding this comment

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

sorry for not spotting it: this method does not allow to set any initial context to the capsule, is it worth it to add this parameter to this method or should we drop it from this method and add a new new_with_value_and_context method that would take for input an initial context and allow the destructor to destruct the context?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think we could do much more than construct the capsule and call set_context afterwards. As this can already be done, I don't think we need to add another argument here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Make sense! But we want to make all other users of new_with_value to care about the possibility of having a context even if it's most often not the case (I am just nitpicking here, feel free to just ignore my reply)

Copy link
Member Author

Choose a reason for hiding this comment

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

I can see the point (always happy to discuss 😄). This is only for new_with_value_and_destructor however and I think we need to give the context in the destructor, so that users have a way to free it if they used it. (If you need a destructor you'd probably have context anyway and if not it's easy to ignore)

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 I'd prefer have context: Option<*mut c_void> as an argument to these functions rather than new flavours of the method, however I also agree that probably just calling set_context after is probably good enough?

Also context is somewhat less useful here than for C callers, because we are already storing additional context in the destructor closure.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'd propose we leave it as is then.

Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

Thanks for moving this forward!

/// Constructs a new capsule whose contents are `value`, associated with `name`.
///
/// Also provides a destructor: when the `PyCapsule` is destroyed, it will be passed the original object,
/// as well as a `*mut c_void` which will point to the capsule's context, if any.
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 I'd prefer have context: Option<*mut c_void> as an argument to these functions rather than new flavours of the method, however I also agree that probably just calling set_context after is probably good enough?

Also context is somewhat less useful here than for C callers, because we are already storing additional context in the destructor closure.

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.

Improved closure constructors

3 participants