From d7a885cea82b39be21b0f847e83cb78e117ff6ca Mon Sep 17 00:00:00 2001 From: "Jeong, YunWon" <69878+youknowone@users.noreply.github.com> Date: Mon, 19 Jan 2026 16:43:20 +0900 Subject: [PATCH] sys._clear_type_descriptors (#6795) --- crates/vm/src/stdlib/sys.rs | 26 ++++++++++++++++++++++++++ crates/vm/src/vm/context.rs | 1 + 2 files changed, 27 insertions(+) diff --git a/crates/vm/src/stdlib/sys.rs b/crates/vm/src/stdlib/sys.rs index 305e1cfa6..6c8a4e406 100644 --- a/crates/vm/src/stdlib/sys.rs +++ b/crates/vm/src/stdlib/sys.rs @@ -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::(identifier!(vm, __dict__), &vm.ctx); + type_obj.update_slot::(identifier!(vm, __weakref__), &vm.ctx); + + Ok(()) + } + #[pyfunction] fn getswitchinterval(vm: &VirtualMachine) -> f64 { // Return the stored switch interval diff --git a/crates/vm/src/vm/context.rs b/crates/vm/src/vm/context.rs index 9978554bc..f4368e91a 100644 --- a/crates/vm/src/vm/context.rs +++ b/crates/vm/src/vm/context.rs @@ -234,6 +234,7 @@ declare_const_name! { __typing_is_unpacked_typevartuple__, __typing_prepare_subst__, __typing_unpacked_tuple_args__, + __weakref__, __xor__, // common names