Return function attributs in method

This commit is contained in:
Aviv Palivoda
2019-11-02 19:34:10 +02:00
parent 72989cdfd1
commit 841e22434a
2 changed files with 16 additions and 0 deletions

View File

@@ -33,6 +33,10 @@ class Bar:
assert __class__ is Bar
return self.x
def doc_func(self):
"doc string"
pass
@classmethod
def fubar(cls, x):
assert __class__ is cls
@@ -48,6 +52,8 @@ class Bar:
assert Bar.__doc__ == " W00t "
bar = Bar(42)
assert bar.get_x.__doc__ == None
assert bar.doc_func.__doc__ == "doc string"
bar.fubar(2)
Bar.fubar(2)

View File

@@ -1,5 +1,6 @@
use super::objcode::PyCodeRef;
use super::objdict::PyDictRef;
use super::objstr::PyStringRef;
use super::objtuple::PyTupleRef;
use super::objtype::PyClassRef;
use crate::function::PyFuncArgs;
@@ -69,6 +70,10 @@ impl PyMethod {
pub fn new(object: PyObjectRef, function: PyObjectRef) -> Self {
PyMethod { object, function }
}
fn getattribute(&self, name: PyStringRef, vm: &VirtualMachine) -> PyResult {
vm.get_attribute(self.function.clone(), name.clone())
}
}
impl PyValue for PyMethod {
@@ -92,6 +97,11 @@ pub fn init(context: &PyContext) {
"__get__" => context.new_rustfunc(bind_method),
"__call__" => context.new_rustfunc(PyFunctionRef::call),
});
let method_type = &context.types.bound_method_type;
extend_class!(context, method_type, {
"__getattribute__" => context.new_rustfunc(PyMethod::getattribute),
});
}
fn bind_method(