Merge pull request #619 from skinny121/class_property

Fix __class__
This commit is contained in:
Adam
2019-03-08 06:47:55 +00:00
committed by GitHub
5 changed files with 84 additions and 5 deletions

View File

@@ -27,6 +27,18 @@ pub fn init(context: &PyContext) {
context.new_rustfunc(member_get),
);
let data_descriptor_type = &context.data_descriptor_type;
context.set_attr(
&data_descriptor_type,
"__get__",
context.new_rustfunc(data_get),
);
context.set_attr(
&data_descriptor_type,
"__set__",
context.new_rustfunc(data_set),
);
let classmethod_type = &context.classmethod_type;
context.set_attr(
&classmethod_type,
@@ -83,6 +95,26 @@ fn member_get(vm: &mut VirtualMachine, mut args: PyFuncArgs) -> PyResult {
}
}
fn data_get(vm: &mut VirtualMachine, mut args: PyFuncArgs) -> PyResult {
match args.shift().get_attr("fget") {
Some(function) => vm.invoke(function, args),
None => {
let attribute_error = vm.context().exceptions.attribute_error.clone();
Err(vm.new_exception(attribute_error, String::from("Attribute Error")))
}
}
}
fn data_set(vm: &mut VirtualMachine, mut args: PyFuncArgs) -> PyResult {
match args.shift().get_attr("fset") {
Some(function) => vm.invoke(function, args),
None => {
let attribute_error = vm.context().exceptions.attribute_error.clone();
Err(vm.new_exception(attribute_error, String::from("Attribute Error")))
}
}
}
// Classmethod type methods:
fn classmethod_get(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
trace!("classmethod.__get__ {:?}", args.args);

View File

@@ -161,6 +161,11 @@ pub fn init(context: &PyContext) {
context.set_attr(&object, "__new__", context.new_rustfunc(new_instance));
context.set_attr(&object, "__init__", context.new_rustfunc(object_init));
context.set_attr(
&object,
"__class__",
context.new_data_descriptor(object_class, object_class_setter),
);
context.set_attr(&object, "__eq__", context.new_rustfunc(object_eq));
context.set_attr(&object, "__ne__", context.new_rustfunc(object_ne));
context.set_attr(&object, "__lt__", context.new_rustfunc(object_lt));
@@ -190,6 +195,20 @@ fn object_init(vm: &mut VirtualMachine, _args: PyFuncArgs) -> PyResult {
Ok(vm.ctx.none())
}
// TODO Use PyClassRef for owner to enforce type
fn object_class(_obj: PyObjectRef, owner: PyObjectRef, _vm: &mut VirtualMachine) -> PyObjectRef {
owner
}
fn object_class_setter(
instance: PyObjectRef,
_value: PyObjectRef,
vm: &mut VirtualMachine,
) -> PyResult {
let type_repr = vm.to_pystr(&instance.typ())?;
Err(vm.new_type_error(format!("can't change class of type '{}'", type_repr)))
}
fn object_dict(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
match args.args[0].payload {
PyObjectPayload::Class { ref dict, .. } | PyObjectPayload::Instance { ref dict, .. } => {

View File

@@ -39,11 +39,6 @@ pub fn init(context: &PyContext) {
"__mro__",
context.new_member_descriptor(type_mro),
);
context.set_attr(
&type_type,
"__class__",
context.new_member_descriptor(type_new),
);
context.set_attr(&type_type, "__repr__", context.new_rustfunc(type_repr));
context.set_attr(
&type_type,