8000 type.__getattribute__ should be bound to type! by cthulahoops · Pull Request #189 · RustPython/RustPython · GitHub
[go: up one dir, main page]

Skip to content

type.__getattribute__ should be bound to type! #189

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 2 commits into from
Nov 4, 2018
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
9 changes: 3 additions & 6 deletions tests/snippets/class.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,9 @@ def kungfu(x):


bar = Bar()
bar.fubar(2)

# TODO: make below work:
# Bar.fubar(2)
bar.fubar(2)
Bar.fubar(2)

bar.kungfu(3)
# TODO: make below work:
# Bar.kungfu(3)

Bar.kungfu(3)
18 changes: 14 additions & 4 deletions vm/src/obj/objfunction.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::super::pyobject::{
AttributeProtocol, PyContext, PyFuncArgs, PyObject, PyObjectKind, PyObjectRef, PyResult,
TypeProtocol,
AttributeProtocol, IdProtocol, PyContext, PyFuncArgs, PyObject, PyObjectKind, PyObjectRef,
PyResult, TypeProtocol,
};
use super::super::vm::VirtualMachine;
use super::objtype;
Expand All @@ -22,7 +22,17 @@ pub fn init(context: &PyContext) {
}

fn bind_method(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
Ok(vm.new_bound_method(args.args[0].clone(), args.args[1].clone()))
arg_check!(
vm,
args,
required = [(function, None), (obj, None), (cls, None)]
);

if obj.is(&vm.get_none()) && !cls.is(&obj.typ()) {
Ok(function.clone())
} else {
Ok(vm.ctx.new_bound_method(function.clone(), obj.clone()))
}
}

fn member_get(vm: &mut VirtualMachine, mut args: PyFuncArgs) -> PyResult {
Expand Down Expand Up @@ -50,7 +60,7 @@ fn classmethod_get(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
match cls.get_attr("function") {
Some(function) => {
let py_obj = owner.clone();
let py_method = vm.new_bound_method(function, py_obj);
let py_method = vm.ctx.new_bound_method(function, py_obj);
Ok(py_method)
}
None => {
Expand Down
2 changes: 1 addition & 1 deletion vm/src/obj/objproperty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn property_get(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {

match cls.get_attr("fget") {
Some(getter) => {
let py_method = vm.new_bound_method(getter, inst.clone());
let py_method = vm.ctx.new_bound_method(getter, inst.clone());
vm.invoke(py_method, PyFuncArgs::default())
}
None => {
Expand Down
3 changes: 2 additions & 1 deletion vm/src/obj/objtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub fn init(context: &PyContext) {
type_type.set_attr("__class__", context.new_member_descriptor(type_new));
type_type.set_attr("__repr__", context.new_rustfunc(type_repr));
type_type.set_attr("__prepare__", context.new_rustfunc(type_prepare));
type_type.set_attr("__getattribute__", context.new_rustfunc(type_getattribute));
}

fn type_mro(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
Expand Down Expand Up @@ -85,7 +86,7 @@ pub fn get_type_name(typ: &PyObjectRef) -> String {
}

pub fn type_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
debug!("type.__new__{:?}", args);
debug!("type.__new__ {:?}", args);
if args.args.len() == 2 {
arg_check!(
vm,
Expand Down
13 changes: 8 additions & 5 deletions vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,6 @@ impl VirtualMachine {
self.ctx.none()
}

pub fn new_bound_method(&self, function: PyObjectRef, object: PyObjectRef) -> PyObjectRef {
self.ctx.new_bound_method(function, object)
}

pub fn get_type(&self) -> PyObjectRef {
self.ctx.type_type()
}
Expand Down Expand Up @@ -202,7 +198,13 @@ impl VirtualMachine {
let cls = obj.typ();
match cls.get_attr(method_name) {
Some(func) => {
trace!("vm.call_method {:?} {:?} -> {:?}", obj, method_name, func);
trace!(
"vm.call_method {:?} {:?} {:?} -> {:?}",
obj,
cls,
method_name,
func
);
let wrapped = self.call_get_descriptor(func, obj.clone())?;
self.invoke(wrapped, args)
}
Expand Down Expand Up @@ -423,6 +425,7 @@ impl VirtualMachine {

// get_attribute should be used for full attribute access (usually from user code).
pub fn get_attribute(&mut self, obj: PyObjectRef, attr_name: PyObjectRef) -> PyResult {
trace!("vm.__getattribute__: {:?} {:?}", obj, attr_name);
self.call_method(&obj, "__getattribute__", vec![attr_name])
}

Expand Down
0