Skip to content

Cleaner type_type declaration, and faster instance checking. #658

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

Merged
merged 3 commits into from
Mar 11, 2019
Merged
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
49 changes: 16 additions & 33 deletions vm/src/obj/objtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,39 +41,23 @@ pub fn create_type(type_type: PyObjectRef, object_type: PyObjectRef, _dict_type:
}
}

pub fn init(context: &PyContext) {
let type_type = &context.type_type;

pub fn init(ctx: &PyContext) {
let type_doc = "type(object_or_name, bases, dict)\n\
type(object) -> the object's type\n\
type(name, bases, dict) -> a new type";

context.set_attr(&type_type, "__call__", context.new_rustfunc(type_call));
context.set_attr(&type_type, "__new__", context.new_rustfunc(type_new));
context.set_attr(&type_type, "__mro__", context.new_property(type_mro));
context.set_attr(&type_type, "__repr__", context.new_rustfunc(type_repr));
context.set_attr(
&type_type,
"__prepare__",
context.new_rustfunc(type_prepare),
);
context.set_attr(
&type_type,
"__getattribute__",
context.new_rustfunc(type_getattribute),
);
context.set_attr(
&type_type,
"__instancecheck__",
context.new_rustfunc(type_instance_check),
);
context.set_attr(
&type_type,
"__subclasscheck__",
context.new_rustfunc(type_subclass_check),
);
context.set_attr(&type_type, "__doc__", context.new_str(type_doc.to_string()));
context.set_attr(&type_type, "__dir__", context.new_rustfunc(type_dir));
extend_class!(&ctx, &ctx.type_type, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handy stuff!

"__call__" => ctx.new_rustfunc(type_call),
"__new__" => ctx.new_rustfunc(type_new),
"__mro__" => ctx.new_property(type_mro),
"__repr__" => ctx.new_rustfunc(type_repr),
"__prepare__" => ctx.new_rustfunc(type_prepare),
"__getattribute__" => ctx.new_rustfunc(type_getattribute),
"__instancecheck__" => ctx.new_rustfunc(type_instance_check),
"__subclasscheck__" => ctx.new_rustfunc(type_subclass_check),
"__doc__" => ctx.new_str(type_doc.to_string()),
"__dir__" => ctx.new_rustfunc(type_dir),
});
}

fn type_mro(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
Expand All @@ -97,8 +81,7 @@ fn _mro(cls: PyObjectRef) -> Option<Vec<PyObjectRef>> {
/// Determines if `obj` actually an instance of `cls`, this doesn't call __instancecheck__, so only
/// use this if `cls` is known to have not overridden the base __instancecheck__ magic method.
pub fn isinstance(obj: &PyObjectRef, cls: &PyObjectRef) -> bool {
let mro = _mro(obj.typ()).unwrap();
mro.into_iter().any(|c| c.is(&cls))
issubclass(obj.type_ref(), &cls)
}

fn type_instance_check(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
Expand All @@ -114,8 +97,8 @@ fn type_instance_check(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
/// so only use this if `cls` is known to have not overridden the base __subclasscheck__ magic
/// method.
pub fn issubclass(subclass: &PyObjectRef, cls: &PyObjectRef) -> bool {
let mro = _mro(subclass.clone()).unwrap();
mro.into_iter().any(|c| c.is(&cls))
let ref mro = subclass.payload::<PyClass>().unwrap().mro;
subclass.is(&cls) || mro.iter().any(|c| c.is(&cls))
}

fn type_subclass_check(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
Expand Down
13 changes: 8 additions & 5 deletions vm/src/pyobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -761,19 +761,22 @@ pub trait FromPyObjectRef {
}

pub trait TypeProtocol {
fn typ(&self) -> PyObjectRef;
fn typ(&self) -> PyObjectRef {
self.type_ref().clone()
}
fn type_ref(&self) -> &PyObjectRef;
}

impl TypeProtocol for PyObjectRef {
fn typ(&self) -> PyObjectRef {
(**self).typ()
fn type_ref(&self) -> &PyObjectRef {
(**self).type_ref()
}
}

impl TypeProtocol for PyObject {
fn typ(&self) -> PyObjectRef {
fn type_ref(&self) -> &PyObjectRef {
match self.typ {
Some(ref typ) => typ.clone(),
Some(ref typ) => &typ,
None => panic!("Object {:?} doesn't have a type!", self),
}
}
Expand Down