Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
20 changes: 20 additions & 0 deletions vm/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::marker::PhantomData;
use std::ops::Deref;

use crate::obj::objtype;
use crate::obj::objtype::PyClassRef;
use crate::pyobject::{
IntoPyObject, PyContext, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef, PyResult,
TryFromObject, TypeProtocol,
Expand Down Expand Up @@ -44,6 +45,25 @@ where
_payload: PhantomData,
}
}

pub fn new_with_type(vm: &mut VirtualMachine, payload: T, cls: PyClassRef) -> PyResult<Self> {
let required_type = T::required_type(&vm.ctx);
if objtype::issubclass(&cls.obj, &required_type) {
Ok(PyRef {
obj: PyObject::new(
PyObjectPayload::AnyRustValue {
value: Box::new(payload),
},
cls.obj,
),
_payload: PhantomData,
})
} else {
let subtype = vm.to_pystr(&cls.obj)?;
let basetype = vm.to_pystr(&required_type)?;
Err(vm.new_type_error(format!("{} is not a subtype of {}", subtype, basetype)))
}
}
}

impl<T> Deref for PyRef<T>
Expand Down
11 changes: 2 additions & 9 deletions vm/src/obj/objcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn init(context: &PyContext) {
("co_kwonlyargcount", code_co_kwonlyargcount),
("co_name", code_co_name),
] {
context.set_attr(code_type, name, context.new_member_descriptor(f))
context.set_attr(code_type, name, context.new_property(f))
}
}

Expand Down Expand Up @@ -79,14 +79,7 @@ fn code_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
}

fn member_code_obj(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult<bytecode::CodeObject> {
arg_check!(
vm,
args,
required = [
(zelf, Some(vm.ctx.code_type())),
(_cls, Some(vm.ctx.type_type()))
]
);
arg_check!(vm, args, required = [(zelf, Some(vm.ctx.code_type()))]);
Ok(get_value(zelf))
}

Expand Down
51 changes: 1 addition & 50 deletions vm/src/obj/objfunction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub fn init(context: &PyContext) {
context.set_attr(
&function_type,
"__code__",
context.new_member_descriptor(function_code),
context.new_property(function_code),
);

let builtin_function_or_method_type = &context.builtin_function_or_method_type;
Expand All @@ -20,25 +20,6 @@ pub fn init(context: &PyContext) {
context.new_rustfunc(bind_method),
);

let member_descriptor_type = &context.member_descriptor_type;
context.set_attr(
&member_descriptor_type,
"__get__",
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,
Expand Down Expand Up @@ -85,36 +66,6 @@ fn function_code(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
}
}

fn member_get(vm: &mut VirtualMachine, mut args: PyFuncArgs) -> PyResult {
match args.shift().get_attr("function") {
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_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);
Expand Down
17 changes: 8 additions & 9 deletions vm/src/obj/objobject.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::objstr;
use super::objtype;
use crate::function::PyRef;
use crate::obj::objproperty::PropertyBuilder;
use crate::pyobject::{
AttributeProtocol, DictProtocol, IdProtocol, PyAttributes, PyContext, PyFuncArgs, PyObject,
PyObjectPayload, PyObjectRef, PyResult, TypeProtocol,
Expand Down Expand Up @@ -171,7 +172,10 @@ pub fn init(context: &PyContext) {
context.set_attr(
&object,
"__class__",
context.new_data_descriptor(object_class, object_class_setter),
PropertyBuilder::new(context)
.add_getter(object_class)
.add_setter(object_class_setter)
.create(),
);
context.set_attr(&object, "__eq__", context.new_rustfunc(object_eq));
context.set_attr(&object, "__ne__", context.new_rustfunc(object_ne));
Expand All @@ -180,11 +184,7 @@ pub fn init(context: &PyContext) {
context.set_attr(&object, "__gt__", context.new_rustfunc(object_gt));
context.set_attr(&object, "__ge__", context.new_rustfunc(object_ge));
context.set_attr(&object, "__delattr__", context.new_rustfunc(object_delattr));
context.set_attr(
&object,
"__dict__",
context.new_member_descriptor(object_dict),
);
context.set_attr(&object, "__dict__", context.new_property(object_dict));
context.set_attr(&object, "__dir__", context.new_rustfunc(object_dir));
context.set_attr(&object, "__hash__", context.new_rustfunc(object_hash));
context.set_attr(&object, "__str__", context.new_rustfunc(object_str));
Expand All @@ -202,9 +202,8 @@ 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(obj: PyObjectRef, _vm: &mut VirtualMachine) -> PyObjectRef {
obj.typ()
}

fn object_class_setter(
Expand Down
Loading