Skip to content

Commit c7702bd

Browse files
authored
Merge pull request RustPython#1582 from palaviv/method-doc
Return function attributes in method
2 parents 153ce3f + 83868f5 commit c7702bd

3 files changed

Lines changed: 17 additions & 6 deletions

File tree

tests/snippets/class.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ def get_x(self):
3333
assert __class__ is Bar
3434
return self.x
3535

36+
def doc_func(self):
37+
"doc string"
38+
pass
39+
3640
@classmethod
3741
def fubar(cls, x):
3842
assert __class__ is cls
@@ -48,6 +52,8 @@ def kungfu(x):
4852
assert Bar.__doc__ == " W00t "
4953

5054
bar = Bar(42)
55+
assert bar.get_x.__doc__ == None
56+
assert bar.doc_func.__doc__ == "doc string"
5157

5258
bar.fubar(2)
5359
Bar.fubar(2)

vm/src/obj/objfunction.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::objcode::PyCodeRef;
22
use super::objdict::PyDictRef;
3+
use super::objstr::PyStringRef;
34
use super::objtuple::PyTupleRef;
45
use super::objtype::PyClassRef;
56
use crate::function::PyFuncArgs;
@@ -69,6 +70,10 @@ impl PyMethod {
6970
pub fn new(object: PyObjectRef, function: PyObjectRef) -> Self {
7071
PyMethod { object, function }
7172
}
73+
74+
fn getattribute(&self, name: PyStringRef, vm: &VirtualMachine) -> PyResult {
75+
vm.get_attribute(self.function.clone(), name.clone())
76+
}
7277
}
7378

7479
impl PyValue for PyMethod {
@@ -92,6 +97,11 @@ pub fn init(context: &PyContext) {
9297
"__get__" => context.new_rustfunc(bind_method),
9398
"__call__" => context.new_rustfunc(PyFunctionRef::call),
9499
});
100+
101+
let method_type = &context.types.bound_method_type;
102+
extend_class!(context, method_type, {
103+
"__getattribute__" => context.new_rustfunc(PyMethod::getattribute),
104+
});
95105
}
96106

97107
fn bind_method(

vm/src/obj/objtype.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -321,12 +321,7 @@ fn type_new_slot(metatype: PyClassRef, args: PyFuncArgs, vm: &VirtualMachine) ->
321321
bases
322322
};
323323

324-
let mut attributes = dict.to_attributes();
325-
326-
// insert __doc__ as None if it is not included in attributes
327-
if !attributes.contains_key("__doc__") {
328-
attributes.insert("__doc__".to_string(), vm.ctx.none());
329-
}
324+
let attributes = dict.to_attributes();
330325

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

0 commit comments

Comments
 (0)