|
1 | | -use crate::function::PyFuncArgs; |
2 | | -use crate::pyobject::{AttributeProtocol, PyContext, PyResult, TypeProtocol}; |
| 1 | +use super::objtype::PyClassRef; |
| 2 | +use crate::pyobject::{PyContext, PyObjectRef, PyRef, PyResult, PyValue}; |
3 | 3 | use crate::vm::VirtualMachine; |
4 | 4 |
|
5 | | -pub fn init(context: &PyContext) { |
6 | | - let staticmethod_type = &context.staticmethod_type; |
7 | | - extend_class!(context, staticmethod_type, { |
8 | | - "__get__" => context.new_rustfunc(staticmethod_get), |
9 | | - "__new__" => context.new_rustfunc(staticmethod_new), |
10 | | - }); |
| 5 | +#[derive(Clone, Debug)] |
| 6 | +pub struct PyStaticMethod { |
| 7 | + pub callable: PyObjectRef, |
11 | 8 | } |
| 9 | +pub type PyStaticMethodRef = PyRef<PyStaticMethod>; |
12 | 10 |
|
13 | | -// `staticmethod` methods. |
14 | | -fn staticmethod_get(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { |
15 | | - trace!("staticmethod.__get__ {:?}", args.args); |
16 | | - arg_check!( |
17 | | - vm, |
18 | | - args, |
19 | | - required = [ |
20 | | - (cls, Some(vm.ctx.staticmethod_type())), |
21 | | - (_inst, None), |
22 | | - (_owner, None) |
23 | | - ] |
24 | | - ); |
25 | | - match cls.get_attr("function") { |
26 | | - Some(function) => Ok(function), |
27 | | - None => Err(vm.new_attribute_error( |
28 | | - "Attribute Error: staticmethod must have 'function' attribute".to_string(), |
29 | | - )), |
| 11 | +impl PyValue for PyStaticMethod { |
| 12 | + fn class(vm: &mut VirtualMachine) -> PyObjectRef { |
| 13 | + vm.ctx.staticmethod_type() |
30 | 14 | } |
31 | 15 | } |
32 | 16 |
|
33 | | -fn staticmethod_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { |
34 | | - trace!("staticmethod.__new__ {:?}", args.args); |
35 | | - arg_check!(vm, args, required = [(cls, None), (callable, None)]); |
| 17 | +impl PyStaticMethodRef { |
| 18 | + fn new( |
| 19 | + cls: PyClassRef, |
| 20 | + callable: PyObjectRef, |
| 21 | + vm: &mut VirtualMachine, |
| 22 | + ) -> PyResult<PyStaticMethodRef> { |
| 23 | + PyStaticMethod { |
| 24 | + callable: callable.clone(), |
| 25 | + } |
| 26 | + .into_ref_with_type(vm, cls) |
| 27 | + } |
| 28 | + |
| 29 | + fn get(self, _inst: PyObjectRef, _owner: PyObjectRef, _vm: &mut VirtualMachine) -> PyResult { |
| 30 | + Ok(self.callable.clone()) |
| 31 | + } |
| 32 | +} |
36 | 33 |
|
37 | | - let py_obj = vm.ctx.new_instance(cls.clone(), None); |
38 | | - vm.ctx.set_attr(&py_obj, "function", callable.clone()); |
39 | | - Ok(py_obj) |
| 34 | +pub fn init(context: &PyContext) { |
| 35 | + let staticmethod_type = &context.staticmethod_type; |
| 36 | + extend_class!(context, staticmethod_type, { |
| 37 | + "__get__" => context.new_rustfunc(PyStaticMethodRef::get), |
| 38 | + "__new__" => context.new_rustfunc(PyStaticMethodRef::new), |
| 39 | + }); |
40 | 40 | } |
0 commit comments