Skip to content
Open
Changes from all 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
4 changes: 3 additions & 1 deletion src/types/capsule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,9 @@ impl PyCapsule {
///
/// # Safety
///
/// It must be known that the capsule imported by `name` contains an item of type `T`.
/// - It must be known that the capsule imported by `name` contains an item of type `T`.
/// - The contents of the capsule must not be mutated by any third-party code while the reference is alive.
/// The python interpreter does _NOT_ provide any synchronization guarantees for capsules.
pub unsafe fn import<'py, T>(py: Python<'py>, name: &CStr) -> PyResult<&'py T> {
Comment on lines +389 to 391
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I wonder, is it potentially worse than this? I imagine that if arbitrary Python code runs while the reference is alive, it can presumably free the capsule and leave the reference dangling.

There are presumably also alignment concerns, which the user cannot know.

Maybe there's enough spice here that it would be worth deprecating this and encouraging users to move over to #6066?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Similarly we made the decision to remove .reference() in #5229 (comment)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Maybe there's enough spice here that it would be worth deprecating this and encouraging users to move over to #6066?

I think that's a good idea. In the meantime, maybe we can check if the pointer is aligned and either panic or return an error if it isn't.

// SAFETY: `name` is a valid C string, thread is attached to the Python interpreter
let ptr = unsafe { ffi::PyCapsule_Import(name.as_ptr(), false as c_int) };
Expand Down
Loading