Skip to content

Commit 768a8e5

Browse files
authored
Merge pull request RustPython#4114 from rimi0108/feature/add-staticmethod-init
Feature/add staticmethod init
2 parents b0d5155 + 99bceb6 commit 768a8e5

File tree

2 files changed

+45
-24
lines changed

2 files changed

+45
-24
lines changed

vm/src/builtins/builtinfunc.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,7 @@ impl PyNativeFuncDef {
5656
class: &'static Py<PyType>,
5757
) -> PyRef<PyStaticMethod> {
5858
let callable = self.build_method(ctx, class).into();
59-
PyRef::new_ref(
60-
PyStaticMethod { callable },
61-
ctx.types.staticmethod_type.to_owned(),
62-
None,
63-
)
59+
PyStaticMethod::new_ref(callable, ctx)
6460
}
6561
}
6662

vm/src/builtins/staticmethod.rs

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ use super::{PyStr, PyType, PyTypeRef};
22
use crate::{
33
builtins::builtinfunc::PyBuiltinMethod,
44
class::PyClassImpl,
5+
common::lock::PyMutex,
56
function::{FuncArgs, IntoPyNativeFunc},
67
types::{Callable, Constructor, GetDescriptor, Initializer},
78
Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
89
};
910

1011
#[pyclass(module = false, name = "staticmethod")]
11-
#[derive(Clone, Debug)]
12+
#[derive(Debug)]
1213
pub struct PyStaticMethod {
13-
pub callable: PyObjectRef,
14+
pub callable: PyMutex<PyObjectRef>,
1415
}
1516

