mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
sys._clear_type_descriptors (#6795)
This commit is contained in:
@@ -1215,6 +1215,32 @@ mod sys {
|
||||
crate::vm::thread::COROUTINE_ORIGIN_TRACKING_DEPTH.get() as i32
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn _clear_type_descriptors(type_obj: PyTypeRef, vm: &VirtualMachine) -> PyResult<()> {
|
||||
use crate::types::PyTypeFlags;
|
||||
|
||||
// Check if type is immutable
|
||||
if type_obj.slots.flags.has_feature(PyTypeFlags::IMMUTABLETYPE) {
|
||||
return Err(vm.new_type_error("argument is immutable".to_owned()));
|
||||
}
|
||||
|
||||
let mut attributes = type_obj.attributes.write();
|
||||
|
||||
// Remove __dict__ descriptor if present
|
||||
attributes.swap_remove(identifier!(vm, __dict__));
|
||||
|
||||
// Remove __weakref__ descriptor if present
|
||||
attributes.swap_remove(identifier!(vm, __weakref__));
|
||||
|
||||
drop(attributes);
|
||||
|
||||
// Update slots to notify subclasses and recalculate cached values
|
||||
type_obj.update_slot::<true>(identifier!(vm, __dict__), &vm.ctx);
|
||||
type_obj.update_slot::<true>(identifier!(vm, __weakref__), &vm.ctx);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn getswitchinterval(vm: &VirtualMachine) -> f64 {
|
||||
// Return the stored switch interval
|
||||
|
||||
@@ -234,6 +234,7 @@ declare_const_name! {
|
||||
__typing_is_unpacked_typevartuple__,
|
||||
__typing_prepare_subst__,
|
||||
__typing_unpacked_tuple_args__,
|
||||
__weakref__,
|
||||
__xor__,
|
||||
|
||||
// common names
|
||||
|
||||
Reference in New Issue
Block a user