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
use super::objtype::PyClassRef;
use crate::function::OptionalArg;
use crate::pyobject::{
PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol,
};
use crate::slots::SlotDescriptor;
use crate::vm::VirtualMachine;
#[pyclass]
#[derive(Clone, Debug)]
pub struct PyClassMethod {
callable: PyObjectRef,
}
pub type PyClassMethodRef = PyRef<PyClassMethod>;
impl PyClassMethod {
pub fn new(value: PyObjectRef) -> Self {
Self { callable: value }
}
}
impl PyValue for PyClassMethod {
const HAVE_DICT: bool = true;
fn class(vm: &VirtualMachine) -> PyClassRef {
vm.ctx.classmethod_type()
}
}
impl SlotDescriptor for PyClassMethod {
fn descr_get(
vm: &VirtualMachine,
zelf: PyObjectRef,
obj: Option<PyObjectRef>,
cls: OptionalArg<PyObjectRef>,
) -> PyResult {
let (zelf, obj) = Self::_unwrap(zelf, obj, vm)?;
let cls = cls.unwrap_or_else(|| obj.class().into_object());
Ok(vm.ctx.new_bound_method(zelf.callable.clone(), cls))
}
}
#[pyimpl(with(SlotDescriptor), flags(BASETYPE))]
impl PyClassMethod {
#[pyslot]
fn tp_new(
cls: PyClassRef,
callable: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<PyClassMethodRef> {
PyClassMethod {
callable: callable.clone(),
}
.into_ref_with_type(vm, cls)
}
#[pyproperty(name = "__func__")]
fn func(&self) -> PyObjectRef {
self.callable.clone()
}
}
pub(crate) fn init(context: &PyContext) {
PyClassMethod::extend_class(context, &context.types.classmethod_type);
}