Skip to content

internal: refactor get_slot and add safety comments - #6647

Open
Person-93 wants to merge 1 commit into
PyO3:mainfrom
Person-93:safety-comments
Open

Person-93 wants to merge 1 commit into
PyO3:mainfrom
Person-93:safety-comments

Conversation

@Person-93

Copy link
Copy Markdown
Contributor

Part of #5487

Minor refactoring of get_slot.rs to remove some safety requirements.

  • get_slot function takes a Python
  • conditional param is_runtime_3_10 is removed, the one place it was checked is replaced by a call to the is_runtime_3_10 function

@Person-93 Person-93 changed the title refactor get_slot and add safety comments internal: refactor get_slot and add safety comments Dec 1, 2026

@davidhewitt davidhewitt left a comment

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.

Thanks, I like the idea, some thoughts in comments.

Comment thread src/internal/get_slot.rs
// SAFETY: we are holding a Python marker and caller guarantees that `ty`
// is valid
let is_heap_type = unsafe {ffi::PyType_HasFeature(ty, ffi::Py_TPFLAGS_HEAPTYPE)} == 0;
if !is_runtime_3_10(_py) && is_heap_type

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 think we should probably change to a form of Python::version_info which doesn't have a receiver. We'll need to create it.

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.

See #6652

Comment thread src/impl_/pymethods.rs
let mut traverse: Option<ffi::traverseproc>;

// SAFETY: this `py` is only used to call `get_slot` which has no side effects
let py = unsafe { Python::assume_attached() };

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 would strongly prefer we avoided creating Python inside traverse as it's wildly unsafe. I think my other idea might give us a route to doing so...

Comment thread src/internal/get_slot.rs
unsafe fn get_slot(
self,
_py: Python<'_>,
ty: *mut ffi::PyTypeObject,

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.

If we made this &Py<PyType> we might be able to contain the unsafety to a much lower level.

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.

How should the callers convert *mut ffi::PyTypeObject to &Py<PyType>?

I think it's sounds to transmute &*mut ffi::PyTypeObject to Option<&Py<PyType>>, but I don't think it's a great idea to have transmutes all over the place. Should it be a method on Py<PyType>?

This branch has not been deployed

No deployments
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