forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjclassmethod.rs
More file actions
44 lines (38 loc) · 1.16 KB
/
objclassmethod.rs
File metadata and controls
44 lines (38 loc) · 1.16 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
use super::objtype::PyClassRef;
use crate::pyobject::{PyContext, PyObjectRef, PyRef, PyResult, PyValue};
use crate::vm::VirtualMachine;
#[derive(Clone, Debug)]
pub struct PyClassMethod {
pub callable: PyObjectRef,
}
pub type PyClassMethodRef = PyRef<PyClassMethod>;
impl PyValue for PyClassMethod {
const HAVE_DICT: bool = true;
fn class(vm: &VirtualMachine) -> PyClassRef {
vm.ctx.classmethod_type()
}
}
impl PyClassMethodRef {
fn new(
cls: PyClassRef,
callable: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<PyClassMethodRef> {
PyClassMethod {
callable: callable.clone(),
}
.into_ref_with_type(vm, cls)
}
fn get(self, _inst: PyObjectRef, owner: PyObjectRef, vm: &VirtualMachine) -> PyResult {
Ok(vm
.ctx
.new_bound_method(self.callable.clone(), owner.clone()))
}
}
pub fn init(context: &PyContext) {
let classmethod_type = &context.classmethod_type;
extend_class!(context, classmethod_type, {
"__get__" => context.new_rustfunc(PyClassMethodRef::get),
"__new__" => context.new_rustfunc(PyClassMethodRef::new)
});
}