Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Raise TypeError when calling __new__ unsafely
  • Loading branch information
garychia authored and youknowone committed Apr 7, 2025
commit 36b6755ba891ca34ebf550e41c27f67b97d32c2c
18 changes: 18 additions & 0 deletions vm/src/builtins/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,24 @@ pub(crate) fn call_slot_new(
args: FuncArgs,
vm: &VirtualMachine,
) -> PyResult {
let mut staticbase = Some(subtype.clone());
while let Some(ref basetype) = staticbase {
if (basetype.flags() & PyTypeFlags::HEAPTYPE.bits()) != 0 {
staticbase = basetype.base().clone();
} else {
break;
}
}
if let Some(ref basetype) = staticbase {
if !typ.fast_issubclass(basetype) {
return Err(vm.new_type_error(format!(
"{}.__new__({}) is not safe, use {}.__new__()",
typ.name(),
subtype.name(),
basetype.name()
)));
}
}
let slot_new = typ
.deref()
.mro_find_map(|cls| cls.slots.new.load())
Expand Down
Loading