forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjproperty.rs
More file actions
66 lines (59 loc) · 1.76 KB
/
Copy pathobjproperty.rs
File metadata and controls
66 lines (59 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*! Python `property` descriptor class.
*/
use super::super::pyobject::{
PyContext, PyFuncArgs, PyObject, PyObjectKind, PyObjectRef, PyResult, TypeProtocol,
};
use super::super::vm::VirtualMachine;
use super::objtype;
pub fn init(context: &PyContext) {
let ref property_type = context.property_type;
context.set_attr(
&property_type,
"__get__",
context.new_rustfunc(property_get),
);
context.set_attr(
&property_type,
"__new__",
context.new_rustfunc(property_new),
);
// TODO: how to handle __set__ ?
}
// `property` methods.
fn property_get(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
trace!("property.__get__ {:?}", args.args);
arg_check!(
vm,
args,
required = [
(cls, Some(vm.ctx.property_type())),
(inst, None),
(_owner, None)
]
);
match vm.ctx.get_attr(&cls, "fget") {
Some(getter) => {
let py_method = vm.ctx.new_bound_method(getter, inst.clone());
vm.invoke(py_method, PyFuncArgs::default())
}
None => {
let attribute_error = vm.context().exceptions.attribute_error.clone();
Err(vm.new_exception(
attribute_error,
String::from("Attribute Error: property must have 'fget' attribute"),
))
}
}
}
fn property_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
trace!("property.__new__ {:?}", args.args);
arg_check!(vm, args, required = [(cls, None), (fget, None)]);
let py_obj = PyObject::new(
PyObjectKind::Instance {
dict: vm.ctx.new_dict(),
},
cls.clone(),
);
vm.ctx.set_attr(&py_obj, "fget", fget.clone());
Ok(py_obj)
}