forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassmethod.rs
More file actions
97 lines (86 loc) · 2.92 KB
/
classmethod.rs
File metadata and controls
97 lines (86 loc) · 2.92 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
93
94
95
96
97
use super::PyTypeRef;
use crate::{
builtins::PyBoundMethod,
types::{Constructor, GetDescriptor},
PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, VirtualMachine,
};
/// classmethod(function) -> method
///
/// Convert a function to be a class method.
///
/// A class method receives the class as implicit first argument,
/// just like an instance method receives the instance.
/// To declare a class method, use this idiom:
///
/// class C:
/// @classmethod
/// def f(cls, arg1, arg2, ...):
/// ...
///
/// It can be called either on the class (e.g. C.f()) or on an instance
/// (e.g. C().f()). The instance is ignored except for its class.
/// If a class method is called for a derived class, the derived class
/// object is passed as the implied first argument.
///
/// Class methods are different than C++ or Java static methods.
/// If you want those, see the staticmethod builtin.
#[pyclass(module = false, name = "classmethod")]
#[derive(Clone, Debug)]
pub struct PyClassMethod {
callable: PyObjectRef,
}
impl From<PyObjectRef> for PyClassMethod {
fn from(value: PyObjectRef) -> Self {
Self { callable: value }
}
}
impl PyValue for PyClassMethod {
fn class(vm: &VirtualMachine) -> &PyTypeRef {
&vm.ctx.types.classmethod_type
}
}
impl GetDescriptor for PyClassMethod {
fn descr_get(
zelf: PyObjectRef,
obj: Option<PyObjectRef>,
cls: Option<PyObjectRef>,
vm: &VirtualMachine,
) -> PyResult {
let (zelf, obj) = Self::_unwrap(zelf, obj, vm)?;
let cls = cls.unwrap_or_else(|| obj.clone_class().into());
Ok(PyBoundMethod::new_ref(cls, zelf.callable.clone(), &vm.ctx).into())
}
}
impl Constructor for PyClassMethod {
type Args = PyObjectRef;
fn py_new(cls: PyTypeRef, callable: Self::Args, vm: &VirtualMachine) -> PyResult {
PyClassMethod { callable }.into_pyresult_with_type(vm, cls)
}
}
impl PyClassMethod {
pub fn new_ref(callable: PyObjectRef, ctx: &PyContext) -> PyRef<Self> {
PyRef::new_ref(Self { callable }, ctx.types.classmethod_type.clone(), None)
}
}
#[pyimpl(with(GetDescriptor, Constructor), flags(BASETYPE, HAS_DICT))]
impl PyClassMethod {
#[pyproperty(magic)]
fn func(&self) -> PyObjectRef {
self.callable.clone()
}
#[pyproperty(magic)]
fn isabstractmethod(&self, vm: &VirtualMachine) -> PyObjectRef {
match vm.get_attribute_opt(self.callable.clone(), "__isabstractmethod__") {
Ok(Some(is_abstract)) => is_abstract,
_ => vm.ctx.new_bool(false).into(),
}
}
#[pyproperty(magic, setter)]
fn set_isabstractmethod(&self, value: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
self.callable.set_attr("__isabstractmethod__", value, vm)?;
Ok(())
}
}
pub(crate) fn init(context: &PyContext) {
PyClassMethod::extend_class(context, &context.types.classmethod_type);
}