Skip to content

Convert objobject.rs to new args style #756

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 1 commit into from
Mar 28, 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
18 changes: 8 additions & 10 deletions vm/src/obj/objfunction.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::frame::Scope;
use crate::function::PyFuncArgs;
use crate::obj::objcode::PyCodeRef;
use crate::obj::objtype::PyClassRef;
use crate::pyobject::{IdProtocol, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol};
Expand Down Expand Up @@ -69,16 +68,15 @@ pub fn init(context: &PyContext) {
});
}

fn bind_method(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [(function, None), (obj, None), (cls, None)]
);

fn bind_method(
function: PyObjectRef,
obj: PyObjectRef,
cls: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult {
if obj.is(&vm.get_none()) && !cls.is(&obj.class()) {
Ok(function.clone())
Ok(function)
} else {
Ok(vm.ctx.new_bound_method(function.clone(), obj.clone()))
Ok(vm.ctx.new_bound_method(function, obj))
}
}
95 changes: 23 additions & 72 deletions vm/src/obj/objobject.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::objdict::{self, PyDictRef};
use super::objlist::PyList;
use super::objstr::{self, PyStringRef};
use super::objstr::PyStringRef;
use super::objtype;
use crate::function::PyFuncArgs;
use crate::obj::objproperty::PropertyBuilder;
Expand Down Expand Up @@ -31,69 +31,31 @@ pub fn new_instance(vm: &VirtualMachine, mut args: PyFuncArgs) -> PyResult {
Ok(PyObject::new(PyInstance, cls, dict))
}

fn object_eq(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [(_zelf, Some(vm.ctx.object())), (_other, None)]
);
Ok(vm.ctx.not_implemented())
fn object_eq(_zelf: PyObjectRef, _other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
Copy link
Contributor

Choose a reason for hiding this comment

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

Not a comment on this PR, but I realized I was wrong when I changed this to return NotImplemented, since it looks like cpython does implement this by comparing object ids:

>>> object.__eq__(2, 2)
True

vm.ctx.not_implemented()
}

fn object_ne(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [(_zelf, Some(vm.ctx.object())), (_other, None)]
);

Ok(vm.ctx.not_implemented())
fn object_ne(_zelf: PyObjectRef, _other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
vm.ctx.not_implemented()
}

fn object_lt(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [(_zelf, Some(vm.ctx.object())), (_other, None)]
);

Ok(vm.ctx.not_implemented())
fn object_lt(_zelf: PyObjectRef, _other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
vm.ctx.not_implemented()
}

fn object_le(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [(_zelf, Some(vm.ctx.object())), (_other, None)]
);

Ok(vm.ctx.not_implemented())
fn object_le(_zelf: PyObjectRef, _other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
vm.ctx.not_implemented()
}

fn object_gt(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [(_zelf, Some(vm.ctx.object())), (_other, None)]
);

Ok(vm.ctx.not_implemented())
fn object_gt(_zelf: PyObjectRef, _other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
vm.ctx.not_implemented()
}

fn object_ge(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [(_zelf, Some(vm.ctx.object())), (_other, None)]
);

Ok(vm.ctx.not_implemented())
fn object_ge(_zelf: PyObjectRef, _other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
vm.ctx.not_implemented()
}

fn object_hash(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(_zelf, Some(vm.ctx.object()))]);

// For now default to non hashable
fn object_hash(_zelf: PyObjectRef, vm: &VirtualMachine) -> PyResult {
Err(vm.new_type_error("unhashable type".to_string()))
}

Expand Down Expand Up @@ -147,15 +109,12 @@ fn object_delattr(obj: PyObjectRef, attr_name: PyStringRef, vm: &VirtualMachine)
}
}

fn object_str(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(zelf, Some(vm.ctx.object()))]);
vm.call_method(zelf, "__repr__", vec![])
fn object_str(zelf: PyObjectRef, vm: &VirtualMachine) -> PyResult {
vm.call_method(&zelf, "__repr__", vec![])
}

fn object_repr(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(obj, Some(vm.ctx.object()))]);
let address = obj.get_id();
Ok(vm.new_str(format!("<{} object at 0x{:x}>", obj.class().name, address)))
fn object_repr(zelf: PyObjectRef, _vm: &VirtualMachine) -> String {
format!("<{} object at 0x{:x}>", zelf.class().name, zelf.get_id())
}

pub fn object_dir(obj: PyObjectRef, vm: &VirtualMachine) -> PyList {
Expand Down Expand Up @@ -235,34 +194,26 @@ fn object_dict(object: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyDictRef>
}
}

fn object_getattribute(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [
(obj, Some(vm.ctx.object())),
(name_str, Some(vm.ctx.str_type()))
]
);
let name = objstr::get_value(&name_str);
fn object_getattribute(obj: PyObjectRef, name_str: PyStringRef, vm: &VirtualMachine) -> PyResult {
let name = &name_str.value;
trace!("object.__getattribute__({:?}, {:?})", obj, name);
let cls = obj.class();

if let Some(attr) = objtype::class_get_attr(&cls, &name) {
let attr_class = attr.class();
if objtype::class_has_attr(&attr_class, "__set__") {
if let Some(descriptor) = objtype::class_get_attr(&attr_class, "__get__") {
return vm.invoke(descriptor, vec![attr, obj.clone(), cls.into_object()]);
return vm.invoke(descriptor, vec![attr, obj, cls.into_object()]);
}
}
}

if let Some(obj_attr) = object_getattr(&obj, &name) {
Ok(obj_attr)
} else if let Some(attr) = objtype::class_get_attr(&cls, &name) {
vm.call_get_descriptor(attr, obj.clone())
vm.call_get_descriptor(attr, obj)
} else if let Some(getter) = objtype::class_get_attr(&cls, "__getattr__") {
vm.invoke(getter, vec![obj.clone(), name_str.clone()])
vm.invoke(getter, vec![obj, name_str.into_object()])
} else {
Err(vm.new_attribute_error(format!("{} has no attribute '{}'", obj, name)))
}
Expand Down