You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Python modules rely on a static definition of a module. To avoid needing to have a static, threadsafe PyObject, I propose we use PyModExport, which is the new way of defining modules, and does not rely on the safety of a static, threadsafe PyObject.
An example of a module definition, taken from the above PEP:
PyMODEXPORT_FUNC is equivalent to *mut PyModuleDef_Slot.
One design I have in mind is something along the lines of:
pubenumPyModuleDefSlot{Name(&'staticCStr),Doc(&'staticCStr),MultipleInterpreters(*mutc_void),GIL(*mutc_void),StateSize(usize),// ... etc.}pubstructPyModuleDef<constN:usize>(UnsafeCell<[ffi::PyModuleDef_Slot;N]>);impl<constN:usize>PyModuleDef<N>{pubconstfnnew(slots:[PyModuleDefSlot;N]) -> Self{todo!()}pubfnas_mut_ptr(&'staticself) -> *mutPyModuleDef_Slot{self.0.get()as*mutPyModuleDef_Slot}}unsafeimpl<constN:usize>SyncforPyModuleDef<N>{}staticMODULE:PyModuleDef<5> = PyModule::new([PyModuleDefSlot::Name(c"mymodule"),PyModuleDefSlot::Doc(c"An example module for a proof of concept"),// etc.]);#[unsafe(no_mangle)]unsafeextern"C"fnPyModExport_mymodule() -> *mutPyModuleDef_Slot{MODULE.as_mut_ptr()}
I think this isn't currently sound because we probably need a thread state-aware lock to prevent races on MODULE.as_mut_ptr()?
We will likely also want a design for specifying methods that are part of the module, which are listed in a static array similar to the PyModExport static.
I think this isn't currently sound because we probably need a thread state-aware lock to prevent races on MODULE.as_mut_ptr()?
Given the PyModExport API is brand new, can we ask it to export as *const PyModuleDef_Slot instead? Immutable static data is generally much nicer and it might be the case that it's mutable only because that's less work in C.
Unless someone beats me to it, I’ll be implementing PEP 793 support in PyO3 sometime early next year to enable support for the new stable ABI. PyO3/pyo3#5644
Activity
davidhewitt commentedon Dec 12, 2025
Given the
PyModExportAPI is brand new, can we ask it to export as*const PyModuleDef_Slotinstead? Immutable static data is generally much nicer and it might be the case that it's mutable only because that's less work in C.cc @encukou
encukou commentedon Dec 12, 2025
Per the docs, “This array must remain valid and constant until interpreter shutdown.”
Can you treat it as immutable?
davidhewitt commentedon Dec 12, 2025
We can pretend and cast the constness away, I guess. Is there a reason you didn't define the API to return
const PyModuleDef_Slot *?encukou commentedon Dec 12, 2025
In C,
constis such a weak annotation that I don't really think about it...ngoldbaum commentedon Dec 12, 2025
Unless someone beats me to it, I’ll be implementing PEP 793 support in PyO3 sometime early next year to enable support for the new stable ABI. PyO3/pyo3#5644
emmatyping commentedon Dec 12, 2025
Yeah, I think this is the best solution.
Since the contents will be immutable/constant, we also don't need a lock to be sound.