diff --git a/src/type_object.rs b/src/type_object.rs index d0693df9..35f6d05c 100644 --- a/src/type_object.rs +++ b/src/type_object.rs @@ -105,7 +105,15 @@ impl LazyStaticType { } pub fn get_or_init(&self, py: Python<'_>) -> *mut ffi::PyTypeObject { - let type_object = *self.value.get_or_init(py, || create_type_object::(py)); + fn inner() -> *mut ffi::PyTypeObject { + // Safety: `py` is held by the caller of `get_or_init`. + let py = unsafe { Python::assume_gil_acquired() }; + create_type_object::(py) + } + + // Uses explicit GILOnceCell::get_or_init:: *mut ffi::PyTypeObject> monomorphization + // so that only this one monomorphization is instantiated (instead of one closure monormization for each T). + let type_object = *self.value.get_or_init:: *mut ffi::PyTypeObject>(py, inner::); self.ensure_init(py, type_object, T::NAME, &T::for_all_items); type_object }