Skip to content

docs: update safety docs for PyCapsule::import#6064

Open
Person-93 wants to merge 2 commits into
PyO3:mainfrom
Person-93:safety_docs_pycapsule
Open

docs: update safety docs for PyCapsule::import#6064
Person-93 wants to merge 2 commits into
PyO3:mainfrom
Person-93:safety_docs_pycapsule

Conversation

@Person-93
Copy link
Copy Markdown
Contributor

No description provided.

@Person-93 Person-93 changed the title update safety docs for PyCapsule::import docs: update safety docs for PyCapsule::import May 22, 2026
Comment thread src/types/capsule.rs
Comment on lines +389 to 391
/// - 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> {
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.

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.

2 participants