Skip to content

Commit d7a885c

Browse files
authored
sys._clear_type_descriptors (#6795)
1 parent 6a064aa commit d7a885c

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

crates/vm/src/stdlib/sys.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,6 +1215,32 @@ mod sys {
12151215
crate::vm::thread::COROUTINE_ORIGIN_TRACKING_DEPTH.get() as i32
12161216
}
12171217

1218+
#[pyfunction]
1219+
fn _clear_type_descriptors(type_obj: PyTypeRef, vm: &VirtualMachine) -> PyResult<()> {
1220+
use crate::types::PyTypeFlags;
1221+
1222+
// Check if type is immutable
1223+
if type_obj.slots.flags.has_feature(PyTypeFlags::IMMUTABLETYPE) {
1224+
return Err(vm.new_type_error("argument is immutable".to_owned()));
1225+
}
1226+
1227+
let mut attributes = type_obj.attributes.write();
1228+
1229+
// Remove __dict__ descriptor if present
1230+
attributes.swap_remove(identifier!(vm, __dict__));
1231+
1232+
// Remove __weakref__ descriptor if present
1233+
attributes.swap_remove(identifier!(vm, __weakref__));
1234+
1235+
drop(attributes);
1236+
1237+
// Update slots to notify subclasses and recalculate cached values
1238+
type_obj.update_slot::<true>(identifier!(vm, __dict__), &vm.ctx);
1239+
type_obj.update_slot::<true>(identifier!(vm, __weakref__), &vm.ctx);
1240+
1241+
Ok(())
1242+
}
1243+
12181244
#[pyfunction]
12191245
fn getswitchinterval(vm: &VirtualMachine) -> f64 {
12201246
// Return the stored switch interval

crates/vm/src/vm/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ declare_const_name! {
234234
__typing_is_unpacked_typevartuple__,
235235
__typing_prepare_subst__,
236236
__typing_unpacked_tuple_args__,
237+
__weakref__,
237238
__xor__,
238239

239240
// common names

0 commit comments

Comments
 (0)