Skip to content

Another trial of #4455 #4516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 0 additions & 2 deletions Lib/test/test_bool.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

class BoolTest(unittest.TestCase):

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_subclass(self):
try:
class C(bool):
Expand Down
33 changes: 32 additions & 1 deletion vm/src/builtins/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ impl Py<PyType> {
/// Determines if `subclass` is actually a subclass of `cls`, this doesn't call __subclasscheck__,
/// so only use this if `cls` is known to have not overridden the base __subclasscheck__ magic
/// method.
/// Similar to CPython PyType_IsSubtype
pub fn fast_issubclass(&self, cls: &impl Borrow<crate::PyObject>) -> bool {
self.as_object().is(cls.borrow()) || self.mro.iter().any(|c| c.is(cls.borrow()))
}
Expand All @@ -383,7 +384,36 @@ impl PyType {
subtype = subtype.name(),
)));
}
call_slot_new(zelf, subtype, args, vm)

let typ = zelf;
// Check that the use doesn't do something silly and unsafe like
// object.__new__(dict). To do this, we check that the
// most derived base that's not a heap type is this type. */
let mut static_base = subtype.clone();
loop {
if static_base
.slots
.new
.load()
.map_or(false, |f| f as usize == crate::types::new_wrapper as usize)
{
static_base = static_base.base().unwrap();
} else {
break;
}
}
if static_base.slots.new.load().map_or(0, |f| f as usize)
!= typ.slots.new.load().map_or(0, |f| f as usize)
{
return Err(vm.new_type_error(format!(
"{}.__new__({}) is not safe, use {}.__new__()",
typ.name(),
subtype.name(),
static_base.name()
)));
}

call_slot_new(typ, subtype, args, vm)
}

#[pygetset(name = "__mro__")]
Expand Down Expand Up @@ -1114,6 +1144,7 @@ pub(crate) fn init(ctx: &Context) {
PyType::extend_class(ctx, ctx.types.type_type);
}

// part of tp_new_wrapper *after* subtype check
pub(crate) fn call_slot_new(
typ: PyTypeRef,
subtype: PyTypeRef,
Expand Down
3 changes: 2 additions & 1 deletion vm/src/types/slot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,8 @@ fn init_wrapper(obj: PyObjectRef, args: FuncArgs, vm: &VirtualMachine) -> PyResu
Ok(())
}

fn new_wrapper(cls: PyTypeRef, mut args: FuncArgs, vm: &VirtualMachine) -> PyResult {
// = slot_tp_new
pub(crate) fn new_wrapper(cls: PyTypeRef, mut args: FuncArgs, vm: &VirtualMachine) -> PyResult {
let new = cls.get_attr(identifier!(vm, __new__)).unwrap();
args.prepend_arg(cls.into());
vm.invoke(&new, args)
Expand Down