forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjfunction.rs
More file actions
24 lines (20 loc) · 960 Bytes
/
objfunction.rs
File metadata and controls
24 lines (20 loc) · 960 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
use super::super::pyobject::{AttributeProtocol, PyContext, PyFuncArgs, PyResult};
use super::super::vm::VirtualMachine;
pub fn init(context: &PyContext) {
let ref function_type = context.function_type;
function_type.set_attr("__get__", context.new_rustfunc(bind_method));
let ref member_descriptor_type = context.member_descriptor_type;
member_descriptor_type.set_attr("__get__", context.new_rustfunc(member_get));
}
fn bind_method(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
Ok(vm.new_bound_method(args.args[0].clone(), args.args[1].clone()))
}
fn member_get(vm: &mut VirtualMachine, mut args: PyFuncArgs) -> PyResult {
match args.shift().get_attr("function") {
Some(function) => vm.invoke(function, args),
None => {
let attribute_error = vm.context().exceptions.attribute_error.clone();
Err(vm.new_exception(attribute_error, String::from("Attribute Error")))
}
}
}