1617
impl PyPayload for PyStaticMethod {
@@ -22,18 +23,21 @@ impl PyPayload for PyStaticMethod {
2223
impl GetDescriptor for PyStaticMethod {
2324
fn descr_get(
2425
zelf: PyObjectRef,
25-
_obj: Option<PyObjectRef>,
26+
obj: Option<PyObjectRef>,
2627
_cls: Option<PyObjectRef>,
2728
vm: &VirtualMachine,
2829
) -> PyResult {
29-
let zelf = Self::_zelf(zelf, vm)?;
30-
Ok(zelf.callable.clone())
30+
let (zelf, _obj) = Self::_unwrap(zelf, obj, vm)?;
31+
let x = Ok(zelf.callable.lock().clone());
32+
x
3133
}
3234
}
3335

3436
impl From<PyObjectRef> for PyStaticMethod {
3537
fn from(callable: PyObjectRef) -> Self {
36-
Self { callable }
38+
Self {
39+
callable: PyMutex::new(callable),
40+
}
3741
}
3842
}
3943

@@ -43,7 +47,10 @@ impl Constructor for PyStaticMethod {
4347
fn py_new(cls: PyTypeRef, callable: Self::Args, vm: &VirtualMachine) -> PyResult {
4448
let doc = callable.get_attr("__doc__", vm);
4549

46-
let result = PyStaticMethod { callable }.into_ref_with_type(vm, cls)?;
50+
let result = PyStaticMethod {
51+
callable: PyMutex::new(callable),
52+
}
53+
.into_ref_with_type(vm, cls)?;
4754
let obj = PyObjectRef::from(result);
4855

4956
if let Ok(doc) = doc {
@@ -54,6 +61,18 @@ impl Constructor for PyStaticMethod {
5461
}
5562
}
5663

64+
impl PyStaticMethod {
65+
pub fn new_ref(callable: PyObjectRef, ctx: &Context) -> PyRef<Self> {
66+
PyRef::new_ref(
67+
Self {
68+
callable: PyMutex::new(callable),
69+
},
70+
ctx.types.staticmethod_type.to_owned(),
71+
None,
72+
)
73+
}
74+
}
75+
5776
impl PyStaticMethod {
5877
pub fn new_builtin_ref<F, FKind>(
5978
name: impl Into<PyStr>,
@@ -66,7 +85,9 @@ impl PyStaticMethod {
6685
{
6786
let callable = PyBuiltinMethod::new_ref(name, class, f, ctx).into();
6887
PyRef::new_ref(
69-
Self { callable },
88+
Self {
89+
callable: PyMutex::new(callable),
90+
},
7091
ctx.types.staticmethod_type.to_owned(),
7192
None,
7293
)
@@ -76,7 +97,8 @@ impl PyStaticMethod {
7697
impl Initializer for PyStaticMethod {
7798
type Args = PyObjectRef;
7899

79-
fn init(_zelf: PyRef<Self>, _func: Self::Args, _vm: &VirtualMachine) -> PyResult<()> {
100+
fn init(zelf: PyRef<Self>, callable: Self::Args, _vm: &VirtualMachine) -> PyResult<()> {
101+
*zelf.callable.lock() = callable;
80102
Ok(())
81103
}
82104
}
@@ -88,37 +110,37 @@ impl Initializer for PyStaticMethod {
88110
impl PyStaticMethod {
89111
#[pyproperty(magic)]
90112
fn func(&self) -> PyObjectRef {
91-
self.callable.clone()
113+
self.callable.lock().clone()
92114
}
93115

94116
#[pyproperty(magic)]
95117
fn wrapped(&self) -> PyObjectRef {
96-
self.callable.clone()
118+
self.callable.lock().clone()
97119
}
98120

99121
#[pyproperty(magic)]
100122
fn module(&self, vm: &VirtualMachine) -> PyResult {
101-
self.callable.get_attr("__module__", vm)
123+
self.callable.lock().get_attr("__module__", vm)
102124
}
103125

104126
#[pyproperty(magic)]
105127
fn qualname(&self, vm: &VirtualMachine) -> PyResult {
106-
self.callable.get_attr("__qualname__", vm)
128+
self.callable.lock().get_attr("__qualname__", vm)
107129
}
108130

109131
#[pyproperty(magic)]
110132
fn name(&self, vm: &VirtualMachine) -> PyResult {
111-
self.callable.get_attr("__name__", vm)
133+
self.callable.lock().get_attr("__name__", vm)
112134
}
113135

114136
#[pyproperty(magic)]
115137
fn annotations(&self, vm: &VirtualMachine) -> PyResult {
116-
self.callable.get_attr("__annotations__", vm)
138+
self.callable.lock().get_attr("__annotations__", vm)
117139
}
118140

119141
#[pymethod(magic)]
120142
fn repr(&self, vm: &VirtualMachine) -> Option<String> {
121-
let callable = self.callable.repr(vm).unwrap();
143+
let callable = self.callable.lock().repr(vm).unwrap();
122144
let class = Self::class(vm);
123145

124146
match (
@@ -138,15 +160,17 @@ impl PyStaticMethod {
138160

139161
#[pyproperty(magic)]
140162
fn isabstractmethod(&self, vm: &VirtualMachine) -> PyObjectRef {
141-
match vm.get_attribute_opt(self.callable.clone(), "__isabstractmethod__") {
163+
match vm.get_attribute_opt(self.callable.lock().clone(), "__isabstractmethod__") {
142164
Ok(Some(is_abstract)) => is_abstract,
143165
_ => vm.ctx.new_bool(false).into(),
144166
}
145167
}
146168

147169
#[pyproperty(magic, setter)]
148170
fn set_isabstractmethod(&self, value: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
149-
self.callable.set_attr("__isabstractmethod__", value, vm)?;
171+
self.callable
172+
.lock()
173+
.set_attr("__isabstractmethod__", value, vm)?;
150174
Ok(())
151175
}
152176
}
@@ -155,7 +179,8 @@ impl Callable for PyStaticMethod {
155179
type Args = FuncArgs;
156180
#[inline]
157181
fn call(zelf: &crate::Py<Self>, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
158-
vm.invoke(&zelf.callable, args)
182+
let callable = zelf.callable.lock().clone();
183+
vm.invoke(&callable, args)
159184
}
160185
}
161186

0 commit comments

Comments
 (0)