Skip to content

Return function attributes in method #1582

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 8, 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
6 changes: 6 additions & 0 deletions tests/snippets/class.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def get_x(self):
assert __class__ is Bar
return self.x

def doc_func(self):
"doc string"
pass

@classmethod
def fubar(cls, x):
assert __class__ is cls
Expand All @@ -48,6 +52,8 @@ def kungfu(x):
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)
Expand Down
10 changes: 10 additions & 0 deletions vm/src/obj/objfunction.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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(
Expand Down
7 changes: 1 addition & 6 deletions vm/src/obj/objtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,7 @@ fn type_new_slot(metatype: PyClassRef, args: PyFuncArgs, vm: &VirtualMachine) ->
bases
};

let mut attributes = dict.to_attributes();

// insert __doc__ as None if it is not included in attributes
if !attributes.contains_key("__doc__") {
attributes.insert("__doc__".to_string(), vm.ctx.none());
}
let attributes = dict.to_attributes();

let mut winner = metatype.clone();
for base in &bases {
Expand Down