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
92 lines (83 loc) · 2.8 KB
/
objproperty.rs
File metadata and controls
92 lines (83 loc) · 2.8 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*! Python `property` descriptor class.
*/
use crate::pyobject::{PyContext, PyFuncArgs, PyResult, TypeProtocol};
use crate::vm::VirtualMachine;
pub fn init(context: &PyContext) {
let property_type = &context.property_type;
let property_doc =
"Property attribute.\n\n \
fget\n \
function to be used for getting an attribute value\n \
fset\n \
function to be used for setting an attribute value\n \
fdel\n \
function to be used for del\'ing an attribute\n \
doc\n \
docstring\n\n\
Typical use is to define a managed attribute x:\n\n\
class C(object):\n \
def getx(self): return self._x\n \
def setx(self, value): self._x = value\n \
def delx(self): del self._x\n \
x = property(getx, setx, delx, \"I\'m the \'x\' property.\")\n\n\
Decorators make defining new properties or modifying existing ones easy:\n\n\
class C(object):\n \
@property\n \
def x(self):\n \"I am the \'x\' property.\"\n \
return self._x\n \
@x.setter\n \
def x(self, value):\n \
self._x = value\n \
@x.deleter\n \
def x(self):\n \
del self._x";
context.set_attr(
&property_type,
"__get__",
context.new_rustfunc(property_get),
);
context.set_attr(
&property_type,
"__new__",
context.new_rustfunc(property_new),
);
context.set_attr(
&property_type,
"__doc__",
context.new_str(property_doc.to_string()),
);
// 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 = vm.ctx.new_instance(cls.clone(), None);
vm.ctx.set_attr(&py_obj, "fget", fget.clone());
Ok(py_obj)
}