Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 36b6755

Browse files
garychiayouknowone
authored andcommitted
Raise TypeError when calling __new__ unsafely
1 parent 861055f commit 36b6755

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

vm/src/builtins/type.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,6 +1308,24 @@ pub(crate) fn call_slot_new(
13081308
args: FuncArgs,
13091309
vm: &VirtualMachine,
13101310
) -> PyResult {
1311+
let mut staticbase = Some(subtype.clone());
1312+
while let Some(ref basetype) = staticbase {
1313+
if (basetype.flags() & PyTypeFlags::HEAPTYPE.bits()) != 0 {
1314+
staticbase = basetype.base().clone();
1315+
} else {
1316+
break;
1317+
}
1318+
}
1319+
if let Some(ref basetype) = staticbase {
1320+
if !typ.fast_issubclass(basetype) {
1321+
return Err(vm.new_type_error(format!(
1322+
"{}.__new__({}) is not safe, use {}.__new__()",
1323+
typ.name(),
1324+
subtype.name(),
1325+
basetype.name()
1326+
)));
1327+
}
1328+
}
13111329
let slot_new = typ
13121330
.deref()
13131331
.mro_find_map(|cls| cls.slots.new.load())

0 commit comments

Comments
 (0)